David
David

Reputation: 660

Connection Refused with TCP socket Error # 10061

I'm creating a small chat (1-1) application to learn network programming and when creating the socket using TCP protocol, the Socket.Connect() always returns error 10061. However, if I make the socket UDP, I don't see the issue.

Here is my code:

myEndPoint = new IPEndPoint(IPAddress.Parse(_txtMyIPAdress.Text), int.Parse(_txtMyPort.Text));
TargetSideEndPoint = new IPEndPoint(IPAddress.Parse(_txtTargetIPAddress.Text), int.Parse(_txtTargetPort.Text));
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(myEndPoint);
mySocket.Connect(TargetSideEndPoint);
byte[] receivedBuffer = new byte[MAX_BUFFER_SIZE = 1024];
ReceivePackets(mySocket, receivedBuffer);//My function

I'm not using Listen(): the issue is when I call Connect(). I already tried multiple ports with the same issue and I'm currently testing on one PC by opening two instances from my application and using two different ports while firewall is off.

Upvotes: 2

Views: 8922

Answers (1)

Mihai Caracostea
Mihai Caracostea

Reputation: 8466

For TCP, which is a connection oriented protocol, you would need a server and a client.

The server must listen for incoming connections and the client should initiate the connection. Then, the client will be able to communicate with the server and exchange data.

Here is a simple working example:

void Main()
{
    Console.WriteLine("Starting listener thread");
    Thread serverThread = new Thread(ListenerThreadProc);
    serverThread.Start();

    Console.WriteLine("Waiting 500 milliseconds to allow listener to start");
    Thread.Sleep(500);

    Console.WriteLine("Client: Connecting to server");
    TcpClient client = new TcpClient();
    client.Connect(IPAddress.Loopback, 12345);
    Console.WriteLine("Client: Connected to server");

    byte[] buffer = new byte[5];
    Console.WriteLine("Client: Receiving data");
    using (NetworkStream clientStream = client.GetStream())
        clientStream.Read(buffer, 0, buffer.Length);

    Console.WriteLine("Client: Received data: " + buffer.Aggregate("", (s, b) => s += " " + b.ToString()));     
}

void ListenerThreadProc()
{   
    TcpListener listener = new TcpListener(IPAddress.Any, 12345);
    listener.Start();
    Console.WriteLine("Server: Listener started");

    Console.WriteLine("Server: Waiting for client to connect");
    TcpClient client = listener.AcceptTcpClient();
    Console.WriteLine("Server: Client connected");

    listener.Stop();    
    Console.WriteLine("Server: Listener stopped");

    Console.WriteLine("Server: Sending data");
    byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };   
    using (NetworkStream clientStream = client.GetStream())
        clientStream.Write(buffer, 0, buffer.Length);
    Console.WriteLine("Server: Sent data");
}

In this example, made as simple as it gets, you have a server accepting a single client to which it sends some data. The client connects, reads the data and then displays it.

A real-life server would spin new threads (or tasks, for async model) to serve the client's request as soon as the client would connect and carry on listening for new connections.

Upvotes: 2

Related Questions