winsock receive problem

Mar 16, 2009 at 11:12am
Hi everybody,

I'm working with winsock TCP sockets. I receive a char array from the recv function. We send the value 0x27 and we receive the following data: array[0]=27 (dec). Why does it so work? How can I have the original hex value??

Thank you!
Mar 16, 2009 at 11:34am
you have send hex 27 and received 27, whats the problem?
treat it as hex? or im not able to understand your problem?
Mar 16, 2009 at 12:10pm
Can you post a small sample that reproduces the problem. It's almost certainly a display problem.
Mar 16, 2009 at 12:12pm
Hi,

The problem is that, we can see with wireshark that we've sent the byte 0x27 (in decimal: 39). And the returned character array from the recv-function is a 27 in decimal, that is 0001 1011=27(dec) and we want to have
0010 1101 =27(hex).
why does it so work? and how can I do to get the hex expression?

Thank you!
Mar 16, 2009 at 12:24pm
its just the debugger which is showing you wrong values because the values wont change when you receive().

you must be typecasting the value when you sent it from hex to char.
similarly typecast it back from char to hex and you will certainly see the correct value.
its just a display problem as kbw said. the values are correct, its just that debugger is showing wrong values as it doesnt know what to expect.

you can post some part of code(client and server part) so that the thing could be a little clear.
Mar 16, 2009 at 12:27pm
Hi!

I use this function to receive the buffer
msgLength = recvfrom(socketIn, buffer, l_buffer,0,(SOCKADDR*)&clientService, &l_client);
In a following if-statement, i got the result, that my buffer at position 0 is not equal to 0x27.
But it is.
The result is true, when i compare the buffer[0] with 27 (in decimal).

I don't know why.
Wireshark gives me the correct value, which is 0x27.

I hope you can help me.
Thanks.
Mar 16, 2009 at 12:33pm
Hi again,

How can I do this typecast? how can I change from 0001 1011(27dec) to 0010 1101(27 hex)

thank you!
Mar 16, 2009 at 12:35pm
can you tell me the signatures of sent and recv functions.

because i am not able to know what are the datatypes of various parameters?
like recvfrom is receiving similarly there must be a function which sending from the client.
write the declaration of the functions?


Mar 16, 2009 at 12:37pm
see when you code you work in decimal only.
like if you are doing this: int i = 39;

but sometimes the debugger shows the values in hex. so if you are putting 39 the debugger is showing 0x27 which is actually 39.
i think there is some confusion using the debugger only.
because if you are sending 27dec then you must receive 27dec only, it cant change to 27hex.
Mar 16, 2009 at 12:38pm
there is no typecasting from dec to hex. you have write code for that.
Mar 16, 2009 at 12:56pm
Hi again!

This is my send function:
bytesSent = sendto(socketOut, packet, sizeof(SigDumpRequest), 0, (SOCKADDR*)&clientService, sizeof(clientService));

and this my packet:

struct DumpReqParameters{
unsigned short int tr_src_mID;
unsigned short int tr_src_mLength;
unsigned short int tr_src_mValue;

unsigned short int tr_src_neg_mID;
unsigned short int tr_src_neg_mLength;
unsigned short int tr_src_neg_mValue;

unsigned short int tr_combinationID;
unsigned short int tr_combinationLength;
unsigned short int tr_combinationValue;

unsigned short int tr_timeoutID;
unsigned short int tr_timeoutLength;
unsigned short int tr_timeoutValue;

unsigned short int tr_st_sfnID;
unsigned short int tr_st_sfnLength;
unsigned short int tr_st_sfnValue;

unsigned short int tr_st_ttiID;
unsigned short int tr_st_ttiLength;
unsigned short int tr_st_ttiValue;

unsigned short int data_srcID;
unsigned short int data_srcLength;
unsigned short int data_srcValue;

unsigned short int data_streams_mID;
unsigned short int data_streams_mLength;
unsigned short int data_streams_mValue;

unsigned short int lengthID;
unsigned short int lengthLength;
unsigned int lengthValue;

unsigned short int leadtimeID;
unsigned short int leadtimeLength;
unsigned int leadtimeValue;

and with this instruction we generate the packet:

memcpy(packet,(char*) (SigDumpRequest*) &msg, sizeof(SigDumpRequest));

we want to get the second parameter of the packet and we know it is 0x27 but I don't know how can I convert its decimal value to hex

we set the Parameter with the following instruction:
DumpReqParameters msg;
msg.tr_src_mLength = htons(39);
Mar 16, 2009 at 1:10pm
lets say you have these signatures of send and recv.

1
2
3
4
5
6
ssize_t sendto(int socket, const void *message, size_t length,
       int flags, const struct sockaddr *dest_addr,
       socklen_t dest_len);

ssize_t recvfrom(int socket, void *buffer, size_t length, int flags,
             struct sockaddr *address, socklen_t *address_len);


lets say you have a packet object and its filled with correct value:
1
2
DumpReqParameters msg;
msg.tr_src_mLength = htons(39);


to send you do this:
sendto(socket,(void*)&msg, sizeof(msg), ...rest of the parameters...);

to receive you do this:

1
2
3
DumpReqParameters  recv_msg;
recvfrom(socket, (void*)&recv_msg, sizeof(recv_msg), ...rest of the parameters...);
unsigned short len = recv_msg.tr_src_mLength;


try this it should solve the problem.

Mar 16, 2009 at 1:12pm
second thing:
when you do:

msg.tr_src_mLength = htons(39);

check the value of msg.tr_src_mLength after assignment. this is the value you will get when you recv.
Mar 16, 2009 at 1:37pm
i have to make a structure for all the incoming messages?
isn't it a little bit complicated?

I got the following error: "Can not convert parameter 2 void* to char*", when i write (void*)&recv_msg.




Last edited on Mar 16, 2009 at 2:01pm
Mar 16, 2009 at 2:25pm
Hi!

I tried it and I got the same error like benemaja

"Can not convert parameter 2 void* to char*", when i write (void*)&recv_msg.

Thank you
Mar 17, 2009 at 5:36pm
oh..sorry for repling late..
your signature is like this i think:
1
2
3
4
5
6
ssize_t sendto(int socket, const char *message, size_t length,
       int flags, const struct sockaddr *dest_addr,
       socklen_t dest_len);

ssize_t recvfrom(int socket, char *buffer, size_t length, int flags,
             struct sockaddr *address, socklen_t *address_len);


instead of taking void* its taking char* so replace void* to char* and you will be done. :)
Mar 17, 2009 at 5:37pm
i have to make a structure for all the incoming messages?
isn't it a little bit complicated?

I got the following error: "Can not convert parameter 2 void* to char*", when i write (void*)&recv_msg.


but then you dont have any option to send the whole data..this is the only way to send data across network..
you can find a way to optimize all the bulky stuff.. thats it.
Mar 17, 2009 at 5:39pm
I mean do this:

to send you do this:
sendto(socket,(char*)&msg, sizeof(msg), ...rest of the parameters...);

to receive you do this:
DumpReqParameters recv_msg;
1
2
recvfrom(socket, (char*)&recv_msg, sizeof(recv_msg), ...rest of the parameters...);
unsigned short len = recv_msg.tr_src_mLength;


you can send me a PM if it didnt solve as im not always on the forum.
Topic archived. No new replies allowed.