client输入内容发送到server端输出

[1].[代码] [C/C++]代码 跳至[1] [2]

view source print ?
01 #include <stdio.h>
02 #include <iostream>
03 #include <winsock2.h>
04 #pragma comment(lib, "ws2_32.lib")
05 using namespace std;
06 int main()
07 {
08     // 初始化 Winsock.
09     WSADATA wsaData;
10     int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
11     if ( iResult != NO_ERROR )
12     {
13         cout<<"failed to initialize!"<<endl;
14         return 0;
15     }
16  
17     // 建立socket socket.
18     SOCKET client;
19     client = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
20     if ( client == INVALID_SOCKET )
21     {
22         cout<<"failed to create client socket!"<<endl;
23         WSACleanup();
24         return 0;
25     }
26  
27     // 连接到服务器.
28     sockaddr_in clientService;
29     clientService.sin_family = AF_INET;
30     clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
31     clientService.sin_port = htons( 13579 );
32  
33     if ( connect( client, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
34     {
35         cout<<"Failed to connect"<<endl;
36         closesocket(client);
37         WSACleanup();
38         return 0;
39     }
40  
41     // 发送数据.
42     int bytesSent;
43     char sendbuf[4096] = "Client: Sending data.";
44     while(TRUE)
45     {
46         bytesSent = send( client, sendbuf, strlen(sendbuf), 0 );
47         gets_s(sendbuf, 4096);
48     }
49  
50     closesocket(client);
51     WSACleanup();
52     return 0;
53 }

[2].[代码] [C/C++]代码 跳至[1] [2]

01 #include <iostream>
02 #include <WinSock2.h>
03 #pragma comment(lib, "ws2_32.lib")
04  
05 using namespace std;
06  
07 int main()
08 {
09     WSADATA wsaData;
10  
11     int iInit = WSAStartup(MAKEWORD(2, 2), &wsaData);
12     if (iInit != NO_ERROR)
13     {
14         cout<<"failed to initialize!"<<endl;
15         return 0;
16     }
17  
18     SOCKET server = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
19     if (server == INVALID_SOCKET)
20     {
21         cout<<"failed to create server socket!"<<endl;
22         WSACleanup();
23         return 0;
24     }
25  
26     sockaddr_in bindinfo;
27     bindinfo.sin_family = AF_INET;
28     bindinfo.sin_addr.s_addr =  inet_addr( "127.0.0.1" );
29     bindinfo.sin_port = htons(13579);
30     if ( bind( server, (SOCKADDR*) &bindinfo, sizeof(bindinfo) ) == SOCKET_ERROR )
31     {
32         cout<<"failed to bind!"<<endl;
33         closesocket(server);
34         WSACleanup();
35         return 0;
36     }
37  
38     //listen
39     if (listen(server, 1) == SOCKET_ERROR) {
40         cout<<"listen failed"<<endl;
41         closesocket(server);
42         WSACleanup();
43         return 0;
44     }
45  
46     //accept and block
47     SOCKET socketWork = accept(server, NULL, NULL);
48     if (socketWork == INVALID_SOCKET) {
49         wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
50         closesocket(server);
51         WSACleanup();
52         return 0;
53     }
54  
55     int byteRecv = SOCKET_ERROR;
56     char recvBuf[4096] = "";
57  
58     while(TRUE)
59     {
60         byteRecv = recv(socketWork, recvBuf, 4096, 0);
61         if (byteRecv == 0)
62         {
63             break;
64         }
65         recvBuf[byteRecv] = 0;
66         cout<<recvBuf<<endl;
67     }
68  
69     closesocket(server);
70     WSACleanup();
71     return 0;
72  
73 }

你可能感兴趣的:(client输入内容发送到server端输出)