0

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 ?

3
  • You're not handling the case when recvfrom returns 0. Why don't you print out what SendByte and inMsgLen are? Commented Sep 11, 2013 at 7:40
  • 2
    You're sending an array of pointer values from the client side that mean absolutely nothing once received by the server. I.e. your sending pointers, not your character data. Edit: its actually worse. you're sending the first 6 bytes of your pointer array, so on a 32bit plaform you're sending exactly 1.5 pointers. Commented Sep 11, 2013 at 7:41
  • 1
    "I am trying to send an array of strings" -- That may be what you want to do, but it's not what you're trying to do ... not even close. Even a teensy weensy amount of effort reading documents would make it clear that send with a len argument of 6 won't transmit 6 NUL-terminated strings. Try harder ... SO is not a substitute for effort. See meta.stackexchange.com/questions/182266/… Commented Sep 11, 2013 at 8:17

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

instead of sending these strings individually, is there a way of sending them together ?
Not the way it is currently structured. What is wrong with looping? Note that assembling them into a single string does not mean a single call to send() as it may send less bytes than requested.
@sukhvir that depends. What does "send them together" mean? In one send call? As a single logical block? Is this your protocol or are you coding to someone else's spec?
@sukhvir Sure there's a way of sending them together: malloc a buffer big enough, stick the strings into it, and send it. But why bother?
@hmjd The same is true for sending individual strings if they're too long.
|
-1

If you try to make a loop, using "for" to send... maybe is possible to server understand. Making client_hello[i], line by line..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.