socket使用问题
我下载了一个socket助手,如何使用它实现两台电脑的通信?希望真正懂得人回答,别随便找一段话复制,谢谢...
我下载了一个socket助手,如何使用它实现两台电脑的通信?
希望真正懂得人回答,别随便找一段话复制,谢谢 展开
希望真正懂得人回答,别随便找一段话复制,谢谢 展开
1个回答
2018-08-15 · 百度知道官方认证企业
腾讯电脑管家
腾讯电脑管家是腾讯公司推出的免费安全管理软件,能有效预防和解决计算机上常见的安全风险,并帮助用户解决各种电脑“疑难杂症”、优化系统和网络环境,是中国综合能力最强、最稳定的安全软件。
向TA提问
关注
展开全部
在一台电脑上创建一个TCP Socket服务器应用程序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <stdlib.h>
#pragma comment (lib,"WS2_32.lib");
#define BUF_SIZE 64
int main()
{
WSADATA wsd; //WSADATA变量,用于初始化Windows Socket
SOCKET sServer; //服务器socket,用于监听客户端请求
SOCKET sClient; //客户端socket,用于实现与客户端的通信
int retVal; //调用各种socket函数的返回值
char buf[BUF_SIZE];
//初始化Socket环境
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
//printf("WASStartup failed !\n");
std::cout << "WASStartup failed !" << std::endl;
return 1;
}
//创建用于监听的Socket
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sServer == INVALID_SOCKET)
{
//printf("socket failed !\n");
std::cout << "socket failed !" << std::endl;
return -1;
}
//设置服务器Socket地址
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(9990);
addrServ.sin_addr.S_un.S_addr = htons(INADDR_ANY);
//绑定Sockets Server 到本地地址
retVal = bind(sServer, (const struct sockaddr *)&addrServ, sizeof(SOCKADDR_IN));
if (SOCKET_ERROR == retVal)
{
std::cout << "bind failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
//在Socket Server上进行监听
retVal = listen(sServer,1);
if (SOCKET_ERROR==retVal)
{
std::cout << "listen failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
//接受来自客户端的请求
std::cout << "TCP Server Start..." << std::endl;
sockaddr_in addrClient; //客户端地址
int addrClientLen = sizeof(addrClient);
sClient = accept(sServer, (sockaddr FAR *)&addrClient, &addrClientLen);
if (INVALID_SOCKET == sClient)
{
std::cout << "accept failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <WinSock2.h>
#include <stdlib.h>
#pragma comment (lib,"WS2_32.lib");
#define BUF_SIZE 64
int main()
{
WSADATA wsd; //WSADATA变量,用于初始化Windows Socket
SOCKET sServer; //服务器socket,用于监听客户端请求
SOCKET sClient; //客户端socket,用于实现与客户端的通信
int retVal; //调用各种socket函数的返回值
char buf[BUF_SIZE];
//初始化Socket环境
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
//printf("WASStartup failed !\n");
std::cout << "WASStartup failed !" << std::endl;
return 1;
}
//创建用于监听的Socket
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sServer == INVALID_SOCKET)
{
//printf("socket failed !\n");
std::cout << "socket failed !" << std::endl;
return -1;
}
//设置服务器Socket地址
SOCKADDR_IN addrServ;
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(9990);
addrServ.sin_addr.S_un.S_addr = htons(INADDR_ANY);
//绑定Sockets Server 到本地地址
retVal = bind(sServer, (const struct sockaddr *)&addrServ, sizeof(SOCKADDR_IN));
if (SOCKET_ERROR == retVal)
{
std::cout << "bind failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
//在Socket Server上进行监听
retVal = listen(sServer,1);
if (SOCKET_ERROR==retVal)
{
std::cout << "listen failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
//接受来自客户端的请求
std::cout << "TCP Server Start..." << std::endl;
sockaddr_in addrClient; //客户端地址
int addrClientLen = sizeof(addrClient);
sClient = accept(sServer, (sockaddr FAR *)&addrClient, &addrClientLen);
if (INVALID_SOCKET == sClient)
{
std::cout << "accept failed !" << std::endl;
closesocket(sServer);
WSACleanup();
return -1;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询