C++ 简单的 Tcp 实现 socket 客户端与客户端通信

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

开发环境  Vs 2008  新建一个 win32 console  project

 

//  服务器端代码


[cpp] view plain copy print ?
  1. // Server.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "winsock2.h"  
  6. #pragma comment(lib, "ws2_32.lib")  
  7.   
  8. #include   
  9. using namespace std;  
  10.   
  11. int _tmain(int argc, char* argv[])  
  12. {  
  13.     const int BUF_SIZE = 64;  
  14.   
  15.     WSADATA         wsd;            //WSADATA变量  
  16.     SOCKET          sServer;        //服务器套接字  
  17.     SOCKET          sClient;        //客户端套接字  
  18.     SOCKADDR_IN     addrServ;;      //服务器地址  
  19.     char            buf[BUF_SIZE];  //接收数据缓冲区  
  20.     char            sendBuf[BUF_SIZE];//返回给客户端得数据  
  21.     int             retVal;         //返回值  
  22.   
  23.     //初始化套结字动态库  
  24.     if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)  
  25.     {  
  26.         cout << "WSAStartup failed!" << endl;  
  27.         return 1;  
  28.     }  
  29.       
  30.     //创建套接字  
  31.     sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);      
  32.     if(INVALID_SOCKET == sServer)  
  33.     {  
  34.         cout << "socket failed!" << endl;  
  35.         WSACleanup();//释放套接字资源;  
  36.         return  -1;  
  37.     }  
  38.       
  39.     //服务器套接字地址   
  40.     addrServ.sin_family = AF_INET;  
  41.     addrServ.sin_port = htons(4999);  
  42.     addrServ.sin_addr.s_addr = INADDR_ANY;        
  43.     //绑定套接字  
  44.     retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));  
  45.   
  46.     if(SOCKET_ERROR == retVal)  
  47.     {     
  48.         cout << "bind failed!" << endl;  
  49.         closesocket(sServer);   //关闭套接字  
  50.         WSACleanup();           //释放套接字资源;  
  51.         return -1;  
  52.     }  
  53.       
  54.     //开始监听   
  55.     retVal = listen(sServer, 1);  
  56.     if(SOCKET_ERROR == retVal)  
  57.     {  
  58.         cout << "listen failed!" << endl;         
  59.         closesocket(sServer);   //关闭套接字  
  60.         WSACleanup();           //释放套接字资源;  
  61.         return -1;  
  62.     }  
  63.       
  64.     //接受客户端请求  
  65.     sockaddr_in addrClient;  
  66.     int addrClientlen = sizeof(addrClient);  
  67.     sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen);  
  68.     if(INVALID_SOCKET == sClient)  
  69.     {  
  70.         cout << "accept failed!" << endl;         
  71.         closesocket(sServer);   //关闭套接字  
  72.         WSACleanup();           //释放套接字资源;  
  73.         return -1;  
  74.     }  
  75.   
  76.       
  77.     while(true){  
  78.         //接收客户端数据  
  79.         ZeroMemory(buf, BUF_SIZE);  
  80.         retVal = recv(sClient, buf, BUF_SIZE, 0);  
  81.         if (SOCKET_ERROR == retVal)  
  82.         {  
  83.             cout << "recv failed!" << endl;       
  84.             closesocket(sServer);   //关闭套接字  
  85.             closesocket(sClient);   //关闭套接字       
  86.             WSACleanup();           //释放套接字资源;  
  87.             return -1;  
  88.         }  
  89.         if(buf[0] == '0')  
  90.             break;  
  91.         cout << "客户端发送的数据: " << buf <
  92.           
  93.         cout << "向客户端发送数据: " ;  
  94.         cin >> sendBuf;  
  95.           
  96.         send(sClient, sendBuf, strlen(sendBuf), 0);  
  97.     }  
  98.       
  99.     //退出  
  100.     closesocket(sServer);   //关闭套接字  
  101.     closesocket(sClient);   //关闭套接字  
  102.     WSACleanup();           //释放套接字资源;  
  103.   
  104.       
  105.     return 0;  
  106. }  
// Server.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "winsock2.h"#pragma comment(lib, "ws2_32.lib")#include using namespace std;int _tmain(int argc, char* argv[]){ const int BUF_SIZE = 64; WSADATA   wsd;   //WSADATA变量 SOCKET   sServer;  //服务器套接字 SOCKET   sClient;  //客户端套接字 SOCKADDR_IN  addrServ;;  //服务器地址 char   buf[BUF_SIZE]; //接收数据缓冲区 char   sendBuf[BUF_SIZE];//返回给客户端得数据 int    retVal;   //返回值 //初始化套结字动态库 if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {  cout << "WSAStartup failed!" << endl;  return 1; }  //创建套接字 sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  if(INVALID_SOCKET == sServer) {  cout << "socket failed!" << endl;  WSACleanup();//释放套接字资源;  return  -1; }  //服务器套接字地址  addrServ.sin_family = AF_INET; addrServ.sin_port = htons(4999); addrServ.sin_addr.s_addr = INADDR_ANY;   //绑定套接字 retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN)); if(SOCKET_ERROR == retVal) {   cout << "bind failed!" << endl;  closesocket(sServer); //关闭套接字  WSACleanup();   //释放套接字资源;  return -1; }  //开始监听  retVal = listen(sServer, 1); if(SOCKET_ERROR == retVal) {  cout << "listen failed!" << endl;    closesocket(sServer); //关闭套接字  WSACleanup();   //释放套接字资源;  return -1; }  //接受客户端请求 sockaddr_in addrClient; int addrClientlen = sizeof(addrClient); sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen); if(INVALID_SOCKET == sClient) {  cout << "accept failed!" << endl;    closesocket(sServer); //关闭套接字  WSACleanup();   //释放套接字资源;  return -1; }  while(true){  //接收客户端数据  ZeroMemory(buf, BUF_SIZE);  retVal = recv(sClient, buf, BUF_SIZE, 0);  if (SOCKET_ERROR == retVal)  {   cout << "recv failed!" << endl;     closesocket(sServer); //关闭套接字   closesocket(sClient); //关闭套接字     WSACleanup();   //释放套接字资源;   return -1;  }  if(buf[0] == '0')   break;  cout << "客户端发送的数据: " << buf <> sendBuf;    send(sClient, sendBuf, strlen(sendBuf), 0); }  //退出 closesocket(sServer); //关闭套接字 closesocket(sClient); //关闭套接字 WSACleanup();   //释放套接字资源;  return 0;}

 

//  客户端代码

 

[cpp] view plain copy print ?
  1. // Client.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "winsock2.h"  
  6. #include   
  7. #pragma comment(lib, "ws2_32.lib")  
  8.   
  9. using namespace std;  
  10. BOOL    RecvLine(SOCKET s, char* buf);  //读取一行数据  
  11.   
  12. int main(int argc, char* argv[])  
  13. {  
  14.     const int BUF_SIZE = 64;  
  15.   
  16.     WSADATA         wsd;            //WSADATA变量  
  17.     SOCKET          sHost;          //服务器套接字  
  18.     SOCKADDR_IN     servAddr;       //服务器地址  
  19.     char            buf[BUF_SIZE];  //接收数据缓冲区  
  20.     char            bufRecv[BUF_SIZE];  
  21.     int             retVal;         //返回值  
  22.       
  23.     //初始化套结字动态库  
  24.     if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)  
  25.     {  
  26.         cout << "WSAStartup failed!" << endl;  
  27.         return -1;  
  28.     }  
  29.       
  30.     //创建套接字  
  31.     sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);    
  32.     if(INVALID_SOCKET == sHost)  
  33.     {  
  34.         cout << "socket failed!" << endl;  
  35.         WSACleanup();//释放套接字资源  
  36.         return  -1;  
  37.     }  
  38.   
  39.     //设置服务器地址  
  40.     servAddr.sin_family =AF_INET;  
  41.     servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  
  42.     servAddr.sin_port = htons((short)4999);  
  43.     int nServAddlen  = sizeof(servAddr);  
  44.   
  45.     //连接服务器  
  46.     retVal=connect(sHost,(LPSOCKADDR)&servAddr, sizeof(servAddr));    
  47.     if(SOCKET_ERROR == retVal)  
  48.     {  
  49.         cout << "connect failed!" << endl;    
  50.         closesocket(sHost); //关闭套接字  
  51.         WSACleanup();       //释放套接字资源  
  52.         return -1;  
  53.     }  
  54.     while(true){  
  55.         //向服务器发送数据  
  56.         ZeroMemory(buf, BUF_SIZE);  
  57.         cout << " 向服务器发送数据:  ";  
  58.         cin >> buf;  
  59.         retVal = send(sHost, buf, strlen(buf), 0);  
  60.         if (SOCKET_ERROR == retVal)  
  61.         {  
  62.             cout << "send failed!" << endl;  
  63.             closesocket(sHost); //关闭套接字  
  64.             WSACleanup();       //释放套接字资源  
  65.             return -1;  
  66.         }  
  67.         //RecvLine(sHost, bufRecv);  
  68.         recv(sHost, bufRecv,5 , 0);     // 接收服务器端的数据, 只接收5个字符  
  69.         cout << endl <<"从服务器接收数据:" << bufRecv;  
  70.   
  71.     }  
  72.     //退出  
  73.     closesocket(sHost); //关闭套接字  
  74.     WSACleanup();       //释放套接字资源  
  75.     return 0;  
  76. }  
// Client.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "winsock2.h"#include #pragma comment(lib, "ws2_32.lib")using namespace std;BOOL RecvLine(SOCKET s, char* buf); //读取一行数据int main(int argc, char* argv[]){ const int BUF_SIZE = 64; WSADATA   wsd;   //WSADATA变量 SOCKET   sHost;   //服务器套接字 SOCKADDR_IN  servAddr;  //服务器地址 char   buf[BUF_SIZE]; //接收数据缓冲区 char   bufRecv[BUF_SIZE]; int    retVal;   //返回值  //初始化套结字动态库 if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {  cout << "WSAStartup failed!" << endl;  return -1; }  //创建套接字 sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  if(INVALID_SOCKET == sHost) {  cout << "socket failed!" << endl;  WSACleanup();//释放套接字资源  return  -1; } //设置服务器地址 servAddr.sin_family =AF_INET; servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servAddr.sin_port = htons((short)4999); int nServAddlen  = sizeof(servAddr); //连接服务器 retVal=connect(sHost,(LPSOCKADDR)&servAddr, sizeof(servAddr));  if(SOCKET_ERROR == retVal) {  cout << "connect failed!" << endl;   closesocket(sHost); //关闭套接字  WSACleanup();  //释放套接字资源  return -1; } while(true){  //向服务器发送数据  ZeroMemory(buf, BUF_SIZE);  cout << " 向服务器发送数据:  ";  cin >> buf;  retVal = send(sHost, buf, strlen(buf), 0);  if (SOCKET_ERROR == retVal)  {   cout << "send failed!" << endl;   closesocket(sHost); //关闭套接字   WSACleanup();  //释放套接字资源   return -1;  }  //RecvLine(sHost, bufRecv);  recv(sHost, bufRecv,5 , 0);  // 接收服务器端的数据, 只接收5个字符  cout << endl <<"从服务器接收数据:" << bufRecv; } //退出 closesocket(sHost); //关闭套接字 WSACleanup();  //释放套接字资源 return 0;}

 

 

 

 

 

 

 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

你可能感兴趣的:(C++ 简单的 Tcp 实现 socket 客户端与客户端通信)