2017-05-01 13 views
0

私はコンソールからテキストをサーバーに送信し、サーバーはそれを私に送信します。サーバーからクライアントにデータを取得できないのはなぜですか?

 static void Main(string[] args) 
    { 
     Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
     IPEndPoint ipserv = new IPEndPoint(IPAddress.Parse("46.174.49.48"), 46666); 
     client.Connect(ipserv); 
     byte[] data = Encoding.UTF8.GetBytes(Console.ReadLine()); 
     client.Send(data); 
     client.Receive(data); //here is the error 
     Console.WriteLine(Encoding.UTF8.GetString(data)); 
     Console.ReadLine(); 
    } 

エラー:リモートホストが強制的にあなたがよりよく理解するために、のtry-catch文を試してみてください

+0

問題を再現できるコードを投稿する必要があります。サーバ用のコードを提供するか、サーバがサードパーティに属している場合は、通信しようとしているサーバの詳細を – Mick

答えて

0

作業既存の接続

サーバーを破りました。 1)不正な形式のデータをアプリケーションに送信している、または(2)何らかの理由でクライアントとサーバー間のネットワークリンクがダウンしている...(3) (4)サードパーティのアプリケーションがシステムリソースを使い果たしてしまった可能性があります。まず、数字が1であると言います。

例:::

try { 
      sender.Connect(remoteEP); 

      Console.WriteLine("Socket connected to {0}", 
       sender.RemoteEndPoint.ToString()); 

      // Encode the data string into a byte array. 
      byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); 

      // Send the data through the socket. 
      int bytesSent = sender.Send(msg); 

      // Receive the response from the remote device. 
      int bytesRec = sender.Receive(bytes); 
      Console.WriteLine("Echoed test = {0}", 
       Encoding.ASCII.GetString(bytes,0,bytesRec)); 

      // Release the socket. 
      sender.Shutdown(SocketShutdown.Both); 
      sender.Close(); 

     } catch (ArgumentNullException ane) { 
      Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); 
     } catch (SocketException se) { 
      Console.WriteLine("SocketException : {0}",se.ToString()); 
     } catch (Exception e) { 
      Console.WriteLine("Unexpected exception : {0}", e.ToString()); 
     } 

    } catch (Exception e) { 
     Console.WriteLine(e.ToString()); 
    } 
+0

に提供する必要があります。サーバーコードにエラーがありました –

関連する問題