views:

406

answers:

3

I have some kind of problem and I can't check this at home if its working or not. Here is the code

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net.Security;

class Program
{
private static IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
private static int port = 6000;
private static string data = null;

static void Main(string[] args)
{
    Thread thread = new Thread(new ThreadStart(receiveThread));
    thread.Start();
    Console.ReadKey();
}

public static void receiveThread()
{
    while (true)
    {
        TcpListener tcpListener = new TcpListener(ipAddress, port);
        tcpListener.Start();

        Console.WriteLine("Waiting for connection...");

        TcpClient tcpClient = tcpListener.AcceptTcpClient();

        Console.WriteLine("Connected with {0}", tcpClient.Client.RemoteEndPoint);

        while (!(tcpClient.Client.Poll(20, SelectMode.SelectRead)))
        {
            NetworkStream networkStream = tcpClient.GetStream();
            StreamReader streamReader = new StreamReader(networkStream);

            data = streamReader.ReadLine();

            if(data != null)
                Console.WriteLine("Received message: {0}", data);
        }
        Console.WriteLine("Dissconnected...\n");
        tcpListener.Stop();
    }
}

}

I have a simple program aswell to connect to this and then send a string with data. It works fine on localhost but there is a problem when Im trying to connect with a different coputer. I even turned off firewall in my pc and router, same I did on my friends laptop. Every time I have tried to connect, his computer refused connection. Maybe Im doing something wrong ? Ofcourse, the ipAddress is a local address now sice its only working with that at the moment. Any suggestions what to do ??

+3  A: 

You're listening on 127.0.0.1 which is the loopback address which is a special address that means 'this computer'. This means that you will only accept connections that are made on the same machine as the server is running on.

You need to listen on one (or more) of the server's real ip addresses.

Len Holgate
So I should use there "my internet" ip to listen for an incoming connections ?
Allek
You could use IPAddress.Any to listen on ALL of your interfaces or the address of one particular interface to listen JUST on that interface. If you need to listen on several of your interfaces but not all then you need one listener per address.
Len Holgate
+2  A: 

Your problem is that the setting an IP address explicitly when you initialize the TcpListener will only allow it to accept connections from that address. Therefore, putting in the local address of 127.0.0.1 will only accept connections originating from your PC.

The implementation you want to use is as follows:

TcpListener tcpListener = new TcpListener(IPAddress.Any, port);

This will allows connections from any IP address to connect to your program on the specified port.

Smallgods
+6  A: 

You need to set it to accept connections from any IP, there is an IPAddress overload function for this:

System.Net.IPAddress.Any

use it instead of 127.0.0.1 and it will fix your problem.

SLC