Recently Learning Network programming, learned the principle of UDP communication. Write a UDP-based server and client, can realize the direct LAN communication, can achieve a service side, for multiple clients, tested over two clients, two clients can receive the service side of the message, two clients of the message server can also receive, two clients cannot communicate. In terms of reception, there may be reception confusion, as is the receipt of a sentence, send a sentence.
The following is the server-side code, which is implemented on the console.
#include <iostream> #include <winsock2.h>using namespace std; #pragma comment (lib, "Ws2_32.lib")//connection Library int Main () {//create socket, equivalent to handle word Version; Wsadata Wdata; Version = Makeword (2, 2), int n = WSAStartup (Version, &wdata), if (n! = 0)//check for errors, but generally not error {cout << "error!" <& Lt endl;return-1;} Socket SOC = socket (af_inet, SOCK_DGRAM, 0), if (Invalid_socket = = Soc) {cout << "error!" << endl;return-1;} Create the port sockaddr_in addser;addser.sin_addr. S_un. S_ADDR = htonl (inaddr_any); addser.sin_family = Af_inet;addser.sin_port = Htons (8008); Ports, the valid range of port numbers is from 0 to 65535. n = Bind (Soc, (sockaddr*) &addser, sizeof (Addser)); In general, a port number greater than 49151 represents a dynamic port. Bind socket if (n = = socket_error)//error check, error, possibly port occupied, need to change {cout << "error!" << endl;return-1;} Char send[256]; Send string char rev[256]; Receive string sockaddr_in addclient; The receiver's address information structure, int len = sizeof (SOCKADDR), while (1) {int i = Recvfrom (Soc, Rev, 0, (sockaddr*) &addclient, &len); Accept, block in this, until receive information rev[i] = '"if (rev[0] = = ' Q ') {cout <<" Char end! "<< endl;break;} cout << Inet_ntoa (addclient.sin_addr) << "say:" << Rev << Endl; Output sender's IP, and information cout << "Please say something:"; gets (send); SendTo (Soc, send, strlen (send), 0, (SOCKADDR *) &ADDC Lient, Len); Send Message}closesocket (SOC); Close socket WSACleanup (); return 0;}
The following is the code for the client:
#include <iostream> #include <winsock2.h>using namespace std; #pragma comment (lib, "Ws2_32.lib") int main () { Create sockets that are similar to Word Version on the server side; Wsadata Wdata; Version = Makeword (2, 2), int n = WSAStartup (Version, &wdata), if (n! = 0) {cout << "error!" << endl;return-1 ;} Socket sockclient = socket (af_inet, SOCK_DGRAM, 0), if (Invalid_socket = = sockclient) {cout << "error!" << Endl; return-1;} Sockaddr_in addser;//Different here, need the server IP, Port addser.sin_addr. S_un. S_ADDR = inet_addr ("192.168.1.100"); addser.sin_family = Af_inet;addser.sin_port = Htons (8008); char send[256]; Message sent by Char rev[256]; Received message int len = sizeof (SOCKADDR), while (1) {cout << "say something:"; get (send); SendTo (sockclient, send, St Rlen (Send), 0, (sockaddr*) &addser, Len); send int i = Recvfrom (sockclient, rev., 0, (sockaddr*) &addser, &len); End, block in this rev[i] = ' + ';//until received if (i = =-1) cout << "service side not on!" << endl;else{if (rev[0] = = ' Q ') {cout << ' Char end! "<< Endl;break;} cout << Inet_ntoa (addser.sin_addr) << "say:" << Rev << Endl; Outputs the sender's IP and message}}closesocket (sockclient); Close socket WSACleanup (); return 0;}
Test instances are not posted out, the other people on the LAN is not very good to speak! ~~~
Network programming ~c++ realizing LAN communication