Code:
/*=========client PGM=========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define MAX 1024
#define ZERO 0
#define ONE 1
#define TWO 2
#define EIGHT 8
#define CLINO 5
#define PORT 5000
#define PORT2 5001
int main(int argc, char **argv)
{
unsigned int sock, bytes_read, addr_len;
struct sockaddr_in server_addr, client_addr;
struct hostent *host;
char send_data[MAX], recv_data[MAX];
unsigned int sock2, bytes_read2, addr_len2;
struct sockaddr_in server_addr2, client_addr2;
char send_data2[MAX], recv_data2[MAX];
struct timeval tv, tv1;
fd_set writefds, readfds;
tv.tv_sec = 10;
tv.tv_usec = 500000;
/* tv1.tv_sec = 2;
tv1.tv_usec = 500000;
*/
if (argc != 2) {
printf ("Usage: ./client <server_ip>\n");
exit (EXIT_FAILURE);
}
host = (struct hostent *) gethostbyname((char *)argv[1]);
if ((sock = socket (AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
perror ("Socket error");
exit (EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero (&(server_addr.sin_zero), EIGHT);
bzero (recv_data, MAX);
if ((sock2 = socket (AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
perror ("Socket2 error");
exit (EXIT_FAILURE);
}
server_addr2.sin_family = AF_INET;
server_addr2.sin_port = htons(PORT2);
server_addr2.sin_addr = *((struct in_addr *)host->h_addr);
bzero (&(server_addr2.sin_zero), EIGHT);
bzero (recv_data2, MAX);
FD_ZERO (&writefds);
FD_SET (sock2, &writefds);
/* FD_ZERO (&readfds);
FD_SET (sock2, &readfds);
*/
while (ONE) {
printf ("Type Something for socket1 :");
fgets (send_data, MAX, stdin);
send_data [strlen (send_data) - ONE] = '\0';
sendto(sock, send_data, strlen(send_data), ZERO ,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
select (sock2+1, NULL, &writefds, NULL, &tv);
if (FD_ISSET (sock2, &writefds)) {
printf("Enter the data from the 2nd socket\n");
fgets (send_data2, MAX, stdin);
send_data2 [strlen (send_data2) - ONE] = '\0';
sendto(sock2, send_data2, strlen(send_data2), ZERO ,
(struct sockaddr *)&server_addr2, sizeof(struct sockaddr));
tv.tv_sec = 10;
tv.tv_usec = 500000;
} else
printf ("Timed out RTCP Send at client side\n");
}
return 0;
}
/*========= Server PGM =========================*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define MAX 1024
#define ZERO 0
#define ONE 1
#define TWO 2
#define EIGHT 8
#define CLINO 5
#define PORT 5000
#define PORT2 5001
int main(int argc, char **argv)
{
int sock;
unsigned int addr_len, bytes_read;
char recv_data[MAX], send_data[MAX];
struct sockaddr_in server_addr ,client_addr;
int sock2;
unsigned int addr_len2, bytes_read2;
char recv_data2[MAX], send_data2[MAX];
struct sockaddr_in server_addr2 ,client_addr2;
struct timeval tv, tv1;
fd_set readfds, writefds;
tv.tv_sec = 5;
tv.tv_usec = 500000;
/* tv1.tv_sec = 2;
tv1.tv_usec = 500000;
*/
if (argc != 2) {
printf ("Usage: ./server <client_ip>\n");
exit (EXIT_FAILURE);
}
if ((sock = socket(AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
perror("Socket error");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero), EIGHT);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -ONE) {
perror("Bind error");
exit(EXIT_FAILURE);
}
addr_len = sizeof(struct sockaddr);
if ((sock2 = socket(AF_INET, SOCK_DGRAM, ZERO)) == -ONE) {
perror("Socket2 error");
exit(EXIT_FAILURE);
}
server_addr2.sin_family = AF_INET;
server_addr2.sin_port = htons(PORT2);
server_addr2.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr2.sin_zero), EIGHT);
if (bind(sock2, (struct sockaddr *)&server_addr2, sizeof(struct sockaddr)) == -ONE) {
perror("Bind2 error");
exit(EXIT_FAILURE);
}
FD_ZERO (&readfds);
FD_SET (sock2, &readfds);
/* FD_ZERO (&writefds);
FD_SET (sock2, &writefds);
*/
addr_len2 = sizeof(struct sockaddr);
printf("\nUDP Server Waiting for client on port 5000");
fflush(stdout);
select (sock2+1, &readfds, NULL, NULL, &tv);
while (ONE) {
bytes_read = recvfrom(sock, recv_data, MAX, ZERO, (struct sockaddr *)&client_addr, &addr_len);
recv_data[bytes_read] = '\0';
printf("\n%s said : ", inet_ntoa(client_addr.sin_addr));
printf("%s", recv_data);
if (FD_ISSET (sock2, &readfds)) {
bytes_read2 = recvfrom(sock2, recv_data2, MAX, ZERO, (struct sockaddr *)&client_addr2, &addr_len2);
recv_data2[bytes_read2] = '\0';
printf("\n%s said : ", inet_ntoa(client_addr2.sin_addr));
printf("%s\n", recv_data2);
printf ("Socket 2\n");
/* tv.tv_sec = 2;
tv.tv_usec = 500000;
*/ } else
printf ("Timed out RTCP recv at server side\n");
/* sleep (TWO);
sendto(sock, recv_data, strlen(recv_data), ZERO,(struct sockaddr *)&client_addr, sizeof(struct sockaddr));
select (sock2+1, NULL, &writefds, NULL, &tv1);
if (FD_ISSET (sock2, &writefds)) {
sendto(sock2, recv_data2, strlen(recv_data2), ZERO,(struct sockaddr *)&client_addr2, sizeof(struct sockaddr));
tv1.tv_sec = 2;
tv1.tv_usec = 500000;
} else
printf ("Timed out RTCP Send at server side\n");
*/
fflush(stdout);
}
return ZERO;
}