Problem with sending structures over a socket

S

simu

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

.....
 
A

Alf P. Steinbach

* (e-mail address removed):
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.

Please ask in a group relevant to socket programming (and there, state
which libraries you're using, e.g. which class ClientSocket is).

clc++ is only for C++ language questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top