Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Winsock : Is it possible to send integer variables using send()?

******************************************
Common and successful method to send information over socket
char DataBuf[256];
strcpy(DataBuf, "100 150")
nRet = send(theSocket,DataBuf,DataBuf,0);
*******************************************

I tried using the below but failed. Any suggestions or comments?
struct struct_var{
int Packet;
int Number;
};struct_var *arraystruct;

(*arraystruct).Packet = 100;
(*arraystruct).Number = 150;

arraystruct = (struct_var *) malloc(sizeof(struct_var));
nRet = send(theSocket,(char*)arraystruct,sizeof(arraystruct),0);

Thanks
[663 byte] By [yhx] at [2007-11-11 10:15:32]
# 1 Re: Winsock : Is it possible to send integer variables using send()?
You should allocate the struct by malloc (or better yet, new) *before* you assign values to its members. Secondly, instead of using (*arraystruct).Packet use the more conventional form: arraystruct->Packet.

arraystruct = (struct_var *) malloc(sizeof(struct_var));//first step
arraystruct->Packet = 100; //second step
arraystruct->Number = 150;
Danny at 2007-11-11 20:59:10 >
# 2 Re: Winsock : Is it possible to send integer variables using send()?
I recommend not sticking the mess after the } for a struct. Treat them like a class (well, actually, you can do the end of bracket mess on a class too, but why would you?).

for example

struct str
{
int data;
int moredata;
};

str mystr;

also, this does not look like dynamic memory helps you at all, bypass it and just allocate the variables.

finally, remember your binary integers sent to the other end will be in one of the endian forms so if your other computer is a different type that could show up as an issue.
jonnin at 2007-11-11 21:00:11 >
# 3 Re: Winsock : Is it possible to send integer variables using send()?
Hi Danny, thks for stepping in.

I had a typo error in my first post. I actually did :
struct struct_var{
int Packet;
int Number;
};struct_var *arraystruct;

arraystruct = (struct_var *) malloc(sizeof(struct_var));
(*arraystruct).Packet = 100;
(*arraystruct).Number = 150;
----------------

I have taken your advice for the
arraystruct->Packet = 100;
arraystruct->Number = 150;

When I do a printf ,%d, I am getting 100 for the 1st but an address for the 2nd. Using Ethereal, I see an error "malform: Skinny". I have error handling for send() and recv() for both the server and client side and nothing was triggered.
yhx at 2007-11-11 21:01:15 >
# 4 Re: Winsock : Is it possible to send integer variables using send()?
how does the prinbtf call look? Also, are you calling it after the values have been sent or before? For debugging purposes, you want to do it on both sides. Finally, measure teh size of the struct:
int sz=sizeof(struct_var);

Do it both on the client and the server. If either gives you a result that isn't 8, this is the problem.
Danny at 2007-11-11 21:02:16 >
# 5 Re: Winsock : Is it possible to send integer variables using send()?
I have solved my problem.
Posting for the benefit of other winsock programmer:

nRet = send(theSocket,(char*)arraystruct,sizeof(*arraystruct),0);

Cheers'
yhx at 2007-11-11 21:03:15 >
# 6 Re: Winsock : Is it possible to send integer variables using send()?
Ho wdid the send() command look like before this fix?
Danny at 2007-11-11 21:04:19 >
# 7 Re: Winsock : Is it possible to send integer variables using send()?
Before:
struct struct_var{
int Packet;
int Number;
};struct_var *arraystruct;
arraystruct = (struct_var *) malloc(sizeof(struct_var));
(*arraystruct).Packet = 100;
(*arraystruct).Number = 150;
nRet = send(theSocket,(char*)arraystruct,sizeof(arraystruct),0);

After:
struct struct_var{
int Packet;
int Number;
};struct_var *arraystruct;
arraystruct = (struct_var *) malloc(sizeof(struct_var));
(*arraystruct).Packet = 100;
(*arraystruct).Number = 150;
nRet = send(theSocket,(char*)arraystruct,sizeof(*arraystruct),0);

or
nRet = send(theSocket,(char*)arraystruct,sizeofstruct_var),0);
yhx at 2007-11-11 21:05:12 >
# 8 Re: Winsock : Is it possible to send integer variables using send()?
That's why naming conventions are important. If your arraystruct variable had a name that denoted a pointer, say parraystruct, it would have been much easier to spot the bug. Anyway, don't forget to call free(arraystruct) at the end.
Danny at 2007-11-11 21:06:22 >