Raghusys 0 Newbie Poster

I have been told to write a c or c++ program for TCP server client program with some conditions that is listed below:

You are to implement a synchronization server managing barriers. Your synchronization server should be a stand-alone single threaded process that continuously awaits requests from other processes (the “clients”). All communications between the synchronization server and its clients should be done through Internet domain sockets.

The Client Side

You should provide four functions:

int set_barrier(char* bname, int bnpar);
int delete_barrier(char* bname);
int wait_barrier(char* bname);
int terminate_server();

where bname is a string containing up to 16 non-blank characters identifying a specific barrier and bnpar specifies the number of participating processes.

The set_barrier() function should:

  1. Connect to the port number of your synchronization server;
  2. Send to the server a message specifying the name of the barrier to be set and its number of participating processes;
  3. Wait for the reply of the server.

Any set_barrier() function that specifies a barrier that was already set should return -1 instead of the normal return code (0).
The delete_barrier() function should:

  1. Connect to the port number of your synchronization server;
  2. Send to the server a message specifying the name of the barrier to be deleted;
  3. Wait for the reply of the server.

The wait_barrier() function should:

  1. Connect to the port number of your synchronization server;
  2. Send to the server a message specifying the name of the barrier to waited on;
  3. Wait for the reply of the server.

Any wait_barrier() and any delete_barrier() function that specifies a barrier that does not exist should return -1 instead of the normal return code (0).
The terminate_server() function should:

  1. Connect to the port number of your synchronization server;
  2. Send to the server a message requesting that the server terminates.

The Synchronization Server
The synchronization server manages a one-dimensional set of barriers. Each barrier will have:

  1. A name (bname) identifying the barrier;
  2. A number of participating processes (bnpar);
  3. A count (bcount) representing the number of processes that have already reached the barrier;
  4. A queue holding all pending wait_barrier requests.

The synchronization server should go through an infinite loop waiting for requests until it receives a terminate_server request.
When it receives a set_barrier request from one of its clients, the server should:

  1. Send an error reply if the set_barrier request refers to a barrier that already exists;
  2. Create the requested barrier with the specified bnpar and set its bcount to zero;
  3. Send a reply to the client.

When it receives a delete_barrier request from one of its clients, the server should:

  1. Send an error reply if the delete_barrier request refers to a barrier that does not exist;
  2. Delete the requested barrier;
  3. Send a reply to the client.

When it receives a wait_barrier request from one of its clients, the server should:

  1. Send an error reply if the wait_barrier request refers to a barrier that does not exist;
  2. Increment the bcount of the barrier;
  3. Delay its reply to the client until bcount equals the number of participants bnpar in the barrier in order to synchronize all the processes participating in the barrier;
  4. Reset bcount to zero.

I have completed till the client server connection establishment but coudnt manage to implement it with functions,
herewith iam posting the code that i have done so far:

server.c:

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
//#include "list.h"

#define MAXPENDING 3
#define BUFFERSIZE 128


int main(int argc, char *argv[])
 {
  int serversock, clientsock;
  struct sockaddr_in server_addr, client_addr;
  char bname[BUFFERSIZE];
  char action[BUFFERSIZE];
  int received = -1;

  if (argc != 2)
  {
    fprintf(stderr, "USAGE: server <port>\n");
   exit(1);
  }

  if ((serversock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  {
   perror("Socket Creation Failed");
  }

  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;                  // Internet/IP
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);   // Incoming addr
  server_addr.sin_port = htons(atoi(argv[1]));       // server port

  if (bind(serversock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
  {
   perror("failed to bind");
  }

  if (listen(serversock, MAXPENDING) < 0)
  {
   perror("failed to listen on server socket");
  }
 while(1)
 {
   unsigned int clientlen = sizeof(client_addr);
   if((clientsock = accept(serversock,(struct sockaddr *) &client_addr,&clientlen)) < 0)
   {
    perror("failed to accept client connection");
   }
   fprintf(stdout, "Client connected: %s\n",inet_ntoa(client_addr.sin_addr));

   if ((received = recv(clientsock, bname, BUFFERSIZE, 0)) < 0)
  {
   perror("message receiving Failed from client");
  }
  while (received > 0)
  {
   char bname,set,delete,conn,wait,terminate;
   conn=recv(clientsock,action,BUFFERSIZE,0);
   if(conn == set)
   {
    set_barrier(bname);
   }
else if (conn == delete)
   {
    delete_barrier(bname);
   }
   else if(conn == wait)
   {
    wait_barrier(bname);
   }
   else if(conn == terminate)
   {
    terminate_server();
    close(clientsock);
   }
   else
   {
        perror("message sending to client failed");
   }
  }
 }
}

 int set_barrier(char *bname)
 {
//insert_first(bname);
  int serversock;
  send(serversock,bname,BUFFERSIZE,0);
 }

 int delete_barrier(char *bname)
 {
  //delete_element(bname);
 }

 int wait_barrier(char *bname)
 {
  int bcount;
  bcount++;
 }

int terminate_server()
{
 int clientsock;
 close(clientsock);
 exit(0);
}


client.c:

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define BUFFSIZE 128

int main(int argc, char *argv[])
 {
  int sock;
  struct sockaddr_in server_addr;
  char bname[BUFFSIZE];
  unsigned int echolen;
  int received = 0;

  if (argc != 5)
  {
    fprintf(stderr, "USAGE: client <serverip> <message> <port> <action>\n");
    exit(1);
  }
 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  {
   perror("socket creation failed");
  }

  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;                  // InternetIP
  server_addr.sin_addr.s_addr = inet_addr(argv[1]);  // IP address
  server_addr.sin_port = htons(atoi(argv[3]));       // server port

 if (connect(sock,(struct sockaddr *) &server_addr,sizeof(server_addr)) < 0)
 {
  perror("connection with server failed");
 }
 echolen = strlen(argv[2]);
 char set,delete,wait,terminate;
 if (send(sock, argv[2], echolen, 0) != echolen)
 {
  perror("number mismatch of sent bytes");
 }
 if(argv[5]==set)
 {
  set_barrier(bname);
else if(argv[5]==delete)
 {
  delete_barrier(bname);
 }
 else if(argv[5]==wait)
 {
  wait_barrier(bname);
 }
 else if(argv[5]==terminate)
 {
  terminate_server();
 }
 fprintf(stdout, "Received from server:");
 while (received < echolen)
 {
  int bytes = 0;
  if ((bytes = recv(sock,bname, BUFFSIZE-1, 0)) < 0)
  {
   perror("failed to receive bytes from server");
  }
  received =received + bytes;
  bname[bytes] = '\0';
  fprintf(stdout, bname);
}
 fprintf(stdout, "\n");
 close(sock);
 exit(0);
}

int set_barrier(char *bname)
{
 char bname[BUFFSIZE];
 int sock;
 send(sock, argv[2], BUFFSIZE,0);
}

int delete_barrier(char *bname)
{
 send(sock, argv[2], BUFFSIZE, 0);
}

int wait_barrier(char *bname);
{
 send(sock, argv[2], BUFFSIZE, 0);
}
int terminate_server()
{
 send(sock, arg[5], BUFFSIZE, 0);
}

I have also created a Linked List to refer the data that will be called by the server.
Please suggest is this program in the right way or have to do some changes according to the scnario given
Thanks in Advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.