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];
inMsg声明为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 ?
我究竟做错了什么 ?如何发送/接收整个client_hello数组?
1 个解决方案
#1
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):
但代码是发送char *数组的前六个字节(正如WhozCraig在对问题的评论中所提到的):
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.
接收端将读取换行符,将读取的内容转换为int,分配缓冲区以包含字符串(记住空终止符的空间),然后从套接字读取该数量的char。长度为零可用于终止字符串列表的传输。
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.
请注意,对send()的调用可能导致仅发送所请求数据的一部分。代码需要通过跟踪到目前为止发送的字节数并将索引编入正在发送的缓冲区来满足此要求。
#1
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):
但代码是发送char *数组的前六个字节(正如WhozCraig在对问题的评论中所提到的):
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.
接收端将读取换行符,将读取的内容转换为int,分配缓冲区以包含字符串(记住空终止符的空间),然后从套接字读取该数量的char。长度为零可用于终止字符串列表的传输。
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.
请注意,对send()的调用可能导致仅发送所请求数据的一部分。代码需要通过跟踪到目前为止发送的字节数并将索引编入正在发送的缓冲区来满足此要求。