By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Problem with sending structures over a socket

P: n/a
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

.....

Jul 9 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* si**@snowplow.ch:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.