Reputation: 1
I have this problem in c# where if I make a loopback (127.0.0.1) tcp request, i can connect to the server on the same PC and send datas, but if i try to connect from another device on the same network it doesn't work. Tried also to change in the client the ip to the network one (192.168.43.58) but even if it is on the same device, it works only if the IP is a loopback one. Tried also to create a new network using my smartphone as router in case the other had some firewalls or something like that but same problem.
Client (the software is on the same device as the server and the ip is absolutely correct, the error code is this: 10061)
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("Messaggio? ");
String messaggio = Console.ReadLine();
string sRisultato = TCPUtils.getData(new TcpClient("192.168.43.58", 8888), messaggio);
Console.WriteLine(sRisultato);
Console.WriteLine();
}
}
}
Server
class Program
{
static void consoleLog(string msg)
{
msg = String.Format("[{0}] - {1}", DateTime.UtcNow.ToString(), msg);
Console.WriteLine(msg);
}
static void Main(string[] args)
{
String host = "127.0.0.1";
int portnum = 8888;
TcpListener server = new TcpListener(IPAddress.Parse(host), portnum);
consoleLog("------------------------------------------------");
consoleLog("Server Accept client on port: " + portnum.ToString());
consoleLog("------------------------------------------------");
server.Start();
while (true)
{
using (TcpClient client = server.AcceptTcpClient())
{
consoleLog("Connected! from " + client.Client.RemoteEndPoint.ToString());
//stuff
client.Close();
consoleLog("Closed ------------------------------------------");
}
}
}
}
Upvotes: 0
Views: 292
Reputation: 792
TL;DR
Try hosting your server on 0.0.0.0
.
Explanation:
The server is hosted on the loopback IP. If you check ipconfig
or ifconfig
depending on your OS, you would see multiple IPs assigned to your system. All those IPs belong to their physical or virtual interface on your system. eg:
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
inet 127.0.0.1 netmask 0xff000000
...
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
....
vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255
All of them behave as different doors to accept traffic. You can send traffic to individual interface and from individual interface without others knowing about it.
You are running your server on 127.0.0.1
or on the lo0
(loopback) interface. This means that traffic will only be accepted on the lo0
interface or the 127.0.0.1
IP address. If you host your server on 192.168.0.2
, then you will only be able to access the socket on that particular IP.
$ http-server -a 192.168.0.2
Starting up http-server, serving ./
Available on:
http://192.168.0.2:8080
Hit CTRL-C to stop the server
and then
$ nc -v 127.0.0.1 8080
nc: connectx to 127.0.0.1 port 8080 (tcp) failed: Connection refused
You can try the above experiment.
You can use a particular IP to listen or tell your server to listen on all interfaces that you have by using 0.0.0.0
. This has a special meaning.
$ http-server -a 0.0.0.0
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.0.2:8080
http://192.168.56.1:8080
Hit CTRL-C to stop the server
And then you can connect to any of them.
Upvotes: 1