win32简单的sockeTCP协议通信

什么也不说了看代码

首先是服务端代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <WinSock2.h>
  4 #include <Windows.h>
  5 #include <process.h>
  6 #include <string.h>
  7 #define  BUFFSIZE 1024
  8 #pragma comment(lib,"ws2_32.lib")    //lib库
  9 unsigned int WINAPI ThreadProc(LPVOID lpArgs);
 10 typedef struct _tagSOCKET
 11 {
 12     SOCKET m_clientSocket;
 13     SOCKET m_ServerSocket;
 14     sockaddr_in m_ClientAddr;
 15 }TAGSOCKET,*PTAGSOCKET;
 16 HANDLE g_Thread = NULL;
 17 int main(void)
 18 {
 19     WSADATA wsData;
 20     //建立一个服务器socket
 21     SOCKET hServer;
 22     int hError;
 23     WORD wVersion = MAKEWORD(2,2);
 24     if(WSAStartup(wVersion,&wsData))//初始化
 25     {
 26         WSACleanup();
 27         return 0;
 28     }
 29     hServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);    //创建套接字
 30     hError = WSAGetLastError();
 31     if (hServer==INVALID_SOCKET)
 32     {
 33         printf("错误代码%d\r\n",hError);
 34         if(SOCKET_ERROR != closesocket(hServer))    //关闭套接字
 35             WSACleanup();    //终止使用流式网络连接
 36         return 0;
 37     }
 38     //创建服务监听的地址端口
 39     sockaddr_in addrServer;
 40     addrServer.sin_family = AF_INET;
 41     addrServer.sin_port = htons(8888);
 42     addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);    //INADDR_ANY     意思是接收当前机器上任何配置的本地ip连接
 43     //绑定与相关联的套接字本地地址
 44     if(SOCKET_ERROR == bind(hServer,(sockaddr*)&addrServer,sizeof(addrServer)))
 45     {
 46         printf("绑定失败\r\n");
 47         if(SOCKET_ERROR != closesocket(hServer))    //关闭套接字
 48             WSACleanup();    //终止使用流式网络连接
 49         return 0;
 50     }
 51     //在socket上监听
 52     if(SOCKET_ERROR==listen(hServer,SOMAXCONN))
 53     {
 54         printf("监听失败\r\n");
 55         if(SOCKET_ERROR != closesocket(hServer))    //关闭套接字
 56             WSACleanup();    //终止使用流式网络连接
 57         return 0;
 58     }
 59     //接收来自客户端的连接请求信息
 60     SOCKET cLientSocket;
 61     sockaddr_in clientAddr;
 62     int nLent = sizeof clientAddr;
 63     cLientSocket = accept(hServer,(sockaddr*)&clientAddr,&nLent);
 64     if(cLientSocket== SOCKET_ERROR)
 65     {
 66         printf("错误的客户socket连接\r\n");
 67         if (SOCKET_ERROR !=closesocket(hServer))
 68         {
 69             WSACleanup();
 70             return 0;
 71         }
 72     }
 73     TAGSOCKET uSerdata;
 74     ZeroMemory(&uSerdata,sizeof(TAGSOCKET));
 75     uSerdata.m_clientSocket = cLientSocket;
 76     uSerdata.m_ServerSocket = hServer;
 77     uSerdata.m_ClientAddr = clientAddr;
 78     //循环接收客户端数据
 79     g_Thread = (HANDLE)_beginthreadex(NULL,NULL,ThreadProc,(LPVOID)&uSerdata,NULL,NULL);
 80     WaitForSingleObject(g_Thread,INFINITE);
 81     CloseHandle(g_Thread);
 82     /*in_addr addr;
 83     addr.S_un.S_addr = inet_addr("192.168.1.1");    //将字符串ip转换成in_addr
 84     //htonl() 主机转TCP 该 htonl函数可用于在主机字节顺序IPv4地址转换为网络字节顺序的IPv4地址。
 85     //该功能不会检查hostlong参数是否是有效的IPv4地址。
 86     // htons() 
 87     //inet_ntoa函数 将in_addr类型转换成字符串格式
 88     //htons()    该 htons函数可用于将IP端口号主机字节顺序转换成网络字节顺序的IP端口号
 89     */
 90 
 91     system("pause");
 92 
 93     return 0;
 94 }
 95 unsigned int WINAPI ThreadProc(LPVOID lpArgs)
 96 {
 97     //接收来自客户发送的数据
 98     PTAGSOCKET tempSocket = (PTAGSOCKET)lpArgs;
 99     if(tempSocket->m_clientSocket == SOCKET_ERROR || tempSocket->m_ServerSocket == SOCKET_ERROR)
100     {
101         closesocket(tempSocket->m_clientSocket);
102         closesocket(tempSocket->m_ServerSocket);
103         WSACleanup();
104         return 0;
105     }
106     char szbuf[BUFFSIZE];
107     int nRecverror = SOCKET_ERROR;
108     while(TRUE)
109     {
110         ZeroMemory(szbuf,BUFFSIZE);
111         nRecverror = recv(tempSocket->m_clientSocket,szbuf,BUFFSIZE,NULL);
112         if(nRecverror==SOCKET_ERROR)
113         {
114             closesocket(tempSocket->m_clientSocket);
115             closesocket(tempSocket->m_ServerSocket);
116             WSACleanup();
117             break;
118         }
119         char sprintfbuf[BUFFSIZE];
120         ZeroMemory(sprintfbuf,BUFFSIZE);
121         sprintf_s(sprintfbuf,"Ip:%s:\t数据:%s\r\n",inet_ntoa(tempSocket->m_ClientAddr.sin_addr),szbuf);
122         printf("%s\r\n",sprintfbuf);
123         if (!_stricmp(szbuf,"close"))
124         {
125             nRecverror = send(tempSocket->m_clientSocket,"close",strlen("close")+1,NULL);
126             if (nRecverror== SOCKET_ERROR)
127             {
128                 closesocket(tempSocket->m_clientSocket);
129                 closesocket(tempSocket->m_ServerSocket);
130                 WSACleanup();
131                 break;
132             }
133             break;
134         }else
135         {
136             sprintf_s(sprintfbuf,"已经接收到您发送的信息:%s\r\n",szbuf);
137             nRecverror = send(tempSocket->m_clientSocket,sprintfbuf,strlen(sprintfbuf),NULL);
138             if (nRecverror== SOCKET_ERROR)
139             {
140                 closesocket(tempSocket->m_clientSocket);
141                 closesocket(tempSocket->m_ServerSocket);
142                 WSACleanup();
143                 break;
144             }
145         }
146     }
147     printf("程序即将退出\r\n");
148     closesocket(tempSocket->m_clientSocket);
149     closesocket(tempSocket->m_ServerSocket);
150     WSACleanup();
151     return 0;
152 }

下面是客户端代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <WinSock2.h>
 4 #include <Windows.h>
 5 #pragma comment(lib,"ws2_32.lib")
 6 #define BUFFSIZE 1024
 7 int main(void)
 8 {
 9     WSADATA wSdata;
10 //    char hostName[256]={0};
11     SOCKET hClientSocket;
12     WORD wVerison = MAKEWORD(2,2);
13     sockaddr_in sockeaddr;
14 //    PHOSTENT hoStent;
15     if(WSAStartup(wVerison,&wSdata))
16     {
17         WSACleanup();
18         return 0;
19     }
20      hClientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
21      if(hClientSocket==INVALID_SOCKET)
22      {
23          WSACleanup();
24          return 0;
25      }
26     /* gethostname(hostName,256);
27      hoStent = gethostbyname(hostName);
28      if(!hoStent)
29      {
30          closesocket(hClientSocket);
31          WSACleanup();
32          return 0;
33      }*/
34      ZeroMemory(&sockeaddr,sizeof(sockaddr_in));
35      sockeaddr.sin_family = AF_INET;
36     // printf("%s\r\n",inet_ntoa(*(in_addr*)hoStent->h_addr));
37      sockeaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
38      sockeaddr.sin_port = htons(8888);
39      //然后链接远程服务器
40      if(SOCKET_ERROR == connect(hClientSocket,(sockaddr*)&sockeaddr,sizeof(sockaddr_in)))
41      {
42          closesocket(hClientSocket);
43          WSACleanup();
44          return 0;
45      }
46      char szbuf[BUFFSIZE];
47      int Nerror = SOCKET_ERROR;
48      while(TRUE)
49      {
50          ZeroMemory(szbuf,BUFFSIZE);
51          char sztemp[BUFFSIZE]={0};
52          ZeroMemory(sztemp,BUFFSIZE);
53          printf("请输入你要发送的内容:\r\n");
54          scanf("%s",sztemp);
55          Nerror = send(hClientSocket,sztemp,strlen(sztemp)+1,0);
56          if (Nerror==SOCKET_ERROR)
57          {
58              break;
59          }
60          Nerror = recv(hClientSocket,szbuf,BUFFSIZE,0);
61          if (Nerror==SOCKET_ERROR)
62          {
63              break;
64          }else
65          {
66              printf("接收到的数据:%s\r\n",szbuf);
67          }
68          if (!_stricmp(szbuf,"close"))
69          {
70              printf("即将关闭程序\r\n");
71              break;
72          }
73      }
74      closesocket(hClientSocket);
75      WSACleanup();
76     system("pause");
77     return 0;
78 }

 最后的效果就是这样

win32 SocketTCP效果图

 

posted @ 2017-08-08 00:28  轻风々  阅读(376)  评论(0编辑  收藏  举报