服务器端:

  1 #pragma comment(lib, "ws2_32.lib")
  2 #include <iostream>
  3 #include <Winsock2.h>
  4 using namespace std;
  5 SOCKET sockServer;
  6 sockaddr_in addrServer;
  7 sockaddr_in addrSend;
  8 #define  BUF_SIZE 1024
  9 #define  IP_ADDR "192.168.59.69"
 10 #define  PORT 5050
 11 
 12 void InitSocket()
 13 {
 14     WSADATA wsaData;
 15     int erro;
 16     erro = WSAStartup(MAKEWORD(2, 2), &wsaData);
 17     if (erro != 0)
 18     {
 19         cout << "加载套接字库失败!" << endl;
 20         return;
 21     }
 22     sockServer = socket(AF_INET, SOCK_DGRAM, 0);
 23     if(sockServer == INVALID_SOCKET)
 24     {
 25         cout << "创建套接字失败!" << endl;
 26         WSACleanup();
 27         return;
 28     }
 29 
 30     addrServer.sin_family = AF_INET;
 31     addrServer.sin_port = htons(PORT);
 32     addrServer.sin_addr.s_addr = htonl(INADDR_ANY);
 33 
 34     addrSend.sin_family = AF_INET;
 35     addrSend.sin_port = htons(PORT+1);
 36     addrSend.sin_addr.s_addr = inet_addr(IP_ADDR);
 37 
 38     erro = bind(sockServer, (sockaddr*)&addrServer, sizeof(sockaddr));
 39     if (erro == SOCKET_ERROR)
 40     {
 41         cout << "绑定失败!" << endl;
 42         closesocket(sockServer);
 43         WSACleanup();
 44         return; 
 45     }
 46 }
 47 
 48 DWORD WINAPI RecvThread(LPVOID lParam)
 49 {
 50     char buf[1024];
 51     int erro;
 52     int len = sizeof(addrServer);
 53     while(1)
 54     {
 55         ZeroMemory(buf, sizeof(buf));
 56         erro = recvfrom(sockServer, buf, BUF_SIZE, 0, (sockaddr*)&addrSend, &len);  
 57         if (0 == erro)
 58         {
 59             cout <<"接收失败!"<< endl;
 60             return 1;
 61         }
 62         char * ipaddr = inet_ntoa(addrSend.sin_addr);
 63         if (strcmp(buf, "") != 0)
 64         {
 65             cout << "客户端"<< "(" << ipaddr<< "):" << buf<<endl;
 66         }
 67 
 68         Sleep(50);
 69     }
 70 
 71     return 1;
 72 }
 73 DWORD WINAPI SendThread(LPVOID lParam)
 74 {
 75     char buf[1024];
 76     int erro;
 77     int len = sizeof(addrServer);
 78     while(1)
 79     {
 80         ZeroMemory(buf, sizeof(buf));
 81         cin.getline(buf, 1024);
 82         erro = sendto(sockServer, buf, sizeof(buf), 0, (sockaddr*)&addrSend, len);  
 83         if (0 == erro)
 84         {
 85             cout<< "发送失败!"<<endl;
 86             return 1;
 87         }
 88         Sleep(50);
 89     }
 90     return 1;
 91 }
 92 int main()
 93 {
 94     InitSocket();
 95     CreateThread(NULL, NULL, RecvThread, NULL, NULL, NULL);
 96     CreateThread(NULL, NULL, SendThread, NULL, NULL, NULL);
 97     while(1)
 98     {
 99         Sleep(50);
100     }
101     return 1;
102 }

 

客户端:

  1 #pragma comment(lib, "ws2_32.lib")
  2 #include <iostream>
  3 #include <Winsock2.h>
  4 using namespace std;
  5 SOCKET sockClient;
  6 sockaddr_in addrClient;
  7 sockaddr_in addrSend;
  8 #define  BUF_SIZE 1024  //数据缓冲区
  9 #define  IP_ADDR "192.168.59.69"  //IP地址
 10 #define  PORT 5050
 11 void InitSocket()
 12 {
 13     WSADATA wsaData;
 14     int erro;
 15     erro = WSAStartup(MAKEWORD(2, 2), &wsaData);
 16     if (erro != 0)
 17     {
 18         cout << "加载套接字库失败!" << endl;
 19         return;
 20     }
 21     sockClient = socket(AF_INET, SOCK_DGRAM, 0);
 22     if(sockClient == INVALID_SOCKET)
 23     {
 24         cout << "创建套接字失败!" << endl;
 25         WSACleanup();
 26         return;
 27     }
 28 
 29     addrClient.sin_family = AF_INET;//服务器端
 30     addrClient.sin_port = htons(PORT);
 31     addrClient.sin_addr.s_addr = inet_addr(IP_ADDR);
 32 
 33     addrSend.sin_family = AF_INET;//客户端
 34     addrSend.sin_port = htons(PORT+1);
 35     addrSend.sin_addr.s_addr = inet_addr(IP_ADDR);
 36 
 37     erro = bind(sockClient, (sockaddr*)&addrSend, sizeof(sockaddr));
 38     if (erro == SOCKET_ERROR)
 39     {
 40         cout << "绑定失败!" << endl;
 41         closesocket(sockClient);
 42         WSACleanup();
 43         return; 
 44     }
 45 
 46 
 47 }
 48 
 49 DWORD WINAPI RecvThread(LPVOID lParam)//接收函数
 50 {
 51     char buf[1024];
 52     int erro;
 53     int len = sizeof(addrClient);
 54     while(1)
 55     {
 56         ZeroMemory(buf, sizeof(buf));
 57         erro = recvfrom(sockClient, buf, BUF_SIZE, 0, (sockaddr*)&addrClient, &len);  
 58         if (erro == 0)
 59         {
 60             cout <<"接收失败!"<< endl;
 61             return 1;
 62         }
 63         char * ipaddr = inet_ntoa(addrSend.sin_addr);
 64         if (strcmp(buf, "") != 0)
 65         {
 66             cout << "服务器端" << "(" << ipaddr<< "):" << buf <<endl;
 67         }
 68         Sleep(50);
 69     }
 70 
 71     return 1;
 72 }
 73 DWORD WINAPI SendThread(LPVOID lParam)
 74 {
 75     char buf[1024];
 76     int erro;
 77     int len = sizeof(addrClient);
 78     while(1)
 79     {
 80         ZeroMemory(buf, sizeof(buf));
 81         cin.getline(buf, 1024);
 82         erro = sendto(sockClient, buf, sizeof(buf), 0, (sockaddr*)&addrClient, len);  
 83         if (0 == erro)
 84         {
 85             cout<< "发送失败!"<<endl;
 86             return 1;
 87         }
 88         Sleep(50);
 89     }
 90     return 1;
 91 }
 92 int main()
 93 {
 94     InitSocket();
 95     CreateThread(NULL, NULL, SendThread, NULL, NULL, NULL);
 96     CreateThread(NULL, NULL, RecvThread, NULL, NULL, NULL);
 97     while(1)
 98     {
 99         Sleep(50);
100     }
101     return 1;
102 }