Thread: How to send Struct ?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    How to send Struct ?

    hi,
    i need to send "struct" from server to client and client should receive it.
    i am not able to write the code from sending and receiving the strucutre.

    My main task is : i need to generate random numbers and send it through structure to client
    Program of server
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<error.h>
    #include<strings.h>
    #include<unistd.h>
    #include<arpa/inet.h>
    #include <time.h>
    
    #define ERROR	-1
    #define MAX_CLIENTS	2
    #define MAX_DATA	1024
    
    
    main(int argc, char **argv)
    {
    	struct packet
    	{
    		int node;
    		int temp;
    		int humidity;
    	};
    	struct packet p1; 
    	
    	struct sockaddr_in server;
    	struct sockaddr_in client;
    	int sock;
    	int new;
    	int sockaddr_len = sizeof(struct sockaddr_in);
    	int data_len;
    	char data[MAX_DATA];
    
    
    
    	
    int i;
    int a,b,c;
    char d[100],e[100],f[100];
    time_t t;
    srand((unsigned) time(&t));
    
    	
    	
    	if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
    	{
    		perror("server socket: ");
    		exit(-1);
    	}
    		
    	server.sin_family = AF_INET;
    	server.sin_port = htons(atoi(argv[1]));
    	server.sin_addr.s_addr = INADDR_ANY;
    	bzero(&server.sin_zero, 8);
    			
    	if((bind(sock, (struct sockaddr *)&server, sockaddr_len))== ERROR)
    	{
    		perror("bind : ");
    		exit(-1);
    	}
    	
    	if((listen(sock, MAX_CLIENTS)) == ERROR)
    	{
    		perror("listen");
    		exit(-1);
    	}
    	
    	while(1) // Better signal handling required
    	{
    		if((new = accept(sock, (struct sockaddr *)&client, &sockaddr_len))== ERROR)
    		{
    			perror("accept");
    			exit(-1);
    		}
    		
    		printf("New Client connected from port no %d and IP %s\n",
    		 ntohs(client.sin_port), inet_ntoa(client.sin_addr));
    
    		data_len = 1;
    				
    		while(data_len)
    		{
    			data_len = recv(new, data, MAX_DATA, 0);
    			data_len= 5;
    							
    a=rand()%10 ;
    printf("a is %d\n",a);
    sprintf(d,"%d",a);
    printf("\n char no. is  %s",d);
    
    
    b=rand()%100;
    printf("\n b is %d\n",b);
    sprintf(e,"%d",b);
    printf("\n char no. is  %s",e);
    
    c=rand()%100;
    printf("\n c is %d\n",c);
    sprintf(f,"%d",c);
    printf("\n char no. is  %s",f);
      
    p1.node=a;
    p1.temp=b;
    p1.humidity=c;  
    
    printf("node_id=%u ",p1.node);
    printf("temp=%u ",p1.temp);
    printf("himid=%u ",p1.humidity);
      
    // WriteBuffer (&p1,sizeof(p1));
     //send(new,&p1,sizeof(p1),0) ;
    
    n=recvfrom(socket_fd,buffer,sizeof(mypdu),0,
    				(struct sockaddr *)&client,&sockaddr_len);
    
    printf("\n over");
    
    
    
    
    
    				//send(new, data,data_len, 0);
    			
    				//printf("\n Sent mesg: %s", data);
    				
    				
    		
    			 
    		}
    
    		printf("Client disconnected\n");
    		
    		close(new);
    		
    	}
    
    	close(sock);
    
    	}

    Program of client

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<strings.h>
    #include<arpa/inet.h>
    
    #define ERROR	-1
    #define BUFFER	1024
    
    
    main(int argc, char **argv)
    {
    	struct sockaddr_in remote_server;
    	int sock;
    	char input[BUFFER];
    	char output[BUFFER];
    	int len;
    	
    	
    	if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
    	{
    		perror("socket");
    		exit(-1);
    	}
    		
    	remote_server.sin_family = AF_INET;
    	remote_server.sin_port = htons(atoi(argv[2]));
    	remote_server.sin_addr.s_addr = inet_addr(argv[1]);
    	bzero(&remote_server.sin_zero, 8);
    	
    	if((connect(sock, (struct sockaddr *)&remote_server, sizeof(struct sockaddr_in))) == ERROR)
    	{
    		perror("connect");
    		exit(-1);
    	}
    	
    	while(1)
    	{
    		
    		fgets(input, BUFFER, stdin);
    		send(sock, input, strlen(input), 0);
    	
    
    		len = recv(sock, output, BUFFER, 0);
    		output[len] = '\0';
    		printf("%s\n", output);
    	}
    	
    	close(sock);
    	
    	
    }


    Please help me !!

    regards

    sunit

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sunit View Post
    hi,
    i need to send "struct" from server to client and client should receive it.
    i am not able to write the code from sending and receiving the strucutre.
    To send a struct, cast it as (char*) on the send side, gather all the data into a char buffer on the other side, and cast that back into a struct.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Can u explain in details

    hi,
    can u please explain me in details as i m new to client server programming

    sunit

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A struct is basically a series of bytes, just like a string. Only, it's easier to send a string through the network, isn't it? So by casting it as (char *) you can start to treat it more like a series of bytes, send it, and your server can recieve that series of byte and convert it back into your struct. It's called serialization.

    If you're unfamiliar with casting, pointers or any of that stuff - you should learn that before moving on - it's rather fundamental for any programming, not just client-server stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM