码迷,mamicode.com
首页 > Windows程序 > 详细

winSocket数据传输

时间:2016-10-11 14:15:18      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

服务器端:

 1 #include <WINSOCK2.H>
 2 #include <stdio.h>
 3 #pragma comment(lib,"ws2_32.lib")
 4 int main(int argc,char **argv)
 5 {
 6     //创建套接字
 7     WORD myVersionRequest;
 8     WSADATA wsaData;
 9     myVersionRequest=MAKEWORD(1,1);
10     int err;
11     err=WSAStartup(myVersionRequest,&wsaData);
12     if (!err)
13     {
14         printf("已打开套接字,等待连接!\n");
15     }
16     else
17     {
18         printf("ERROR:嵌套字未打开!");
19         return 1;
20     }
21     //进一步绑定套接字
22     SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);//创建了可识别套接字
23 
24     //需要绑定的参数
25     SOCKADDR_IN addr;
26     addr.sin_family=AF_INET;
27     addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//ip地址
28     addr.sin_port=htons(6000);//绑定端口
29 
30     bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成
31     listen(serSocket,5);//其中第二个参数代表能够接收的最多的连接数
32 
33     //////////////////////////////////////////////////////////////////////////
34     //开始进行监听
35     //////////////////////////////////////////////////////////////////////////
36     SOCKADDR_IN clientsocket;
37     int len=sizeof(SOCKADDR);
38     while (1)
39     {
40         SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听
41         char sendBuf[100];
42 
43         sprintf(sendBuf,"hello, %s !",inet_ntoa(clientsocket.sin_addr));//找对对应的IP并且将这行字打印到那里
44         printf("Send:%s\n",sendBuf);
45         send(serConn,sendBuf,strlen(sendBuf)+1,0);
46 
47         char receiveBuf[100];//接收
48         recv(serConn,receiveBuf,sizeof(receiveBuf),0);
49         printf("recv:%s\n",receiveBuf);
50 
51         closesocket(serConn);//关闭
52         WSACleanup();//释放资源的操作
53         return 0;
54     }
55     return 1;
56 }

客户机端:

 1 #include <WINSOCK2.H>
 2 #include <stdio.h>
 3 #pragma comment(lib,"ws2_32.lib")
 4 int main(int argc,char **argv)
 5 {
 6     int err;
 7     WORD versionRequired;
 8     WSADATA wsaData;
 9     versionRequired=MAKEWORD(1,1);
10     err=WSAStartup(versionRequired,&wsaData);//协议库的版本信息
11     if (!err)    {
12         printf("客户端嵌套字已经打开!\n");
13     } else {
14         printf("ERROR:客户端的嵌套字打开失败!\n");
15         return 1;//结束
16     }
17     SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0);
18 
19     SOCKADDR_IN clientsock_in;
20     clientsock_in.sin_addr.S_un.S_addr=inet_addr("192.168.1.3");
21     clientsock_in.sin_family=AF_INET;
22     clientsock_in.sin_port=htons(6000);
23 
24     //bind(clientSocket,(SOCKADDR*)&clientsock_in,strlen(SOCKADDR));//注意第三个参数
25     //listen(clientSocket,5);
26     connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//开始连接
27     char receiveBuf[100];
28     if(1) {
29 
30         printf("Send:%s\n","hello,this is client");
31         send(clientSocket,"hello,this is client",strlen("hello,this is client")+1,0);
32 
33         recv(clientSocket,receiveBuf,101,0);
34         printf("Recv:%s\n",receiveBuf);
35 
36         closesocket(clientSocket);
37         WSACleanup();
38     }
39     return 0;
40 }

 

winSocket数据传输

标签:

原文地址:http://www.cnblogs.com/A--Q/p/5948884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!