Search code examples
c#c++winsock

C# Client halted while connecting with C++ Server, but works fine when connecting with C# Server


I have C# Client and C# Server Programs that connected with each other successfully and works fine. But when i want to connect C# Client with C++ server then C# client gets halted while C++ Server produces success messages of "winsock initialization success " and "creating socket success".

C++ Server Code

WSADATA wsaData;

struct sockaddr_in address_of_server;
struct sockaddr_in address_of_client;

int socket_of_client;
int size_of_address_of_client = sizeof(address_of_client);

if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0) {
    printf("winsock initialization success\n");
}
else {
    printf("winsock initialization failure\n");
}

SOCKET socket_of_server = socket(AF_INET, SOCK_STREAM, 0);

if (socket_of_server == -1) {
    printf("creating socket failure\n");
}
else {
    printf("creating socket success\n");
}

memset(&address_of_server, 0, sizeof(address_of_server));
address_of_server.sin_family = AF_INET;
//address_of_server.sin_family = PF_INET;
address_of_server.sin_addr.s_addr = htonl(INADDR_ANY);
address_of_server.sin_port = htons(8888);

bind(socket_of_server, (struct sockaddr*)&address_of_server, sizeof(address_of_server));

listen(socket_of_server, 5);

ClientSocket = accept(socket_of_server, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
    printf("accept failed with error: %d\n", WSAGetLastError());
    closesocket(socket_of_server);
    WSACleanup();
    return 1;
}

socket_of_client = accept(socket_of_server, (struct sockaddr*)&address_of_client, &size_of_address_of_client);

WSACleanup();

C# Client Code

public CCRMain()
{
    InitializeComponent();
    clientSocket.Connect("127.0.0.1", 8888);    
}
void Data()
{
    MainWindow mw = (MainWindow)Application.Current.MainWindow;
    NetworkStream serverStream = mw.clientSocket.GetStream();
    byte[] outStream = System.Text.Encoding.ASCII.GetBytes(A1G1.Text + "$");
    serverStream.Write(outStream, 0, outStream.Length);
    serverStream.Flush();

    byte[] inStream = new byte[10025];
    serverStream.Read(inStream, 0, (int)mw.clientSocket.ReceiveBufferSize);

    System.IO.MemoryStream ms = new System.IO.MemoryStream(inStream);
    System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
    int[] inComingData = new int[5];

    for (int i = 0; i < 5; i++)
    {
        inComingData[i] = br.ReadInt32();
        Debug.WriteLine(inComingData[i].ToString());
        A1G1Text.Text = inComingData[0].ToString();
    }
}   

Solution

  • NetworkStream.Read() is blocking. So as long as the server does not close the connection, this will not return until 10025 bytes have been read.

    You should directly read from the network stream, not copy it over twice (or even four times in your case: From network to inStream and then to a MemoryStream and then to inComingData and then to your text variable).

    Use the NetworkStream.ReadTimeout property to indicate that you want Read() to return even if not the entire buffer was filed within some time.