Thread: Client / Server protocol

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    25

    Client / Server protocol

    Hello,
    Thanks for any help in advance. I'm trying to get the following client/server protocol to work however I'm a little stuck. The program is meant to tell a bus stop (server) the time the next bus is coming so the info can be displayed. I'm trying to do it so when the client enters the ID and it matches data on the server (bus stop) using an if statement it displays the data. Problem is the value is in a %s and doesnt seem to want to compare to a char. Could anyone help? I've marked out the error in the server section.

    Thanks!

    Client
    Code:
    #include <stdio.h>
    #include "streamsockets.h"
    
    /*-- HCI layer
     * Provides for interaction with the user
     *	Typically this might be held in a separate source file.
     *	This would allow for a different interface (such as a GUI)
     *	to be substituted.
     */
    
    char getname(char *dataline)
    {   /* input a name from the user and build a 'key:value' string */
        char busID[100];
        printf("Please enter the bus ID?");
        scanf("%s", busID);
        sprintf(dataline,"Bus ID:%s",busID);
    }
    
    void printbuffer(char *buf, int len) 
    {  /* print 'len' bytes from 'buf', 1 char at a time
          this is safer than using printf("%s",...) */
       int i;
       for (i=0;i<len;i++) {
           printf("%c", buf[i]);
       }
       printf("\n");
    }
    
    
    /*-- End of HCI layer --*/
    
    
    /*-- Application Layer
     *	This is where the application layer protocol is implemented.
     *	Typically this might be held in a separate source file.
     */
    void client_protocol(int socket)
    {
        const int buffersize = 1024;
        char buffer[buffersize];
        char name[buffersize];
        int rcode;
        rcode = recv_data(socket, buffer, buffersize);
        /* The value returned by recv can mean one of three things
         *   0 end-of-file       the socket has been closed
         *  -1 error             some error has occured
         *  >0 number-of-bytes   the number of bytes recieved.
         *
         * Here the action for end-of-file and error is to print a message
         * and continue on.  In reality the likely event is to exit the
         * function and make sure that resources are cleaned up.
         */
        if(rcode==0) {
    	printf("client: could not recv: socket was closed\n");
        } else if (rcode==-1) {
    	printf("client: send error: %s\n", strerror(errno));
        } else {
    	// do something with the data recieved
    	printf("client: %d bytes received\n", rcode);
            printbuffer(buffer,rcode);
        }
        getname(name);
        rcode = send_data(socket, name, strlen(name)+1);
        if (rcode==-1) {
    	printf("client: send error: %s\n", strerror(errno));
        }  else {
    	printf("client: %d bytes sent \n", rcode);
        }
    
        rcode = recv_data(socket, buffer, buffersize);
        if (rcode==0) {
    	printf("client: could not recv: socket was closed\n");
        } else if (rcode==-1) { 
    	printf("client recv error: %s\n", strerror(errno));
        } else {
    	printf("client: %d bytes received\n", rcode);
            printbuffer(buffer,rcode);
        }
    }
    
    /*-- End of application Layer --*/
    
    int main(int argc, char *argv[])
    {   int portnum;
        int rcode;
        int sock;
        char server_name[] = "c71179769";
    
     /*   printf("Client\n\nPlease enter the bus stop ID?\n");
        scanf("%s",server_name);  */
    
        printf("Please enter the bus stop ID?\n");  
        scanf("%d",&portnum);
        if (portnum<1) {
            printf("client: error: Port number must be a positive integer\n");
        } else if (portnum<1024) {
            printf("client: warning: Port numbers is in reserved range 0-1023\n");
        }
    
        sock = create_socket();
    
        if (sock == -1) {
    	printf("client: error creating socket: %s\n", strerror(errno));
    	exit(EXIT_FAILURE);
        }
    
        rcode = connect_sockets(sock, server_name, portnum);
        if (rcode == -1) {
    	printf("client: error connecting socket: %s\n", strerror(errno));
    	exit(EXIT_FAILURE);
        }
    
        client_protocol(sock);
    }



    server
    Code:
    #include <stdio.h>
    #include  "streamsockets.h"
    
    /* application protocol layer */
    
    void protocol_serverside(int socket, int clientnum)
    {
        const int buffersize = 2000;
        char buffer[buffersize];
        char name[80];
        int rcode;
        /* send to the client */
        char confirm[] = "Confirmed bus stop ID is connected to bus stop (server)";  
        rcode = send_data(socket, confirm, strlen(confirm));
        if (rcode==-1) {
    	printf("server send error %s\n", strerror(errno));
        } else {
    	printf("server: %d bytes sent\n", rcode);
        }
    
        /* receive from the client */
        rcode = recv_data(socket, buffer, buffersize-1);
        if (rcode==0) {
    	printf("server: socket was closed\n");
        } else if (rcode==-1) {
    	printf("server: recv error: %s\n", strerror(errno));
        } else {
    	printf("server: %d bytes received\n", rcode);
        }
    
        /* extract data from buffer into the array name[] 
           warning: this assumes the data in the buffer starts with 'Bus ID:'  */
        buffer[rcode]='\0';  /* make sure the buffer has a well firmed string */
        sscanf(buffer, "Bus ID:%s", name);
        
        printf("server: %s is client number %d\n", name,clientnum);
        
        /* if bus1 = %s which is the value printed above it should print message*/
        char bus1 = "1";
        char bus2 = "2";
        
        if(bus1 == %s)
        {
            printf("Bus info here");
        }
    
    
    
    
        /* send the reply message to the client */
        rcode = send_data(socket, "goodbye", 8);
        if (rcode==-1) {
    	printf("server: send error %s\n", strerror(errno));
        } else {
    	printf("server: %d bytes sent\n", rcode);
        }
    
       close(socket);
    }
    
    /*-- End of application Layer --*/
    
    
    void acceptclients(int socketid)
    {
        char remote_host[400];
        int child;
        int incomming;
        long clientcount;
    
        clientcount=0;
        for (;;) {  /* loop forever */
    	incomming = accept_connection(socketid, remote_host,400);
    	if (incomming == -1) {
    	    printf("server: error accepting connection: %s\n", strerror(errno));
    	    exit(EXIT_FAILURE);
    	} else {
       	    printf("server: connection accepted from %s\n",remote_host);
    	    clientcount++;
    	
        child = fork();
    	    if (child==0) {
    	      /* child process */
    		close(socketid);
    		protocol_serverside(incomming,clientcount);
                    printf("server: finished serving client %d \n",clientcount);
    		exit(0);
    	    } else {
    	      /* parent process */
    		close(incomming);
    		printf("server: child process created id:%d\n", child);
    	    }
    	}
        } /* end of main loop */
    }
    
    int main(int argc, char *argv[])
    {
        int rcode;
        int portnum = 4444;
        int server_socket;
    
        
       /* printf("demonstration server:\n\nPlease enter the bus stop ID?\n"); 
        scanf("%d",&portnum); */
    
        server_socket = create_socket();
        if (server_socket == -1) {
    	printf("server: error creating socket: %s\n", strerror(errno));
    	exit(EXIT_FAILURE);
        }
    
        rcode = bind_socket(server_socket, portnum);
        if (rcode == -1) {
    	printf("server: error binding port number to socket: %s\n", strerror(errno));
    	exit(EXIT_FAILURE);
        }
    
        rcode = listen(server_socket, 5);
        if (rcode == -1) {
    	printf("server: error setting size of socket's listen queue: %s\n", strerror(errno));
    	exit(EXIT_FAILURE);
        }
        
        printf("server: now wait to accept connections\n");
        acceptclients(server_socket);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    /* if bus1 = %s which is the value printed above it should print message*/
    >    char bus1 = "1";
    >    char bus2 = "2";
        
    >    if(bus1 == %s)
    >    {
    >        printf("Bus info here");
    >    }
    Use strcmp() to compare strings.
    Code:
    #include <string.h>
    .
    .
        /* if bus1 = %s which is the value printed above it should print message*/
        char bus1[] = "1";
        char bus2[] = "2";
        
        if(strcmp(name, bus1) == 0)
        {
            printf("Bus info here");
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  2. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM