Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中无环回的异步套接字_C#_Ip_Remote Server_Asyncsocket - Fatal编程技术网

C#中无环回的异步套接字

C#中无环回的异步套接字,c#,ip,remote-server,asyncsocket,C#,Ip,Remote Server,Asyncsocket,我应该如何在两台不同的电脑之间异步使用套接字?我的问题出现了,因为到目前为止,应用程序是在环回中运行的,在同一台PC上初始化客户端和服务器 但我不知道如何使用我拥有的相同异步方案在两台远程PC之间建立连接 这是我的听众: public class Escuchar { ManualResetEvent todoListo = new ManualResetEvent(false); public delegate void Recibido(Serializador r); publ

我应该如何在两台不同的电脑之间异步使用套接字?我的问题出现了,因为到目前为止,应用程序是在环回中运行的,在同一台PC上初始化客户端和服务器

但我不知道如何使用我拥有的相同异步方案在两台远程PC之间建立连接

这是我的听众:

public class Escuchar {
  ManualResetEvent todoListo = new ManualResetEvent(false);
  public delegate void Recibido(Serializador r);
  public event Recibido objetoRecibido;

  Socket escuchador;
  public void Comenzar(int puerto) {
    escuchador = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    escuchador.Bind(new IPEndPoint(IPAddress.Loopback, puerto));
    Console.WriteLine("\n\nServidor iniciado.");
    EsperarRespuesta();
  }

  public void EsperarRespuesta() {
    todoListo.Reset();
    escuchador.Listen(100);
    escuchador.BeginAccept(Aceptar, escuchador);
  }

  public void Aceptar(IAsyncResult resultado) {
    Serializador respuesta = new Serializador();
    respuesta.Socket = ((Socket) resultado.AsyncState).EndAccept(resultado);
    respuesta.Socket.BeginReceive(respuesta.buffer, 0, respuesta.buffer.Length, SocketFlags.None, Receive, respuesta);
  }

  public void Receive(IAsyncResult resultado) {
    Serializador respuesta = (Serializador) resultado.AsyncState;
    int read = respuesta.Socket.EndReceive(resultado);
    if (read > 0) {
      for (int i = 0; i < read; i++) {
        respuesta.TransmisionBuffer.Add(respuesta.buffer[i]);
      }
      if (read == respuesta.buffer.Length) {
        respuesta.Socket.BeginReceive(respuesta.buffer, 0, respuesta.buffer.Length, SocketFlags.None, Receive, respuesta);
        Console.WriteLine("\n\nNo encaja!");
      } else {
        Listo(respuesta);
      }
    } else {
      Listo(respuesta);
    }
  }

  public void Listo(Serializador respuesta) {
    Console.WriteLine("\n\nDeserializando respuesta...");
    Serializador deserializado = respuesta.Deserializar();
    objetoRecibido(deserializado);
    todoListo.Set();
  }
}

我知道我必须获取PC的IP地址,然后发送它,但我不知道如何应用它。

唯一需要的更改是使用对等方的IP地址,而不是
IPAddress。连接时进行环回
,因此

public void Comenzar(IPAdress ipAddress, int puerto) {
    this.puerto = puerto;
    Console.WriteLine("\n\nCliente esperando por una conexion...");
    todoListo.Reset();
    Socket emisor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    emisor.BeginConnect(new IPEndPoint(ipAddress, puerto), Conectar, emisor);
    todoListo.WaitOne();
  }

@Klaus_Güiter确定,并获取ip本地地址。有什么办法吗?如果您只有对等主机名,请使用
Dns.Resolve
。如果你想自动检测到对方,那就另当别论了。
public class Enviar {
  ManualResetEvent todoListo = new ManualResetEvent(false);
  Serializador respuesta = new Serializador();
  private int puerto;

  public void ConfigurarClase(Juego game, Jugador player) {
    respuesta.claseGuardarJuego.SetDatos(game, player);
  }

  public void Comenzar(int puerto) {
    this.puerto = puerto;
    Console.WriteLine("\n\nCliente esperando por una conexion...");
    todoListo.Reset();
    Socket emisor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    emisor.BeginConnect(new IPEndPoint(IPAddress.Loopback, puerto), Conectar, emisor);
    todoListo.WaitOne();
  }

  public void Conectar(IAsyncResult resultado) {
    try {
      respuesta.Socket = (Socket) resultado.AsyncState;
      respuesta.Socket.EndConnect(resultado);
      byte[] buffer = respuesta.Serializar();
      respuesta.Socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, Send, respuesta);
    } catch (SocketException) {
      Console.WriteLine("\n\nServidor caído, reintentando...");
      Thread.Sleep(1000);
      Comenzar(puerto);
    }
  }

  public void Send(IAsyncResult result) {
    Serializador respuesta = (Serializador) result.AsyncState;
    int size = respuesta.Socket.EndSend(result);
    Console.WriteLine("Enviando dato: " + respuesta.claseGuardarJuego.Jugador.NombreJugador);
    todoListo.Set();
  }
}
public void Comenzar(IPAdress ipAddress, int puerto) {
    this.puerto = puerto;
    Console.WriteLine("\n\nCliente esperando por una conexion...");
    todoListo.Reset();
    Socket emisor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    emisor.BeginConnect(new IPEndPoint(ipAddress, puerto), Conectar, emisor);
    todoListo.WaitOne();
  }