Reputation: 989
I am trying to establish a TCP Client-Server connection between my locally connected computers (MacBook-BigSur is the running server while a Windows 10 laptop is acting as a client). For this, I'm using Visual Studio Code to run both applications as follows:
On macOS:
Console.WriteLine("Starting build agent...");
var listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 12345);
Console.WriteLine("Build agent Started ");
listener.Start();
while (true)
{
var fileName = $"s-{DateTime.Now.Ticks}.tar.gz";
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var output = File.Create(fileName))
{
Console.WriteLine("Client connected. Starting to receive the file...");
// read the file in chunks of 1KB
var buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
Console.WriteLine("Client connected. File received successfuly.");
Console.WriteLine(Execute($"tar -xvf '{fileName}'"));
}
}
On Windows:
var client = new TcpClient("192.168.0.109", 12345);
Byte[] data = File.ReadAllBytes(@"D:\shared\file.tar.gz");
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
Console.WriteLine("data sent.");
However, when the Windows client is trying to establish the connection, it fails saying:
System.Net.Internals.SocketExceptionFactory.ExtendedSocketException: 'No connection could be made because the target machine actively refused it. 192.168.0.109:12345'
Note that after running the server I can see Visual Studio is using the port 12345 by checking it using sudo lsof -i :123456
in Terminal. Besides, my Mac device is using the port 192.168.0.109
, the firewall is disabled and I can ping it using my Windows command prompt.
Upvotes: 1
Views: 962
Reputation: 42640
127.0.0.1
is localhost which means by definition the server is only reachable on the same host.
If you want the server to listen on all interfaces don't specify IPAddress.Any
respectively IPAddress.IPv6Any
or use 0.0.0.0.
Upvotes: 2