TCP- Sending and Receiving a Struct

This is a discussion on TCP- Sending and Receiving a Struct within the C Programming forums, part of the General Programming Boards category; Hi Guys So i have this Code i have been doing, and it giving mysterious out, anyone want to help, ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    TCP- Sending and Receiving a Struct

    Hi Guys
    So i have this Code i have been doing, and it giving mysterious out, anyone want to help, its been a while since i did c, so I'm a bit rusty,
    basically just sending a recieving a struct to mimic the 1st stages of SSL in Linux
    *Server Code*
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/stat.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    typedef struct {
    	char Version;			   //SSL Version
    	char SessionID;			   //Session ID
    	char Random[2];			   //Nounce
    	char CipherSuite[21][40];  //List of All the CipherSuites availabe in the SSL Client
    	char Compression_Method; // List of All the Compression Methods Available to the client
    } SSL_Struct;
    
    int main(){
        SSL_Struct Server_SSL;
        SSL_Struct Client_SSL;
        struct sockaddr_in client,server;
        int i;
        int j=0;
        int s,n,sock;
        int x;
        char y[4];
        char z;
        char b1[100]="",b2[100]="Hi Client";
        s=socket(AF_INET,SOCK_STREAM,0);
        server.sin_family=AF_INET;
        server.sin_port=2000;
        server.sin_addr.s_addr=inet_addr("127.0.0.1");
        bind(s,(struct sockaddr *)&server,sizeof server);
        listen(s,1);
        printf("\nServer ready,waiting for client....\n");
        n=sizeof client;
        sock=accept(s,(struct sockaddr *)&client,&n);
     //   for(;;){
            recv(sock,b1,sizeof(b1),0);
            //if(strcmp(b1,"end")==0)
              //  break;
            printf("\n--------------Client:-----------------------\n");
            printf("%s",b1);
            //printf("\nServer:");
            send(sock,b2,sizeof(b2),0);
          //  if(strcmp(b2,"end")==0)
            //    break;
            recv(sock,(SSL_Struct*)&Client_SSL,sizeof(Client_SSL),0);
            printf("\n--------------Client Hello:-----------------------");
            printf("\nVersion: %d",Client_SSL.Version);
            printf("\nSessionID: %d",Client_SSL.SessionID);
            printf("\nNonce: %s\n",Client_SSL.Random);
            for(j=1;j<21;j++){
              printf("\nCiperSuite:[%s]",Client_SSL.CipherSuite[j]);
             }
            printf("\nCompression Method : %d\n",Client_SSL.Compression_Method); 
            Server_SSL.Version=Client_SSL.Version;
            Server_SSL.SessionID=Client_SSL.SessionID;
            x = 1 + (int)(999999000.0*rand()/(RAND_MAX + 1.0));
            sprintf(y,"%4d",x);
            strcpy(Server_SSL.Random,y);
            for(i=1;i<21;i++){
            strncpy(Server_SSL.CipherSuite[i],Client_SSL.CipherSuite[i],40);
            }
            Server_SSL.Compression_Method=Client_SSL.Compression_Method; 
            send(sock,(SSL_Struct*)&Server_SSL,sizeof(Server_SSL),0); 
      
    
    
    
    
    //}
        close(sock);
        close(s);
    return 0;
    }
    Here Is my Client Code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/stat.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    typedef struct {
    	char Version;			   //SSL Version
    	char SessionID;			   //Session ID
    	char Random[3];			   //Nounce
    	char CipherSuite[21][40];  //List of All the CipherSuites availabe in the SSL Client
    	char Compression_Method; // List of All the Compression Methods Available to the client
    } SSL_Struct;
    
    int main(){
        SSL_Struct Server_SSL;
        SSL_Struct Client_SSL;
        struct sockaddr_in client,server;
        int s,sock;
        int x;
        int i;
        char y[4];
        char b1[100]="",b2[100]="Hi Server";
        s=socket(AF_INET,SOCK_STREAM,0);
        server.sin_family=AF_INET;
        server.sin_port=2000;
        server.sin_addr.s_addr=inet_addr("127.0.0.1");
        printf("\nClient ready....\n");
        connect(s,(struct sockaddr *)&server,sizeof(server));
      //for(;;){
           // printf("\nClient:");
              send(s,b2,sizeof(b2),0);
           // if(strcmp(b2,"end")==0)
             //   break;
              recv(s,b1,sizeof(b1),0);
        //    if(strcmp(b1,"end")==0)
          //      break;
              printf("\n--------------Server:-----------------------\n");
              printf("%s\n",b1);
              Client_SSL.Version='a'-94;
              Client_SSL.SessionID='a'-90; 
              x = 1 + (int)(999999000.0*rand()/(RAND_MAX + 1.0));
              sprintf(y,"%2d",x);
              strcpy(Client_SSL.Random,y);
              strcpy(Client_SSL.CipherSuite[1],"SSL_RSA_WITH_NULL_MD5");
              strcpy(Client_SSL.CipherSuite[2],"SSL_RSA_WITH_NULL_SHA");
              strcpy(Client_SSL.CipherSuite[3],"SSL_RSA_EXPORT_WITH_RC4_40_MD5");
              strcpy(Client_SSL.CipherSuite[4],"SSL_RSA_WITH_RC4_128_MD5");
              strcpy(Client_SSL.CipherSuite[5],"SSL_RSA_WITH_RC4_128_SHA");
              strcpy(Client_SSL.CipherSuite[6],"SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5");
              strcpy(Client_SSL.CipherSuite[7],"SSL_RSA_WITH_IDEA_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[8],"SSL_RSA_EXPORT_WITH_DES40_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[9],"SSL_RSA_WITH_DES_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[10],"SSL_RSA_WITH_3DES_EDE_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[11],"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[12],"SSL_DHE_DSS_WITH_DES_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[13],"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[14],"SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[15],"SSL_DHE_RSA_WITH_DES_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[16],"SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[17],"SSL_DH_anon_EXPORT_WITH_RC4_40_MD5");
              strcpy(Client_SSL.CipherSuite[18],"SSL_DH_anon_WITH_RC4_128_MD5");
              strcpy(Client_SSL.CipherSuite[19],"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[20],"SSL_DH_anon_WITH_DES_CBC_SHA");
              strcpy(Client_SSL.CipherSuite[21],"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA");
              Client_SSL.Compression_Method='a'-96;
              printf("\n-----------------------------------Testing-------------------------------\n");
              printf("\n--------------Client Hello:-----------------------");
              printf("\nVersion: %d",Client_SSL.Version);
              printf("\nSessionID: %d",Client_SSL.SessionID);
              printf("\nNonce: %s\n",Client_SSL.Random);
              for(i=1;i<21;i++){
              printf("\nCiperSuite:[%s]",Client_SSL.CipherSuite[i]);
              }
              printf("\nCompression Method : %d\n",Client_SSL.Compression_Method);
              send(s,(SSL_Struct*)&Client_SSL,sizeof(Client_SSL),0);
              recv(s,(SSL_Struct*)&Server_SSL,sizeof(Server_SSL),0);
              printf("\n--------------------Server Hello---------\n"); 
             //printf("\nServer:");
             printf("\nVersion: %d",Server_SSL.Version);
             printf("\nSessionID: %d",Server_SSL.SessionID);
             printf("\nNonce: %s",Server_SSL.Random);
             printf("\nCiperSuite :[%s]",Server_SSL.CipherSuite[3]);
             printf("\nCompression Method : %d\n",Server_SSL.Compression_Method);
      //}
        close(s);
    return 0;
    }
    Its complied and run in the same pc, however some random characters are cropping up when i print it, any help would be kindly appreciated
    Thanks
    Attached Images Attached Images  

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It looks to me like (amongst other problems) your random value might be overflowing the char array...

    You should probably write a small test proggy to make sure it doesn't.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Quote Originally Posted by CommonTater View Post
    It looks to me like (amongst other problems) your random value might be overflowing the char array...

    You should probably write a small test proggy to make sure it doesn't.
    how do i do that ?
    and how can i increase my char array ?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take the code that's generating your random numbers and run it in a small, separate test program and see what it prints out...

    Right now your array is defined in your struct as 3 characters... you may need to either generate a smaller random number string or increase the size of the array in the struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending/Receiving packets over socket
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 10-22-2010, 10:01 AM
  2. What model should I use when sending receiving packets?
    By Subsonics in forum Networking/Device Communication
    Replies: 22
    Last Post: 05-03-2010, 12:34 AM
  3. Sending / Receiving Info Via TCP or UDP
    By bengreenwood in forum C Programming
    Replies: 0
    Last Post: 03-24-2009, 02:17 AM
  4. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  5. Sending/Receiving Messages
    By osal in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2005, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21