2016-06-23 71 views
0

UdpClient.sendを使用してローカルホスト上の別のプログラム(ポート7777経由)にメッセージを送信する完璧に機能するコンソールプログラムがありました。 (これは奇妙なことに、このC#スクリプトとほとんど同じバージョンですが、unity3dで動作していて、同じコードで問題なく受け取ります)。.NET UDPClient:エラー:リモートホストによって既存の接続が強制的にクローズされました

私はそのプログラムからの返信を取得する必要があります。私はポート7778でメッセージを待ち受けるスレッド(下を見てください)を追加しました。しかし、それを言って起動したとき、私はエラーを取得しています:

An existing connection was forcibly closed by the remote host

using Newtonsoft.Json; 
using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 

namespace Blaster 
{ 
    class Blaster 
    { 
     UdpClient client; 
     IPEndPoint outPoint; 
     IPEndPoint inPoint; 
     public int oPort = 7777; 
     public int iPort = 7778; 
     public string hostName = "localhost"; 
     public int stepNum = 0; 
     const int rate = 1000; 
     public System.Timers.Timer clock; 
     Thread listener; 

     static void Main(string[] args) 
     { 
      Blaster b = new Blaster(); 
      b.run(); 
     } 
     Blaster() 
     { 
      client = new UdpClient(); 
      outPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], oPort); 
      inPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], iPort); 
     } 
     void run() 
     { 
      this.stepNum = 0; 
      listener = new Thread(new ThreadStart(translater)); 
      listener.IsBackground = true; 
      listener.Start(); 
      Console.WriteLine("Press Enter to do a send loop...\n"); 
      Console.ReadLine(); 
      Console.WriteLine("started at {0:HH:mm:ss.fff}", DateTime.Now); 
      start(); 
      Console.WriteLine("Press Enter to stop"); 
      Console.ReadLine(); 
      stop(); 
      client.Close(); 
     } 
     void stop() 
     { 
      clock.Stop(); 
      clock.Dispose(); 
     } 
     void start() 
     { 
      clock = new System.Timers.Timer(rate); 
      clock.Elapsed += send; 
      clock.AutoReset = true; 
      clock.Enabled = true; 
     } 
     void send(Object source, System.Timers.ElapsedEventArgs e) 
     { 
      Console.WriteLine("sending: {0}", stepNum); 
      Byte[] sendBytes = Encoding.ASCII.GetBytes(message()); 
      try 
      { 
       client.Send(sendBytes, sendBytes.Length, outPoint); 
      } 
      catch (Exception err) 
      { 
       Console.WriteLine(err.ToString()); 
      } 
     } 
     string message() 
     { 
      Packet p = new Packet(); 
      p.id = "car"; 
      p.start = DateTime.Now; 
      p.x = 1.2f; 
      p.y = 0.4f; 
      p.z = 4.5f; 
      p.step = stepNum++; 
      string json = JsonConvert.SerializeObject(p); 
      return json; 
     } 
     void translater() 
     { 
      Byte[] data = new byte[0]; 
      client.Client.Bind(inPoint); 
      while (true) 
      { 
       try 
       { 
        data = client.Receive(ref inPoint); 
       } 
       catch (Exception err) 
       { 
        Console.WriteLine("Blaster.translater: recieve data error: " + err.Message); 
        client.Close(); 
        return; 
       } 
       string json = Encoding.ASCII.GetString(data); 
       Console.WriteLine(json); 
       Packet p = JsonConvert.DeserializeObject<Packet>(json); 
      } 
     } 
    } 
} 
+1

送信と受信の操作で同じ 'client'オブジェクトを使用することを意味していますか? – pay

+0

あなたは別の変数 'UdpClient client2;を作成しようとしましたが、それをあなたのReceiving ..で使用しましたか? – MethodMan

+0

私は1人のクライアントでこれをやっている人々の例を見ました。しかし、はい、別のサーバーで動作します。 –

答えて

0

[OK]を、私は送信と受信の両方のための単一のクライアントオブジェクト(だけでなく、同じポート)を使用して、人々のいくつかの例を見ていました。しかし、同じホストにいれば別のポートが必要であることがわかりました。これで別のudpClientが必要になることがわかりました。

using Newtonsoft.Json; 
using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 

namespace Blaster 
{ 
    class Blaster 
    { 
     UdpClient client; 
     IPEndPoint outPoint; 
     IPEndPoint inPoint; 
     public int oPort = 7777; 
     public int iPort = 7778; 
     public string hostName = "localhost"; 
     public int stepNum = 0; 
     const int rate = 1000; 
     public System.Timers.Timer clock; 
     Thread listener; 

     static void Main(string[] args) 
     { 
      Blaster b = new Blaster(); 
      b.run(); 
     } 
     Blaster() 
     { 
      client = new UdpClient(); 
      outPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], oPort); 
      inPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], iPort); 
     } 
     void run() 
     { 
      this.stepNum = 0; 
      listener = new Thread(new ThreadStart(translater)); 
      listener.IsBackground = true; 
      listener.Start(); 
      Console.WriteLine("Press Enter to do a send loop...\n"); 
      Console.ReadLine(); 
      Console.WriteLine("started at {0:HH:mm:ss.fff}", DateTime.Now); 
      start(); 
      Console.WriteLine("Press Enter to stop"); 
      Console.ReadLine(); 
      stop(); 
      client.Close(); 
     } 
     void stop() 
     { 
      clock.Stop(); 
      clock.Dispose(); 
     } 
     void start() 
     { 
      clock = new System.Timers.Timer(rate); 
      clock.Elapsed += send; 
      clock.AutoReset = true; 
      clock.Enabled = true; 
     } 
     void send(Object source, System.Timers.ElapsedEventArgs e) 
     { 
      Console.WriteLine("sending: {0}", stepNum); 
      Byte[] sendBytes = Encoding.ASCII.GetBytes(message()); 
      try 
      { 
       client.Send(sendBytes, sendBytes.Length, outPoint); 
      } 
      catch (Exception err) 
      { 
       Console.WriteLine(err.ToString()); 
      } 
     } 
     string message() 
     { 
      Packet p = new Packet(); 
      p.id = "car"; 
      p.start = DateTime.Now; 
      p.x = 1.2f; 
      p.y = 0.4f; 
      p.z = 4.5f; 
      p.step = stepNum++; 
      string json = JsonConvert.SerializeObject(p); 
      return json; 
     } 
     void translater() 
     { 
      UdpClient server = new UdpClient(); 
      Byte[] data = new byte[0]; 
      server.Client.Bind(inPoint); 
      while (true) 
      { 
       try 
       { 
        data = server.Receive(ref inPoint); 
       } 
       catch (Exception err) 
       { 
        Console.WriteLine("Blaster.translater: recieve data error: " + err.Message); 
        client.Close(); 
        return; 
       } 
       string json = Encoding.ASCII.GetString(data); 
       Console.WriteLine(json); 
       Packet p = JsonConvert.DeserializeObject<Packet>(json); 
      } 
     } 
    } 
} 
関連する問題