简体   繁体   中英

c++ socket send don't transmit CR and LF

Is there any solution to send CR (0x0D) and LF (0x0A) by socket?
I've got some data like the char buffer below. I want to have a solution where I'm able to send some binary data (range should be full 8Bit = 0x00 to 0xFF).
The socket "connectedSocket" is successfully set up.

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

If I change the data to something without 0x0A and 0x0D there is no error.

The client is programmed in 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. The problem is that Java is closing the connection.

You aren't sending a line, you are sending binary data which happens to contain 0x0A and 0x0D, but not as line terminators. So don't read lines. Use InputStream.read() .

But it's hard to believe that sending this really produced ECONNABORTED and that removing two bytes fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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