Thread: TCP/IP Server Client implementation using C

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    TCP/IP Server Client implementation using C

    HI all, The C programming task is described as below:

    TASK1

    Your client and server should operate as follows. Your server runs first and it contains a buffer with an initial text string in it. The server process waits for connections from a client process using a server port number choosing by yourself (must be bigger than 1024).
    You should run your client program on another machine which is a Linux or Unix machine. Your client operates by sending TRANSLATE, GET, STORE, and EXIT commands to the server. You should create a single client that is able to send any of the four commands above. If the client sends an invalid command, the server should respond by sending back a message "400 Command not valid."

    Initially, when the server accepts the connection from a client, the server should display "server: got connection from client x.x.x.x" where x.x.x.x is the IP address of the client. It should also reply client with the message "Server is ready..." and be displayed at the client side.

    TRANSLATE
    This command requires the server process to translate the ASCII text sent from the client process to be in all upper-letter text and return back to the client process. The ASCII text sent from the client is ended with "." in the last line (similar as in the SMTP protocol). The server should reply "200 OK" and then return back the upper-letter text to the client. A client-server interaction looks like (shown at the client side):
    c: TRANSLATE
    s: 200 OK
    c: Hello.
    c: Nice to meet you!
    c: .
    s: HELLO.
    s: NICE TO MEET YOU!
    กก
    At the server side, the server process should display "x.x.x.x sends TRANSLATE" when receiving this command. For all other commands, the server should also do this kind of display.

    GET
    This command asks the server to return back the text string saved in the buffer of the server process. When your server receives a GET command from a client, it should return the string "200 OK" (terminated with a newline), followed by the text. A client-server interaction with this command thus looks like (suppose the server's buffer contains a text "I don't think we're in Kansas anymore."):
    c: GET
    s: 200 OK
    I don't think we're in Kansas anymore.

    STORE
    This command asks the server to store the text string that input from the client. When the server receives the STORE command from a client, it should return the acknowledgement "200 OK" (terminated with a newline) and then wait for client's input. The client input text is ended by "." after the last line of text (the saved string should not contain this ending line of "."). After saving the text, the server should return back the code "200 OK".
    A client-server interaction with this command thus looks like:
    c: STORE
    s: 200 OK
    c: One small step for man, one giant leap for mankind
    c: Read my lips, no new taxes
    c: .
    s: 200 OK
    EXIT
    This command closes the client program and informs the server to close, too. After receiving this command, the server should respond the acknowledgement "200 OK" to the client, display the "x.x.x.x sends EXIT", then close itself.
    A client-server interaction thus looks like:
    c: EXIT
    s: 200 OK

    TASK2
    Rewrite your server to be a concurrent server - that is a server that waits on the welcoming socket and then creates a new thread or process to handle the incoming request (the fork() system call). You can use either threads or processes to do this part.

    The Server.c code so far I made is below

    Code:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <netdb.h>
    #include <unistd.h> /*close */
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <time.h>
    #include <resolv.h>
    #include <linux/ioctl.h>
    #include <sys/stat.h>
    #include <net/if.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <memory.h>
    #include <ifaddrs.h>
    #include <stdarg.h>
    #include <math.h>
    #include <sys/termios.h>
    #include <string.h>
    #include <sys/ioctl.h>
    
    #define SUCCESS 0
    #define ERROR 1
    
    #define END_LINE 0X0A
    #define SERVER_PORT 5000
    #define MAX_MSG 100
    #define DEFAULT_BUFLEN 512
    
    int main (void)
    {
         int listenfd = 0, connfd = 0;
         struct sockaddr_in_serv_addr;
         struct serv_addr  server1;
         struct sockaddr sock1;
         typedef struct serv_addr serv_addr;
         typedef struct sockaddr sockaddr;    
         
         struct stored_data = ' ';
         typedef struct stored_data stored_data;
         char *reply = "Server is ready ";
         //typedef struct reply reply; 
         char *tokens;
         //typedef struct tokens tokens;
         char *command;
         struct data = {0};
         typedef struct data data;
         char recvbuf[DEFAULT_BUFLEN];
         char line[MAX_MSG];
         char sendBuff[1025];
         time_t ticks;
    
         listenfd = socket(AF_INET, SOCK_STREAM, 0);
         memset (&server1, 8, sizeof(server1));
         memset (&sock1, 20000, sizeof(sock1));
         //memset (sendBuff, '0', sizeof(sendBuff));
    
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
         serv_addr.sin_port = htons(SERVER_PORT);
    
         void *bind(listenfd, void (struct sockaddr*)&serv_addr, sizeof(serv_addr));
         listen(listenfd, 10);
         
         while(1)
         {
             connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
             printf ("Server: got connection from Client" + sizeof (struct sockaddr[0]));
             ticks = time(NULL);
             snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
             write(connfd, sendBuff, strlen(sendBuff));
       
             //reply = "Server is ready";
             //printf(reply);
             // Receive data
             data = recv(connfd,recvbuf, 1025,0);
             
              // Process Data
            //tokens = data.split(' ', 1);
              tokens = str_split(data, ' ');
              command = tokens[0];
                                 
              if (command  == "TRANSLATE") {
                      printf (sizeof (struct sockaddr[0]) + "sends TRANSLATE");
                      reply = "200 OK\n";
                      stored_data = strupr(stored_data);
                      reply = stored_data;
              } elsif (command == "GET") {
                      printf (sockaddr[0] + "sends GET");
                      reply = "200 OK\n";
                      reply = stored_data;
              } elsif (command == "STORE") {
                      printf (sockaddr[0] + "sends STORE"); 
                      reply = "200 OK\n";
                      stored_data = tokens[1];
                      reply = "200 OK\n";
              } elsif (command == "EXIT") {
                      printf (sockaddr[0] + "sends EXIT");
                      reply = "200 OK\n";
                      connfd.send ("EXIT");
                      break;
             } else {
                      reply = "400 command not valid\n";
             }
             //send reply to the client                                                                                                                                                                                                                                                                                                     connfd.send (reply);                                                                                                                                                                                                                                                                                                        //when we are out of the loop, close the connection as we are done.
              close(connfd);
              sleep(1);
          }
    }
    I am getting the below errors that I am unable to fix:

    Code:
    Server.c: In function ‘main’:
    02 Server.c:46: error: storage size of ‘server1’ isn’t known
    03 Server.c:51: error: expected identifier or ‘(’ before ‘=’ token
    04 Server.c:58: error: expected identifier or ‘(’ before ‘=’ token
    05 Server.c:70: error: expected identifier or ‘(’ before ‘.’ token
    06 Server.c:71: error: expected identifier or ‘(’ before ‘.’ token
    07 Server.c:72: error: expected identifier or ‘(’ before ‘.’ token
    08 Server.c:74: error: expected ‘)’ before ‘void’
    09 Server.c:88: error: expected identifier or ‘(’ before ‘=’ token
    10 Server.c:92: error: expected exp<b></b>ression before ‘data’
    11 Server.c:93: warning: assignment makes pointer from integer without a cast
    12 Server.c:98: error: expected identifier or ‘(’ before ‘=’ token
    13 Server.c:99: error: expected exp<b></b>ression before ‘stored_data’
    14 Server.c:100: error: expected ‘;’ before ‘{’ token
    Any help would be appreciated.Thanks!!!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Please look at this line of code.

    Code:
    struct sockaddr_in_serv_addr;
    What do you think it is doing?

    Because I think you might have meant to do this instead

    Code:
    typedef sockaddr_in serv_addr;
    or

    Code:
    struct sockaddr_in serv_addr;

    Tim S.
    Last edited by stahta01; 09-13-2016 at 07:34 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server implementation.
    By szczepanczyk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-19-2010, 01:38 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. Opinion about game server implementation (sockets)
    By Rennor in forum Game Programming
    Replies: 3
    Last Post: 08-29-2006, 05:10 PM
  4. UDP server-client
    By c++.prog.newbie in forum Networking/Device Communication
    Replies: 6
    Last Post: 09-12-2005, 08:59 AM
  5. server/client
    By goodmonkie in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2001, 07:00 AM

Tags for this Thread