简体   繁体   English

C ++套接字发送不发送CR和LF

[英]c++ socket send don't transmit CR and LF

Is there any solution to send CR (0x0D) and LF (0x0A) by socket? 有什么解决方案可以通过套接字发送CR(0x0D)和LF(0x0A)?
I've got some data like the char buffer below. 我有一些数据,例如下面的char缓冲区。 I want to have a solution where I'm able to send some binary data (range should be full 8Bit = 0x00 to 0xFF). 我想要一个能够发送一些二进制数据的解决方案(范围应为完整的8Bit = 0x00至0xFF)。
The socket "connectedSocket" is successfully set up. 套接字“ connectedSocket”已成功设置。

char bufSend[] = {0x01, 0x01, 0x0A, 0x04, 0x44, 0x0D, 0x12};
ret = send(connnectedSocket, bufSend, 7, 0);
if(SOCKET_ERROR == ret) {
    printf("Error: send, code: %d\n", WSAGetLastError());
    return 1;
}

I've got the return message: 10053 = WSAECONNABORTED 我收到了返回消息:10053 = WSAECONNABORTED

If I change the data to something without 0x0A and 0x0D there is no error. 如果我将数据更改为没有0x0A和0x0D的内容,则没有错误。

The client is programmed in java: 客户端使用Java编程:

if (socket.isConnected()) {
out = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(socket.getOutputStream())), true);

in = new BufferedReader(new InputStreamReader(
    socket.getInputStream()));

// start connection
out.println(CONNECTION_BEGIN);
inMessage = in.readLine();
System.out.println("data length: " + inMessage.length());

Write your Java client to read more than a line. 编写Java客户端以读取一行以上的内容。 The problem is that Java is closing the connection. 问题是Java正在关闭连接。

You aren't sending a line, you are sending binary data which happens to contain 0x0A and 0x0D, but not as line terminators. 您不是在发送行,而是在发送二进制数据,该数据恰好包含0x0A和0x0D,但不作为行终止符。 So don't read lines. 所以不要读行。 Use InputStream.read() . 使用InputStream.read()

But it's hard to believe that sending this really produced ECONNABORTED and that removing two bytes fixed it. 但是很难相信发送此消息确实可以产生ECONNABORTED,并且删除两个字节可以修复该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM