为了更容易理解,我们举一个小例子来说明服务器与客户端之间的连接过程。
有一个饭店,饭店里有服务员,服务员用于招待客人
特别要注意的是:要记住相关函数的各个参数都是什么,什么时候返回SOCKET_ERROR,什么时候返回INVALID_SOCKET
服务器
1 #include<stdio.h>
2 #include<winsock2.h>
3 #pragma comment(lib,"ws2_32.lib") 5 int main()
6 {
7 //初始化wsa
8 WORD wVersionRequested;
9 WSADATA wsaData;
10 int err;
11 wVersionRequested = MAKEWORD(2, 2);
12
13 err = WSAStartup(wVersionRequested, &wsaData);
14 if (err != 0)
15 {
16
17 printf("WSAStartup failed with error: %d\n", err);
18 return 1;
19 }
20
21 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
22 {
23
24 printf("Could not find a usable version of Winsock.dll\n");
25 WSACleanup();
26 return 1;
27 }
28 else
29 printf("The Winsock 2.2 dll was found okay\n");
30
31 SOCKET sever;
32 SOCKET waiter;//服务员用于招待客人
33 struct sockaddr_in severaddr;
34 struct sockaddr_in clientaddr;37 //创建套接字---创建饭店
38 sever=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
39 if(sever==INVALID_SOCKET)
40 {
41 WSACleanup();
42 return 1;
43 }
44 /*if(sever=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==INVALID_SOCKET)
45 {
46 WSACleanup();
47 return 1;
48 }*///这么写会导致绑定失败??
49 //绑定---相当于拉客,把客人拉进屋子
50 severaddr.sin_family=AF_INET;
51 severaddr.sin_port=htons(1234);
52 severaddr.sin_addr.S_un.S_addr=inet_addr("192.168.202.1");
53
54 if(bind(sever,(const sockaddr*)&severaddr,sizeof(severaddr))==SOCKET_ERROR)
55 {
56 WSACleanup();
57 closesocket(sever);
58 return 1;
59 } //监听----看客人有没有要点餐的
60 if(listen(sever,4)==SOCKET_ERROR)
61 {
62 WSACleanup();
63 return 1;
64 }
65 printf("等待连接...\n");
66 int clientlen=sizeof(clientaddr); //接收
67 waiter=accept(sever,(SOCKADDR*)&clientaddr,&clientlen);//如果有要点餐的,派一个服务员去,将客户的地址信息传给店长
68 if(waiter==INVALID_SOCKET)//第一个参数为监听套接字
69 {
70 printf("接受失败\n");
71 WSACleanup();
72 closesocket(sever);
73 return 1;
74 }
75 while(1)
76 {
77 char recvbuf[1024]={0};//接受缓冲区
78 char sendbuf[1024]={0};//发送缓冲区
79 if(recv(waiter,recvbuf,sizeof(recvbuf),0)==SOCKET_ERROR)//服务员去负责收客户信息
80 { //waiter去收
81 printf("接受数据失败\n");
82 }
83 printf("%s\n",recvbuf);
84 scanf("%s",sendbuf);
85 send(waiter,(char *)sendbuf,sizeof(sendbuf),0);//waiter去send
86 }
87 WSACleanup();
88 closesocket(sever);
89 closesocket(waiter);
90 return 0;
91 }
客户端
1 #include<iostream>
2 #include <winsock2.h>
3
4 using namespace std;
5 #pragma comment(lib, "ws2_32.lib")
6
7 int main()
8 {
9 WORD wVersionRequested;
10 WSADATA wsaData;
11 int err;
12 wVersionRequested = MAKEWORD(2, 2);
13
14 err = WSAStartup(wVersionRequested, &wsaData);
15 if (err != 0)
16 {
17
18 printf("WSAStartup failed with error: %d\n", err);
19 return 1;
20 }
21
22 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
23 {
24
25 printf("Could not find a usable version of Winsock.dll\n");
26 WSACleanup();
27 return 1;
28 }
29 else
30 printf("The Winsock 2.2 dll was found okay\n");
31
32
33 //创建socket
34 SOCKET sockClient=socket(AF_INET,SOCK_STREAM ,IPPROTO_TCP );
35 if(INVALID_SOCKET ==sockClient)
36 {
37 WSACleanup();
38 return 1;
39 }
40 //连接服务器
41 sockaddr_in severaddr;
42 severaddr.sin_family=AF_INET;
43 severaddr.sin_port=htons(1234);
44 severaddr.sin_addr.S_un.S_addr=inet_addr("192.168.202.1");
45 if(connect(sockClient,(const sockaddr*)&severaddr,sizeof(severaddr))==SOCKET_ERROR)
46 {//客户端去连,连接服务器地址
47 WSACleanup();
48 closesocket(sockClient);
49 return 1;
50 }
51 while(1)
52 {
53 char szbuf[1024]={0};
54 cin>>szbuf;
55 send(sockClient,szbuf,sizeof(szbuf),0);//客户端发送
56 int nReNum=recv(sockClient,szbuf,sizeof(szbuf),0);//客户端接收
57 if( nReNum>0)
58 {
59 cout<<szbuf<<endl;
60 }
61 }
62 WSACleanup();
63 closesocket(sockClient);
64 return 0;
65 }