UDP Client Server Example

UDP Client-Server   TCP Client-Server  

Description

The sample program depicts the communication between a client and server using UDP based sockets. Server starts first creates and binds socket waits for data to come in on the RECEIVER_PORT_NUM. Client creates socket binds to SENDER_PORT_NUM and sends data to Server.

UDP Client Server Socket Example


Server
#include <lwip/sockets.h>

#define RECEIVER_PORT_NUM 6001
#define RECEIVER_IP_ADDR "192.136.23.21"

void main(void)
{

int socket_fd;
struct sockaddr_in sa,ra;

int recv_data; char data_buffer[80]; /* Creates an UDP socket (SOCK_DGRAM) with Internet Protocol Family (PF_INET).
 * Protocol family and Address family related. For example PF_INET Protocol Family and AF_INET family are coupled.
*/

socket_fd = socket(PF_INET, SOCK_DGRAM, 0);

if ( socket_fd < 0 )
{

printf("socket call failed");
exit(0);
}

memset(&sa, 0, sizeof(struct sockaddr_in));
ra.sin_family = AF_INET;
ra.sin_addr.s_addr = inet_addr(RECEIVER_IP_ADDR);
ra.sin_port = htons(RECEIVER_PORT_NUM);


/* Bind the UDP socket to the port RECEIVER_PORT_NUM and to the current
* machines IP address (Its defined by RECEIVER_PORT_NUM).
* Once bind is successful for UDP sockets application can operate
* on the socket descriptor for sending or receiving data.
*/
if (bind(socket_fd, (struct sockaddr *)&ra, sizeof(struct sockaddr_in)) == -1)
{

printf("Bind to Port Number %d ,IP address %s failed\n",RECEIVER_PORT_NUM,RECEIVER_IP_ADDR);
close(socket_fd);
exit(1);
}
/* RECEIVER_PORT_NUM is port on which Server waits for data to
* come in. It copies the received data into receive buffer and
* prints the received data as string. If no data is available it
* blocks.recv calls typically return any availbale data on the socket instead of waiting for the entire data to come.
*/

recv_data = recv(socket_fd,data_buffer,sizeof(data_buffer),0);
if(recv_data > 0)
{

data_buffer[recv_data] = '\0';
printf("%s\n",data_buffer);
}
close(socket_fd);
}

UDP Client Server Example


Client
#include <lwip/sockets.h>

#define SENDER_PORT_NUM 6000
#define SENDER_IP_ADDR "192.136.23.20"
#define RECEIVER_PORT_NUM 6001
#define RECEIVER_IP_ADDR "192.136.23.21"

void main(void)
{

int socket_fd;
struct sockaddr_in sa;

int sent_data; char recv_data_buffer[80]; /* Creates an TCP socket (SOCK_STREAM) with Internet Protocol Family (PF_INET).
 * Protocol family and Address family related. For example PF_INET Protocol Family and AF_INET family are coupled.
*/

socket_fd = socket(PF_INET, SOCK_STREAM, 0);

if ( socket_fd < 0 )
{

printf("socket call failed");
exit(0);
}

memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(SENDER_IP_ADDR);
sa.sin_port = htons(SENDER_PORT_NUM);


/* Bind the TCP socket to the port SENDER_PORT_NUM and to the current
* machines IP address (Its defined by SENDER_IP_ADDR).
* Once bind is successful for UDP sockets application can operate
* on the socket descriptor for sending or receiving data.
*/
if (bind(socket_fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == -1)
{
printf("Bind to Port Number %d ,IP address %s failed\n",SENDER_PORT_NUM,SENDER_IP_ADDR);
close(socket_fd);
exit(1);
}

memset(&ra, 0, sizeof(struct sockaddr_in));
ra.sin_family = AF_INET;
ra.sin_addr.s_addr = inet_addr(RECEIVER_IP_ADDR);
ra.sin_port = htons(RECEIVER_PORT_NUM);


strcpy(data_buffer,"Hello World");
sent_data = send(socket_fd, data_buffer,sizeof("Hello World",0);
if(sent_data < 0)
{
printf("send failed\n");
close(socket_fd);
exit(2);
}
close(socket_fd);
}