1. Related to Class
Ace_inet_addr//ace Network Address
Ace_sock_dgram//ace message
2. Introduction
UDP communication does not need to connect and close the connection as TCP, TCP programming requires the connection through accept and connect, and UDP communication omits this step, relatively speaking, programming is simpler.
Because the UDP communication does not establish a connection, the server side can not be like TCP communication in the establishment of the connection when the client's address information, so the server can not actively send information to the client (do not know the address of the client), only wait until the client sends the UDP information to determine the client address information, for communication.
The UDP communication process is as follows:
The server-side binds a fixed UDP port, waiting for the communication of the receiving client.
The client sends a message directly to the server through the server's IP and address information.
The server receives the client's IP and port information after receiving the message sent by the client, and communicates with the client through that address information.
3. Sample Code
1#include <iostream>2#include"ace/sock_dgram.h"3 using namespacestd;4 5 Const intServer_port = the ;6 intMainintargcChar*argv[])7 {8 Charbuffer[1024x768];9ssize_t bc=0;//number of bytes receivedTenAce_sock_dgram peer;//Sock_io, and client-side data Paths OneAce_time_value Timeout ( -,0);//TCP Accept time-out AAce_inet_addr remoteaddr;//The remote address that is connected - - //socket creation, binding, monitoring the ace_inet_addr Addr (server_port); - if(Peer.open (addr)! =0)//Binding Port - { -cout<<"Bind Port fail!"<<Endl; + return-1; - } +cout<<"server ready."<<Endl; A at while(true) - { - while(true) - { - //receive data (timeout or break on end) - if(Bc=peer.recv (Buffer,1024x768, Remoteaddr,0, &timeout)) <=0) in { - Break; to } +buffer[bc]=' /'; -cout<<"[Server rev]:>"<<buffer<<Endl; the if(strcmp (Buffer,"quit") ==0) * { $ Break; Panax Notoginseng } - the //Send Data + Do A { thecout<<"[Server cin]:>"; +Cin.getline (Buffer,1024x768); -} while(strlen (buffer) <=0); $Peer.send (buffer, strlen (buffer), REMOTEADDR,0,&timeout); $ } - } -Peer.close ();//the UDP server is not available the - return 0; Wuyi}
Server.cpp
1#include <ace/SOCK_Dgram.h>2 3#include <string>4#include <iostream>5 using namespacestd;6 7 Const intServer_port = the ;8 9 intMainintargcChar*argv[])Ten { One Charbuffer[1024x768]; Assize_t bc=0;//number of bytes received - -Ace_inet_addr remoteaddr (Server_port,"192.168.237.128"); the ace_inet_addr Addr; -Ace_time_value Timeout ( -,0); - Ace_sock_dgram Peer (addr); - +cout<<"Ready !"<<Endl; - + while(true) A { at //Send Data - Do - { -cout<<"[Client cin]:>"; -Cin.getline (Buffer,1024x768); -} while(strlen (buffer) <=0); inPeer.send (buffer, strlen (buffer), REMOTEADDR,0,&timeout); - to //receive data (timeout or break on end) + if(Bc=peer.recv (Buffer,1024x768, Remoteaddr,0, &timeout)) <=0) - { the Break; * } $buffer[bc]=' /';Panax Notoginsengcout<<"[Client rev]:>"<<buffer<<Endl; - if(strcmp (Buffer,"quit") ==0) the { + Break; A } the } + peer.close (); - $ return 0; $}
client.cpp
Ok!
ACE_LINUX:UDP Communication