Friday, February 6, 2015

LINUX UDP SOCKET EXAMPLE

udp_ipc.h
/** create UDP socket and bind to specified port; 
    @param port
    @returns -1 on error otherwise fd
*/
int os_udp_sock_init(unsigned short port);
 
 
/** Send buf to destination port; 
    @param fd identifies the socket to be closed.
    @param dst_port identifies the destination port where data to be send.
    @param data identifies the data to be send.
    @param len identifies the data length.
    @returns size of data sent otherwise -1 on error
*/ 
int os_udp_send(int fd, unsigned short  dst_port, char *data, int len);
 
 
/** Send buf to destination port; 
    @param fd identifies the socket to be closed.
    @param src_port identifies the destination port where data have been received.
    @param data identifies the data to be send.
    @param len identifies the data length.
    @returns size of data sent otherwise -1 on error
*/ 
int os_udp_recv(int fd, char *data, int len, unsigned short *src_port);
 
 
/** Close UDP socket
    @param fd identifies the socket to be closed.
    @returns 0 on success or -1 on error 
*/
int os_udp_sock_exit(int fd);


udp_ipc.c
#include < stdio.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < string.h >
#include < unistd.h >
#include < netdb.h >
#include < arpa/inet.h >
 
#include "udp_ipc.h"
 
int os_udp_sock_init(unsigned short port)
{
 int sock_fd;
 socklen_t sock_len;
 struct sockaddr_in sock_addr;
 memset(&sock_addr,0,sizeof(sock_addr));
 
 sock_addr.sin_family = AF_INET; // Internet IP
 sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Any IP address
 sock_addr.sin_port = port; // server port
       sock_len = sizeof(sock_addr);
     
 // Create the UDP socket
 if ((sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  printf("Failed to create socket\n");
  return -1;
 }
  
 if (bind(sock_fd, (struct sockaddr *)&sock_addr, sock_len) < 0) {
  printf("Failed to bind server socket on port %d\n",port);
  return -1;
 }    
 
 return sock_fd;
}
 
 
int os_udp_sock_exit(int fd)
{
 if(close(fd) < 0){
  printf("ERROR closing\n");
  return -1;
 }
 return 0;
}
 
int os_udp_send(int fd, unsigned short  dst_port, char *data, int len)
{
 struct sockaddr_in sock_addr;
 int length = 0;
 memset(&sock_addr,0,sizeof(sock_addr));
 
 sock_addr.sin_family = AF_INET; // Internet/IP
 sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Any IP address
 sock_addr.sin_port = dst_port;// server port
 
 //send over UDP port
 if ((length = sendto(fd, data, len, 0, (struct sockaddr *) &sock_addr, 
                         sizeof(sock_addr))) < 0)  {
            printf("Failed to send message\n");
            return -1;
        }
 return length;
 
}
 
int os_udp_recv(int fd, char *data, int len, unsigned short *src_port)
{
 struct sockaddr_in sock_dst_addr;
 int length=0;
 socklen_t addr_len = sizeof(sock_dst_addr);
 
  //receive over UDP port
  if ((length = recvfrom(fd, data, len,0,(struct sockaddr *)&sock_dst_addr,
                             &addr_len)) < 0)  {
            printf("Failed to receive message\n");
            return -1;
        }
 
  *src_port = sock_dst_addr.sin_port;
  return length;
}


client.c 
#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < string.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < netdb.h > 
#include "udp_ipc.h"
 
#if 0
void error(const char *msg)
{
    perror(msg);
    exit(0);
}
#endif
 
int main(int argc, char *argv[])
{
 
     int fd = os_udp_sock_init(9931);
 
     int len = 256;
     int res;
     char buf[len];
     int count = 0;
     while(1){
      sleep(5);
      count++;
      sprintf(buf, "Client Request %d",count);
           res = os_udp_send(fd,9930,buf,len);
      printf("sent data = %s, size = %d, res length = %d\n\n",buf,len, res);
 
      char src[len];
      unsigned short port;
      res = os_udp_recv(fd,src,len,&port);
      printf("received data = %s, port = %d, read length = %d\n\n",src, port, res);
      }
 
     os_udp_sock_exit(fd);
 
 
    return 0;
}


server.c 
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include < sys/types.h > 
#include < sys/socket.h >
#include < netinet/in.h >
#include "udp_ipc.h"
 
int main(int argc, char *argv[])
{
 
     int fd = os_udp_sock_init(9930);
     int len = 256;
     char src[len];
 
     while(1){
      unsigned short port;
      printf("===================================\n");
      os_udp_recv(fd,src,len,&port);
      printf("received data = %s, port = %d\n\n",src, port);
 
 
      sprintf(src, "Welcome");
      os_udp_send(fd,port,src,len);
      printf("sent data = %s, size = %d\n\n",src,len);
      }
     os_udp_sock_exit(fd);
 
      
     return 0; 
}

0 comments:

Post a Comment