I've made a snippet of code that sends a message via TCP / IP with sockets from one computer to another. It works well (sends the message) if the two computers are connected to the same network, however, if I connect one of the two computers to a different wifi does not work.
I do not know if the problem is that I must open ports or if I have to put an IP different from the local one of the computer.
The code snippet is as follows:
private void Envio()
{
//Ejemplo de IP
IPAddress IP = IPAddress.Parse("192.168.1.40");
TcpClient TCP = new TcpClient();
await TCP.ConnectAsync(IP, 443);
Stream Str = TCP.GetStream();
if (Str.CanWrite == true)
{
var Mensaje = Encoding.ASCII.GetBytes("Hola");
if (Str != null)
{
Str.Write(Mensaje, 0, Mensaje.Length);
}
}
TCP.Dispose();
}
private void Recepcion()
{
IPAddress IP = IPAddress.Parse("192.168.1.40");
TcpListener TCP = new TcpListener(IP, 443);
TCP.Start();
MensajeB[] buffer = new byte[256];
string Mensaje = null;
bool Seguimiento = true;
while (Seguimiento == true)
{
TcpClient TCPCl = await TCP.AcceptTcpClientAsync();
ResultPrueba.Text = "Conectado";
datos = null;
NetworkStream NetStr = TCPCl.GetStream();
int i;
while ((i = NetStr.Read(MensajeB, 0, MensajeB.Length)) != 0)
{
datos = Encoding.ASCII.GetString(MensajeB, 0, i);
ResultPrueba.Text = datos;
}
TCPCl.Dispose();
if (datos.Length > 0)
{
Seguimiento = false;
}
}
}