System.Net.Sockets.SocketException (0x80004005) when connecting C#-Client to C++ Server


I want to initiate a TCP connection between my Android smartphone and my Win10 computer. I have a C++ server running that can be easily connected with other C++ clients. My C# client on Xamarin throws the following exception:

{System.Net.Sockets.SocketException (0x80004005): Connection refused at
System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0 at
AndroidSocketClient.MainActivity.ConnectServer (System.Object sender, System.EventArgs e) [0x00039]

C++ Server

#include <string>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc, char* argv[])
    WSADATA wsa;
    SOCKET s, new_socket;
    struct sockaddr_in server, client;
    int c;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
        printf("Failed. Error Code : %d", WSAGetLastError());
        return 1;


    //Create a socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        printf("Could not create socket : %d", WSAGetLastError());

    printf("Socket created.\n");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(27015);

    if (bind(s, (struct sockaddr*) & server, sizeof(server)) == SOCKET_ERROR)
        printf("Bind failed with error code : %d", WSAGetLastError());

    puts("Bind done");

    //Listen to incoming connections
    listen(s, 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");

    c = sizeof(struct sockaddr_in);
    new_socket = accept(s, (struct sockaddr*) & client, &c);
    if (new_socket == INVALID_SOCKET)
        printf("accept failed with error code : %d", WSAGetLastError());

    puts("Connection accepted");

    //Reply to client
    const char* message = "Hello Client , I have received your connection. But I have to go now, bye\n";
    send(new_socket, message, strlen(message), 0);



    return 0;


public void ConnectServer(object sender, System.EventArgs e)
    byte[] data = new byte[10];

    IPHostEntry iphostInfo = Dns.GetHostEntry(Dns.GetHostName());
    IPAddress ipAdress = iphostInfo.AddressList[0];
    IPEndPoint ipEndpoint = new IPEndPoint(ipAdress, 27015);

    Socket client = new Socket(ipAdress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

        Console.WriteLine("Socket created to {0}", client.RemoteEndPoint.ToString());

        while (true)
            string message = "Test";
            byte[] sendmsg = Encoding.ASCII.GetBytes(message);
            int n = client.Send(sendmsg);
    catch (Exception ex)
        Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();

    Console.WriteLine("Transmission end.");

Tested Solutions:

I already tried turning off the firewall and permitted internet connection.

uses-permission android:name="android.permission.INTERNET"
asked on Stack Overflow Apr 8, 2019 by obszoenling • edited Apr 8, 2019 by GSerg

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0