Hi all:

I have successfully opened a socket to a server and trying to send some data vai the send() function. But what surprised me is: () returns nothing, which means the data has been sent to the server, however all the data (a string) will only be displayed at the moment I disconnect from the server.
p.s. I do not have access to the server codes, which has been written by someone else in Java.

Any help is deeply appreciated.

Dani AI

Generated

Useful follow-up and troubleshooting notes based on the thread: this is a classic client/server framing issue rather than a send() bug. As observed, the server only processed the bytes once the connection closed; as suggested, checking send()'s return is the right way to detect errors. The root cause is usually the server waiting for a message boundary (a line terminator or EOF) before it treats buffered bytes as a complete message.

Why it happens: many Java server readers block until they see a line terminator or until the peer closes the socket (EOF). When the client closes the socket the server sees EOF and processes the buffered data. Also note that a successful send() only hands bytes to the OS send buffer — it does not guarantee the remote application has consumed them.

Practical fixes and checks:

  • Always check send()'s return and handle partial sends by looping until all bytes are sent. Use WSAGetLastError only when send() returns SOCKET_ERROR. For example:
int total = 0, n;
int bytesleft = len;
while (total < len) {
    n = send(sock, buf + total, bytesleft, 0);
    if (n == SOCKET_ERROR) {
        int err = WSAGetLastError();
        // handle error
        break;
    }
    total += n;
    bytesleft -= n;
}
  • Match the server's expected framing: terminate messages with the line-ending the server expects, or switch to a length-prefixed protocol so both sides know message boundaries.
  • If small writes are delayed, consider Nagle (TCP_NODELAY) and test with a simple client (telnet/netcat) or packet capture (Wireshark) to confirm what bytes hit the wire.

Closing the socket forces the server to process buffered data (not ideal for interactive protocols). Prefer explicit message framing so the server can process each message immediately without relying on disconnect.

Recommended Answers

All 2 Replies

If send is successful I think it returns the number of bytes sent at that time. YOu can use that and compare with the number of data bytes you wanted to send to see if an error occured or not. GetLastError or WSAGetLastError is only used to get detailed error information, and not for error checking.

Anyway you are saying that the string is displayed only when you disconnect from the server. Displayed where? in the Client( that is your software ) or in the Server ( Java Software)? If it is the Server software, try appending a NULL, '\0' character to your string and see. Maybe the server is waiting for a NULL character as the data termination to display it. Since disconnecting sends a NULL character it is displaying it at that point.

Hi there:

Thank you for your fast and helpful reply. I managed to get this problem solved. The solution is to add "\r" at the end of the data string to be sent over to the server.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.