运行程序的时候,一般都是先运行server端,后运行client端
一次传输
server 端
#include
#include
#pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll,不加载的话,好像会出错
#pragma warning(disable:4996)
int main()
{
//初始化 DLL
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//创建套接字
SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
//绑定套接字
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = PF_INET; //使用IPv4地址
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
sockAddr.sin_port = htons(18000); //端口
bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
//进入监听状态
listen(servSock, 20);
//接收客户端请求
SOCKADDR clntAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
//向客户端发送数据
char *str = "Hello World!";
send(clntSock, str, strlen(str) + sizeof(char), NULL);
//关闭套接字
closesocket(clntSock);
closesocket(servSock);
//终止 DLL 的使用
WSACleanup();
system("pause");
return 0;
}
client 端
#include
#include
#include
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll
#pragma warning(disable:4996)
int main()
{
//初始化DLL
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);//采用的是最新的2.2版本 MAKEWORD()用以对版本号进行切换,是一个宏函数
//创建套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//向服务器发起请求
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockAddr.sin_port = htons(18000);
connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
//接收服务器传回的数据
char szBuffer[MAXBYTE] = { 0 };
recv(sock, szBuffer, MAXBYTE, NULL);
//输出接收到的数据
printf("Message form server: %s\n", szBuffer);
system("pause");
//关闭套接字
closesocket(sock);
//终止使用 DLL
WSACleanup();
return 0;
}
循环传输数据
server端
#include
#include
#pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll
#pragma warning(disable:4996)
#define BUF_SIZE 100
using namespace std;
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET array_servSock[10] = {};
//创建套接字
SOCKET servSock = socket(AF_INET, SOCK_STREAM, 0);
//绑定套接字
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = PF_INET; //使用IPv4地址
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
sockAddr.sin_port = htons(18000); //端口
bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
//进入监听状态
listen(servSock, 20);
//接收客户端请求
SOCKADDR clntAddr;
int nSize = sizeof(SOCKADDR);
char buffer[BUF_SIZE] = { 0 }; //缓冲区
char *str = "Hello World!";
SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
while (1) // 循环发送数据
{
send(clntSock, str, strlen(str) + sizeof(char), NULL);
memset(buffer, 0, BUF_SIZE); //重置缓冲区
}
closesocket(clntSock); //关闭套接字
//关闭套接字
closesocket(servSock);
//终止 DLL 的使用
WSACleanup();
return 0;
}
client 端
#include
#include
#include
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll
#pragma warning(disable:4996)
#define BUF_SIZE 100
int main()
{
//初始化DLL
WORD myVersionRequest;
myVersionRequest = MAKEWORD(2, 2);
WSADATA wsaDate;
int err;
err = WSAStartup(myVersionRequest, &wsaDate);
if (!err)
{
printf("已经打开套接字\n");
}
else
{
printf("未打开套接字\n");
return 0;
}
//向服务器发起请求
SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockAddr;
//memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockAddr.sin_family = PF_INET;
sockAddr.sin_port = htons(18000);
char bufRecv[BUF_SIZE] = { 0 };
connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
while (1) //循环接收数据
{
recv(sock, bufRecv, BUF_SIZE, 0);
//输出接收到的数据
printf("Message form server: %s\n", bufRecv);
memset(bufRecv, 0, BUF_SIZE); //重置缓冲区
}
closesocket(sock); //关闭套接字
WSACleanup(); //终止使用 DLL
return 0;
}