UDP Server and Client application
UDPServer :
--------------
1.WSAStartup()
2.socket()
3.bind()
4.recv()
5.send()
UDPClient :
------------
1.WSAStartup()
2.gethostbyaddr()
3.socket()
4.connect()
5.send()
6.receive()
UDPServer:
----------
struct sockaddr_in local, from;
WSADATA wsaData;
SOCKET listen_socket, msgsock;
WSAStartup(0x202, &wsaData)
local.sin_family = AF_INET;
local.sin_addr.s_addr = (!ip_address) ? INADDR_ANY:inet_addr(ip_address);
local.sin_port = htons(port);
listen_socket = socket(AF_INET, SOCK_DGRAM,0);
if (listen_socket == INVALID_SOCKET){ //Error}
if (bind(listen_socket, (struct sockaddr*)&local, sizeof(local)) == SOCKET_ERROR){ //error}
msgsock = listen_socket;
retval = recvfrom(msgsock,Buffer, sizeof(Buffer), 0, (struct sockaddr *)&from, &fromlen);
if(retval == SOCKET_ERROR) {return;}
//send the received data back to the client...
retval = sendto(msgsock, Buffer, sizeof(Buffer), 0, (struct sockaddr *)&from, fromlen);
UDP client :
---------------
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
SOCKET conn_socket;
if (isalpha(server_name[0]))
{ // server address is a name
hp = gethostbyname(server_name);
}
else
{ // Convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { //Error}
retval = send(conn_socket, Buffer, sizeof(Buffer), 0);
retval = recv(conn_socket, Buffer, sizeof(Buffer), 0);
closesocket(conn_socket);
WSACleanup();
--------------
1.WSAStartup()
2.socket()
3.bind()
4.recv()
5.send()
UDPClient :
------------
1.WSAStartup()
2.gethostbyaddr()
3.socket()
4.connect()
5.send()
6.receive()
UDPServer:
----------
struct sockaddr_in local, from;
WSADATA wsaData;
SOCKET listen_socket, msgsock;
WSAStartup(0x202, &wsaData)
local.sin_family = AF_INET;
local.sin_addr.s_addr = (!ip_address) ? INADDR_ANY:inet_addr(ip_address);
local.sin_port = htons(port);
listen_socket = socket(AF_INET, SOCK_DGRAM,0);
if (listen_socket == INVALID_SOCKET){ //Error}
if (bind(listen_socket, (struct sockaddr*)&local, sizeof(local)) == SOCKET_ERROR){ //error}
msgsock = listen_socket;
retval = recvfrom(msgsock,Buffer, sizeof(Buffer), 0, (struct sockaddr *)&from, &fromlen);
if(retval == SOCKET_ERROR) {return;}
//send the received data back to the client...
retval = sendto(msgsock, Buffer, sizeof(Buffer), 0, (struct sockaddr *)&from, fromlen);
UDP client :
---------------
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
SOCKET conn_socket;
if (isalpha(server_name[0]))
{ // server address is a name
hp = gethostbyname(server_name);
}
else
{ // Convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { //Error}
retval = send(conn_socket, Buffer, sizeof(Buffer), 0);
retval = recv(conn_socket, Buffer, sizeof(Buffer), 0);
closesocket(conn_socket);
WSACleanup();
0 Comments:
Post a Comment
<< Home