1

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 return 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

Can any one help me? Update: 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 1 PC by opening 2 instances from my application and using 2 different ports while firewall is off.

Thanks,

David
  • 640
  • 1
  • 7
  • 15
  • Possible duplicate of [TCP socket error 10061](http://stackoverflow.com/questions/9077606/tcp-socket-error-10061) – Ghasem Dec 19 '15 at 09:30
  • Also check [this question](http://stackoverflow.com/questions/9695224/no-connection-could-be-made-because-the-target-machine-actively-refused-it-127-0) – Ghasem Dec 19 '15 at 09:33
  • Also post the listener code. – Mihai Caracostea Dec 19 '15 at 09:59
  • @Mihai Caracostea Do I have to use a listener with TCP? Because I didn't use any for the UDP and it worked! – David Dec 19 '15 at 10:17
  • @David Yes, you have to use a listener for TCP and one peer should be the server and the other the client. That is because it uses connections. UDP is a connectionless protocol. That's why it worked without a listener. – Mihai Caracostea Dec 19 '15 at 10:22

1 Answers1

2

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.

Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46