Monday, October 29, 2007

UNIX Networking : Sockets UDP Transmitter and Reciever

Intro

Networking in UNIX or Linux is done with sockets. The following are 2 programs I made up.
1. Sends messages in UDP.
2. Listens and Recieves messages from UDP.

User Datagram Protocol or in short UDP is like a letter or telegram, its arrival is not anticipated and there is no connect and disconnect procedures to send and recieve UDP messages unlike TCP/IP. So the program is a little more easier and simpler to understand.


The Transmitter
Reads messages from input and sends it to localhost:34000.
To Setup a transmitter following must be done:
1. Setup a port address and open a socket and bind to it.
2. Send message to specified port and IP addresses.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>

/*Some are unused, But I read these are
general headers used in a network program
:)*/

int main(void)
{
struct sockaddr_in sin;
int s;

s = socket(AF_INET, SOCK_DGRAM, 0);
if(!s)
{
perror("socket()");
return 1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(9999);
sin.sin_addr.s_addr = INADDR_ANY;//Just any would do

if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
{
perror("bind()");
return 1;
}

struct sockaddr_in sout;

sout.sin_family = AF_INET;
sout.sin_port = htons(34000);//Destination PORT!
sout.sin_addr.s_addr = inet_addr("127.0.0.1");//Destination IP Address, here localhost.

char msg[1000];//Buffer

do
{
printf("Message: ");
scanf("%s",msg);
sendto(s, msg, strlen(msg)+1, 0, (struct sockaddr *)&sout, sizeof(sout));
//Sends the message
}while(*msg!='0'); /*Just an exit condition*/

close(s);//Close Socket

return 0;
}


The Reciever
This program listens to localhost:34000 and prints the messages whenever it recieves one.
To Setup a reciever following must be done:
1. Setup a port address and open a socket and bind to it.
2. Start Recieving...

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>

int main(void)
{

struct sockaddr_in sin;
char msg[10000];
int ret;
int sin_length;


int s;

s = socket(AF_INET, SOCK_DGRAM, 0);
if(!s)
{
perror("socket()");
return;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(34000);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
{
perror("bind()");
return 1;
}


do
{
sin_length = sizeof(sin);
ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)&sin, &sin_length);
//Waits until a message is recieved...
printf("Message[%s:%d] : %s\n",
inet_ntoa(sin.sin_addr), sin.sin_port,msg);
}
while(msg[0]!='0');

close(s);
return 0;
}


Thats all
Tested and proved working on Fedora Core 6.
Do let me know your results and comments.....
To know more about the functions check the Linux Manual for each function.

Check out... (TCP/IP Transciever)
:)

0 comments: