stock 服务器 客服端

Server.exe PortNumber,例如Server 8000

Client.exe IPAddress PortNumber,例如Client 127.0.0.1 8000

然后在客户端的命令行输入字符串并回车,客户端将会把消息发送到服务器,服务器再把消息传回客户端。
标签: Socket

代码片段(2)

[代码] 服务器端,Server.cpp

01 //Server.cpp
02 #include <winsock2.h>
03 #include <stdio.h>
04 #include <windows.h>
05 #pragma comment(lib,"ws2_32.lib")
06 int main(int argc, char* argv[]){
07 //判断是否输入了端口号
08 if(argc!=2){
09 printf("Usage: %s PortNumber\n",argv[0]);
10 exit(-1);
11 }
12 //把端口号转化成整数
13 short port;
14 if((port = atoi(argv[1]))==0){
15 printf("端口号有误!");
16 exit(-1);
17 }
18 WSADATA wsa;
19 //初始化套接字DLL
20 if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
21 printf("套接字初始化失败!");
22 exit(-1);
23 }
24 //创建套接字
25 SOCKET serverSocket;
26 if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
27 printf("创建套接字失败!");
28 exit(-1);
29 }
30 struct sockaddr_in serverAddress;
31 memset(&serverAddress,0,sizeof(sockaddr_in));
32 serverAddress.sin_family=AF_INET;
33 serverAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
34 serverAddress.sin_port = htons(port);
35 //绑定
36 if(bind(serverSocket,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
37 printf("套接字绑定到端口失败!端口: %d\n",port);
38 exit(-1);
39 }
40 //进入侦听状态
41 if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR){
42 printf("侦听失败!");
43 exit(-1);
44 }
45 printf("Server %d is listening......\n",port);
46 SOCKET clientSocket;//用来和客户端通信的套接字
47 struct sockaddr_in clientAddress;//用来和客户端通信的套接字地址
48 memset(&clientAddress,0,sizeof(clientAddress));
49 int addrlen = sizeof(clientAddress);
50 //接受连接
51 if((clientSocket=accept(serverSocket,(sockaddr*)&clientAddress,&addrlen))==INVALID_SOCKET){
52 printf("接受客户端连接失败!");
53 exit(-1);
54 }
55 printf("Accept connection from %s\n",inet_ntoa(clientAddress.sin_addr));
56 char buf[4096];
57 while(1){
58 //接收数据
59 int bytes;
60 if((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR){
61 printf("接收数据失败!\n");
62 exit(-1);
63 }
64 buf[bytes]='\0';
65 printf("Message from %s: %s\n",inet_ntoa(clientAddress.sin_addr),buf);
66 if(send(clientSocket,buf,bytes,0)==SOCKET_ERROR){
67 printf("发送数据失败!");
68 exit(-1);
69 }
70   
71 }
72 //清理套接字占用的资源
73 WSACleanup();
74 return 0;
75 }

[代码] 客户端,Client.cp

01 //Client.cpp
02 #include <winsock2.h>
03 #include <stdio.h>
04 #include <windows.h>
05 #pragma comment(lib,"ws2_32.lib")
06   
07 int main(int argc, char* argv[]){
08 //判断是否输入了IP地址和端口号
09 if(argc!=3){
10 printf("Usage: %s IPAddress PortNumber\n",argv[0]);
11 exit(-1);
12 }
13 //把字符串的IP地址转化为u_long
14 unsigned long ip;
15 if((ip=inet_addr(argv[1]))==INADDR_NONE){
16 printf("不合法的IP地址:%s",argv[1]);
17 exit(-1);
18 }
19 //把端口号转化成整数
20 short port;
21 if((port = atoi(argv[2]))==0){
22 printf("端口号有误!");
23 exit(-1);
24 }
25 printf("Connecting to %s:%d......\n",inet_ntoa(*(in_addr*)&ip),port);
26 WSADATA wsa;
27 //初始化套接字DLL
28 if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
29 printf("套接字初始化失败!");
30 exit(-1);
31 }
32 //创建套接字
33 SOCKET sock;
34 if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
35 printf("创建套接字失败!");
36 exit(-1);
37 }
38 struct sockaddr_in serverAddress;
39 memset(&serverAddress,0,sizeof(sockaddr_in));
40 serverAddress.sin_family=AF_INET;
41 serverAddress.sin_addr.S_un.S_addr = ip;
42 serverAddress.sin_port = htons(port);
43 //建立和服务器的连接
44 if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){
45 printf("建立连接失败!");
46 exit(-1);
47 }
48 char buf[4096];
49 while(1){
50 printf(">");
51 //从控制台读取一行数据
52 gets(buf);
53 //发送给服务器
54 if(send(sock,buf,strlen(buf),0)==SOCKET_ERROR){
55 printf("发送数据失败!");
56 exit(-1);
57 }
58 int bytes;
59 if((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR){
60 printf("接收数据失败!\n");
61 exit(-1);
62 }
63 buf[bytes]='\0';
64 printf("Message from %s: %s\n",inet_ntoa(serverAddress.sin_addr),buf);
65   
66 }
67 //清理套接字占用的资源
68 WSACleanup();
69 return 0;
70 }

你可能感兴趣的:(stock)