Struct über connected Socket senden



  • I've a problem with sending a structure of the type pkt over my connected socket. I've not too much experience in programming C/C++. Thus, I've no clue what the problem could be. I've spent quite a lot of time in this but didn't got the problem. I'm using sendmsg() and recvmsg(). Both seems to work. But when I try to access the received data, I recognize that the results are completely wrong (probably initial values instead of received data). Can anybody tell me my mistake? Would be great!

    CLIENT:

    int main ( int argc, int argv[] )
    {
    	struct msghdr msg;
    	struct iovec iov[1];
    	struct pkt {
    		int a;
    		int b;
    	} p ;
    	p.a = 123;
    	p.b = 234;
    
    	iov[0].iov_base = &p;
    	iov[0].iov_len = sizeof(p);
    
    	try
    	{
    		ClientSocket client_socket ( "localhost", 30000 );
    
    		std::string reply;
    		std::string message;
    
    		try
    		{
    			msg.msg_iov = iov ;
    			msg.msg_iovlen = 1 ;
    			msg.msg_name = NULL ;
    			msg.msg_namelen = 0 ;
    			msg.msg_control = (caddr_t) &p ;
      			msg.msg_controllen = sizeof (struct pkt) ;
      			msg.msg_flags = 0 ;
    
    			client_socket << msg ;
    		}
    

    ...

    SERVER:

    int main ( int argc, int argv[] )
    {
    	struct iovec iov[1]; 
    	struct msghdr inmessage;
    	char * buffer_rcv;
    
    	struct pkt {
    		int a;
    		int b;
    	} inp ;
    
    	try
        {
    		// Create the socket
    		ServerSocket server ( 30000 ) ;
    
    		while ( true )
    		{
    			ServerSocket new_sock ;
    			server.accept ( new_sock ) ;
    
    			try
    			{
    				while ( true )
    				{
    					buffer_rcv = (char*)memset(&inmessage, 0, sizeof(inmessage));
    
    					iov[0].iov_base = buffer_rcv; 
    					iov[0].iov_len = sizeof(inmessage);
    
    					inmessage.msg_iov = iov; 
    					inmessage.msg_iovlen = 1; 
    					inmessage.msg_name = NULL; 
    					inmessage.msg_namelen = 0; 
    					inmessage.msg_control = (caddr_t) &inp; 
    					inmessage.msg_controllen = sizeof (struct pkt); 
    					inmessage.msg_flags = 0;
    
    					new_sock >> inmessage;
    
    					std::cout << "inp.a: '" << inp.a << "'" << endl;  // returns 134522371
    					std::cout << "inp.b: '" << inp.b << "'" << endl;  // 134522371 too
    

    ....

    Vielen Dank. Grüsse, Simu


Anmelden zum Antworten