I am trying to send an array of strings over a socket and receive it on the other end.
Here is my client side ( side sending the data ) :
char* client_hello[] =
{
"Type client_hello",
"Version SSLv3.0",
"Session ID 1",
"Random 1as2@3%$h&KF(*)JAGG&(@H%A$D@J*@@",
"Cipher-Suites TLS_RSA_WITH_RC4_128_SHA",
"Compression null(0)"
};
SendBytes = send(sock, client_hello, 6, 0);
This is my Server side ( side receiving the data ):
int inMsgLen = recvfrom(clntSock, inMsg, 1024, 0,(struct sockaddr *)&clntAddr, (socklen_t*)&clntAddrLen);
if (inMsgLen < 0) {
//printf("Error in recvfrom() errno=%d\n", errno);
continue;//exit(1);
}else if (inMsgLen > 1024) {
printf("Received message too long (size=%d)\n", inMsgLen);
exit(1);
}else{
printf("Received Message: %s\n", inMsg);
}
inMsg
is declared as char inMsg[1024];
This is what the output is on the server side :
Received Message: +?
What am I doing wrong ? How can I send/receive the entire client_hello array ?
I am trying to send an array of strings over a socket and receive it on the other end.
But the code is sending the first six bytes of an array of char*
(as mentioned by WhozCraig in a comment to the question):
SendBytes = send(sock, client_hello, 6, 0);
the receiving side is reading the pointer addresses and treating them as strings, hence the junk.
To correct, send each string in turn but you will need to create a protocol that defines the beginning and end of a string as the bytes are written and read from sockets as streams, not some notion of message. For example, prefixing each string with its length (a sequence of digits terminated by a new-line character would be one simple option) followed by the actual string:
18\nSession ID 124\nCompression null(0)0\n
The receiving end would read to the new-line character, convert what was read to an int
, allocate a buffer to contain the string (remembering space for the null terminator) and then read that number of char
from the socket. A length of zero could be used to terminate the transfer of the list of strings.
Note that a call to send()
may result in only a part of the requested data being sent. The code needs to cater for this by keeping track of the number of bytes send so far and indexing into the buffer being sent.