0

C++ socket server receives a standard message from my C++ client without any issue. Now I'm trying to write the client in C# and it connects without any issue.

My problem comes in when I try to send a simple message from the C# client to the C++ server. Through a bit of research I think C++ is expecting a buffer that's 255 in length and it's just not outputting the message because it doesn't know how long the message is but I'm not sure how to communicate that using C#.

C++ Server reads data from each client on a separate thread as follows.

void Socket::thread (int newsockfd) {

    char buffer[256];
    int n;
    std::cout << "Waiting to read client messages\n";
    n = read(newsockfd,buffer,255);

    if (n < 0){
        std::cout << "Failed to read from socket\n";
        return;
    }

    printf("Received Message %s",buffer);

}

C# Client sends data to the server as follows.

try {

    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    socket.Connect(endpoint);

    NetworkStream stream = new NetworkStream(socket, true);

    if (stream.CanWrite){
        byte[] message = Encoding.UTF8.GetBytes("Hello World");

        stream.Write(message, 0, message.Length);

        Debug.Log("Wrote message to stream with " + message.Length.ToString() + " length.");
    }

    stream.Close();
    socket.Close();

} catch (SocketException e) {             
    Debug.Log("Socket exception: " + e);         
}

Everything works totally fine, the connection gets made successfully but no message ever goes through so I think that the message is probably being written successfully but C++ doesn't know when to stop reading.

For the record, here's how the C++ client sends its message (just a shorter excerpt).

char buffer[256];

bzero(buffer,256);
fgets(buffer,255,stdin);

n = write(sockfd,buffer,strlen(buffer));
if (n < 0) 
     std::cout << "Error writing to socket\n";

close(sockfd);

Which works totally fine and sends the message as expected. I'm really just having trouble with the juxtaposition of C++ and C# I guess. I'm happy to provide anymore information if needed :)

Zei
  • 409
  • 5
  • 14
  • c++ is sending a buffer of 256 bytes and the read function is waiting for 256 bytes. Send more data in c# – jdweng Mar 11 '18 at 08:44
  • Yeah I thought it'd be something like that. How would I go about making the message sent in C# 256 long? I'm not very familiar with C#, sorry. – Zei Mar 11 '18 at 08:48
  • You need to read this question: https://stackoverflow.com/q/666601/5311735. Your C++ client code now works just by accident, so you need to properly fix your server first. – Evk Mar 11 '18 at 08:51
  • byte[] buffer = new byte[256]; byte[] message = Encoding.UTF8.GetBytes("Hello World"); message.CopyTo(buffer, 0);stream.Write(message, 0, 256); – jdweng Mar 11 '18 at 08:51
  • @jdweng Okay so I've changed the code as you suggested although `stream.Write(buffer, 0, buffer.Length);`. Still no luck though, same behaviour. Might be something else wrong. I'll go have a look at Evk's link. – Zei Mar 11 '18 at 09:02
  • Tryout https://developers.google.com/protocol-buffers/, it is a great binary serializer for cross language projects – Frank Nielsen Mar 11 '18 at 09:24
  • @Evk That link was helpful. I ended up coming up with the following `byte[] buffer = new byte[256]; byte[] message = Encoding.UTF8.GetBytes("Hello World"); message.CopyTo(buffer, 0); Array.Clear(buffer, message.Length, (buffer.Length - message.Length)); stream.Write(buffer, 0, buffer.Length);` which does work as it's supposed to. From what I understand, C++ might have been waiting for a terminating 0 at the end of the buffer which I believe Array.Clear() is adding. Otherwise, I have no idea why but this process works. I think I should be able to figure out the rest now that I have output. – Zei Mar 11 '18 at 09:24
  • Hold up, I'm a total idiot. I figured it out. I actually added \n to the end of Hello World and C++ read is taking that as the final character I guess and outputting the text. I'm confused :( – Zei Mar 11 '18 at 09:32

0 Answers0