简单的TCP回射服务
1 //TCP服务器端
2 #include <stdio.h>
3 #include <winsock2.h>
4 #include <Windows.h>
5
6 #pragma comment(lib,"ws2_32.lib")
7 #define PORT 6000
8 int main()
9 {
10 char buf[512];//接发数据的缓冲区
11 memset(buf, 0, 1024);//格式化缓冲区
12 SOCKET serversock = INVALID_SOCKET;//用来监听信息的套接字
13 SOCKET acceptsock = INVALID_SOCKET;//用来接发数据的套接字
14 struct sockaddr_in clientAddr;// 保存客户端地址信息
15 struct sockaddr_in serverAddr;//保存本地主机地址信息
16 int serAddrLen = sizeof(serverAddr);
17 //初始化套接字
18 WSADATA wsa;
19 if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
20 {
21 printf("套接字初始化失败:%d.\n",WSAGetLastError());
22 return -1;
23 }
24 printf("套接字初始化完成..........\n");
25 //建立一个TCP套接字
26 serversock = socket(AF_INET, SOCK_STREAM, 0);
27 if (serversock == SOCKET_ERROR)
28 {
29 printf("创建套接字失败:%d.\n",WSAGetLastError());
30 return -1;
31 }
32 printf("成功创建套接字...........\n");
33 //绑定地址信息
34 serverAddr.sin_family = AF_INET;
35 serverAddr.sin_port = htons(PORT);
36 serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
37 if (bind(serversock, (SOCKADDR*)& serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
38 {
39 printf("绑定套接字失败:%d.\n", WSAGetLastError());
40 closesocket(serversock);
41 WSACleanup();
42 return -1;
43 }
44 printf("绑定成功,准备监听..........\n");
45 //监听套接字
46 if (listen(serversock, 5) == SOCKET_ERROR)
47 {
48 printf("监听失败:%d.\n", WSAGetLastError());
49 closesocket(serversock);
50 WSACleanup();
51 return -1;
52 }
53 printf("准备接受连接请求..........\n");
54 if ((acceptsock = accept(serversock, (SOCKADDR*)& serverAddr, &serAddrLen)) == SOCKET_ERROR)
55 {
56 printf("接收连接请求失败.\n");
57 closesocket(acceptsock);
58 WSACleanup();
59 return -1;
60 }
61 printf("准备收发数据..........\n");
62 while (1)
63 {
64 //接收客户端发来的数据
65 recv(acceptsock, buf, 512, 0);
66 printf("接收到的数据:%s.\n",buf);
67 //如发现exit,则退出循环
68 if (strncmp(buf, "exit", sizeof("exit")) == 0)
69 {
70 printf("退出接收循环.....\n");
71 break;
72 }
73 // 返回数据给客户端
74 send(acceptsock, buf, strlen(buf), 0);
75 printf("返回的的数据:%s.\n\n",buf);
76 memset(buf, 0, sizeof(buf));
77 }
78 return 0;
79 }
1 //TCP客户端
2 #include <stdio.h>
3 #include <winsock2.h>
4 #include <Windows.h>
5
6 #pragma comment(lib,"ws2_32.lib")
7 #define PORT 6000
8 int main()
9 {
10 char buf[1024];//发送缓冲区
11 char recvBuf[1024];//接收缓冲区
12 memset(buf, 0, 1024); //格式化缓冲区
13 memset(recvBuf, 0, 1024);//格式化缓冲区
14 struct sockaddr_in serAddr;//保存服务器端地址信息
15 int ServerAddrLen = sizeof(serAddr);
16 //初始化套接字
17 WSADATA wsa;
18 if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
19 {
20 printf("初始化失败.\n");
21 return -1;
22 }
23 printf("套接字初始化成功..........\n");
24 //建立一个TCP套接字
25 SOCKET clientsock= socket(AF_INET, SOCK_STREAM, 0);
26 if (clientsock== INVALID_SOCKET)
27 {
28 printf("套接字创建失败.\n");
29 return -1;
30 }
31 printf("成功创建套接字..........\n");
32 //保存服务器的地址信息
33 serAddr.sin_family = AF_INET;
34 serAddr.sin_port = htons(PORT);
35 serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
36 printf("准备连接到:%s:%d....\n", inet_ntoa(serAddr.sin_addr), htons(serAddr.sin_port));
37 //连接到服务器端
38 if (connect(clientsock, (SOCKADDR*)& serAddr, &ServerAddrLen) == SOCKET_ERROR)
39 {
40 printf("连接到服务器端失败:%d", WSAGetLastError());
41 closesocket(clientsock);
42 WSACleanup();
43 return -1;
44 }
45 printf("连接成功,准备发送数据........\n");
46 while (1)
47 {
48 printf("\n要发送的数据为:");
49 gets(buf);//获取缓冲区中数据
50 //发送数据
51 int Sent = send(clientsock, buf, strlen(buf), 0);
52 if (Sent == 0)
53 {
54 printf("要发送的数据失败....\n");
55 return -1;
56 }
57 // 接收数据
58 int Recv = recv(clientsock, recvBuf, 255, 0);
59 if (Recv == 0)
60 {
61 printf("数据接收失败.....\n");
62 return -1;
63 }
64 printf("从服务接收到的数据:%s\n\n", recvBuf);
65 memset(recvBuf, 0, sizeof(recvBuf));
66 }
67 //关闭SOCKET连接
68 closesocket(clientsock);
69 WSACleanup();
70 system("pause");
71 return 0;
72 }
调试结果如下: