Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <pthread.h>
#include <stdlib.h>
#define SEND_PORT 5004
#define SEND_INDEX 0
#define RECIEVE_PORT 5005
#define RECIEVE_INDEX 1
#define MSG_SIZE_IN 70
#define MSG_SIZE_OUT 22
#define THREAD_SLEEP_TIME 10000
int sockfd[1];
struct sockaddr_in out_addr, in_addr;
struct hostent *remote_server;
int outgoing_connection(int port, int pid)
{
if ((sockfd[pid] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("pid%d: ERROR opening socket\n", pid);
return 1;
}
bzero((char *) &out_addr, sizeof(out_addr));
out_addr.sin_family = AF_INET;
out_addr.sin_addr.s_addr = INADDR_ANY;
out_addr.sin_port = htons(port);
if (bind(sockfd[pid], (struct sockaddr *)&out_addr, sizeof(out_addr)) < 0) {
printf("pid%d: ERROR on binding\n", pid);
return 1;
}
return 0;
}
int incoming_connection(int port, int pid)
{
struct hostent *server;
server = remote_server;
if ((sockfd[pid] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("pid%d: ERROR opening socket\n", pid);
return 1;
}
bzero((char *) &in_addr, sizeof(in_addr));
in_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &in_addr.sin_addr.s_addr,
server->h_length);
in_addr.sin_port = htons(port);
if ((connect(sockfd[pid], &in_addr, sizeof(in_addr))) < 0) {
printf("pid%d: ERROR on binding\n", pid);
return 1;
}
return 0;
}
void* send_task (void* arg)
{
char msg[MSG_SIZE_OUT];
int sequ_no, pid, mutex, curr_time_s;
float curr_time_ns;
struct timespec t;
pthread_mutex_t t_mutex = PTHREAD_MUTEX_INITIALIZER;
pid = SEND_INDEX;
pthread_mutex_lock(&t_mutex);
if (clock_gettime(CLOCK_REALTIME, &t)) {
printf("pid%d: ERROR getting current time\n", pid);
} else {
curr_time_ns = t.tv_nsec;
curr_time_s = t.tv_sec;
sprintf(msg, "%d %d %f", sequ_no, curr_time_s, curr_time_ns);
if (send(sockfd[pid], msg, MSG_SIZE_OUT, 0)) {
printf("pid%d: ERROR writing to socket\n", pid);
} else {
printf("pid%d: %s\n", pid, msg);
sequ_no++;
}
}
usleep(THREAD_SLEEP_TIME);
pthread_mutex_unlock(&t_mutex);
pthread_exit(NULL);
return;
}
void* recieve_task (void* arg)
{
char buffer[MSG_SIZE_IN];
float lost_packets;
int socket, pid = RECIEVE_INDEX;
FILE *fp;
socket = sockfd[pid];
while(1) {
memset(buffer,0x0,MSG_SIZE_IN-1);
if (recv(sockfd[pid], buffer, MSG_SIZE_IN, 0)) {
printf("pid%d: ERROR reading from socket\n", pid);
} else {
printf("%s\n",buffer);
fp = fopen("sender_output.txt", "w");
lost_packets = atof((char *) strtok(buffer," "));
fprintf(fp, "%f\n", lost_packets);
fclose(fp);
}
}
pthread_exit(NULL);
return;
}
int main(int argc, char *argv[])
{
pthread_t thread[1];
int incoming, outgoing;
if (argc < 2) {
fprintf(stderr,"usage: %s hostname\n", argv[0]);
return 0;
}
if((remote_server= gethostbyname(argv[1]))==NULL) {
printf("Unknown host '%s' \n", argv[1]);
return 0;
} else {
outgoing = SEND_INDEX;
incoming = RECIEVE_INDEX;
if (incoming_connection(RECIEVE_PORT, incoming)) return 0;
if (pthread_create(&thread[incoming], NULL, recieve_task,
(void*) incoming)) {
printf("pid%d: ERROR pthread_create() failed!\n", incoming);
return 0;
}
if (outgoing_connection(SEND_PORT, outgoing)) return 0;
if (pthread_create(&thread[outgoing], NULL, send_task, (void*) outgoing)) {
printf("pid%d: ERROR pthread_create() failed!\n", outgoing);
return 0;
}
}
pthread_exit(NULL);
return 0;
}