C# client communicating with C++ server - server can read from socket but writing is unsuccessful
<p>I have a problem with my two programs - the C# client can successfully write data to the C++ client using a socket, but reading data from the server is unsuccessful.</p>

<p>Here's my C++ code:</p>

<pre><code>int sock = socket(PF_INET, SOCK_STREAM, 0);

sockaddr_in sockad;
// sin_family, port etc defined here

bind(sock, (sockaddr*)&amp;sockad, sizeof(sockad));

listen(sock, 15);

char buf;
stringstream ss;

while(running) {

    socklen_t sockl = sizeof(sockad);
    int sockd = accept(sock, (sockaddr*)&amp;sockad, &amp;sockl);

    while(recv(sockd, &amp;buf, 1, 0) &gt; 0) {
        ss &lt;&lt; buf;
    }

    // THIS is printed
    cout &lt;&lt; ss.str() &lt;&lt; endl;

    string response = "asdgfh";

    // THIS never reaches the C# client
    write(sockd, response.c_str(), strlen(response.c_str()));

    close(sockd);
}

close(sock);
</code></pre>

<p>And here's my C# code:</p>

<pre><code>string message = "message";
Int32 port = 4345;
TcpClient client = new TcpClient("server.address.com", port);
NetworkStream stream = client.getStream();

Byte[] data = new Byte[256];
data = Encoding.ASCII.GetBytes(message);

// THIS SUCCEEDS
stream.Write(data, 0, data.Length);

// THIS FAILS, it also freezes the other program
Int32 bytes = stream.Read(data, 0, data.Length);

responseData = Encoding.ASCII.GetString(data, 0, bytes);

stream.Close();
client.Close();
</code></pre>

<p>So what's wrong with my code, or is this a server configuration issue or something like that?</p>