2017-10-01 7 views
0

System.Net.Socketを使用してC#でクライアント/サーバー図を使用して簡単な電卓を作成しようとしています。すべてが動作しますが、サーバー側では、クライアントから値を変換しようとすると、値は整数ではなく10進数に変換されますが、多くの時間をかけて解決しません。バイト配列は常に整数ではなく整数を返します。#

例、クライアント入力A = 5、B = 5値、サーバー側では、53と53

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 

namespace Server_Fix 
{ 
    class Program 
    { 
     private static int SendVarData(Socket s, byte[] data) 
     { 
      int total = 0; 
      int size = data.Length; 
      int dataleft = size; 
      int sent; 

      byte[] datasize = new byte[4]; 
      datasize = BitConverter.GetBytes(size); 
      sent = s.Send(datasize); 

      while (total < size) 
      { 
       sent = s.Send(data, total, dataleft, SocketFlags.None); 
       total += sent; 
       dataleft -= sent; 
      } 
      return total; 
     } 

     private static byte[] ReceiveVarData(Socket s) 
     { 
      int total = 0; 
      int recv; 
      byte[] datasize = new byte[4]; 

      recv = s.Receive(datasize, 0, 4, 0); 
      int size = BitConverter.ToInt32(datasize,0); 
      int dataleft = size; 
      byte[] data = new byte[size]; 


      while (total < size) 
      { 
       recv = s.Receive(data, total, dataleft, 0); 
       if (recv == 0) 
       { 
        data = Encoding.ASCII.GetBytes("exit "); 
        break; 
       } 
       total += recv; 
       dataleft -= recv; 
      } 
      return data; 
     } 


     public static void Main() 
     { 
      byte[] data = new byte[1024]; 
      byte[] data1 = new byte[1024]; 
      byte[] data2 = new byte[1024]; 
      byte[] data3 = new byte[1024]; 
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); 

      Socket newsock = new Socket(AddressFamily.InterNetwork, 
          SocketType.Stream, ProtocolType.Tcp); 

      newsock.Bind(ipep); 
      newsock.Listen(10); 
      Console.WriteLine("Waiting for a client..."); 

      Socket client = newsock.Accept(); 
      IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; 
      Console.WriteLine("Connected with {0} at port {1}", 
          newclient.Address, newclient.Port); 

      string welcome = "CALCULATOR CLIENT-SERVER DIAGRAM!"; 
      data = Encoding.ASCII.GetBytes(welcome); 
      int sent = SendVarData(client, data); 

      string phepToan; 
      int result=0; 
      int a = 0, b = 0; 
      while(true) 
      { 


       sent = SendVarData(client, Encoding.ASCII.GetBytes("Nhap vao so a: ")); 
       data1 = ReceiveVarData(client); 

       //Console.WriteLine("Client: " + Encoding.ASCII.GetString(data)); 
       sent = SendVarData(client, Encoding.ASCII.GetBytes("Nhap vao so b: ")); 
       data2 = ReceiveVarData(client); 
       //b = Convert.ToInt32(data2); 
       sent = SendVarData(client, Encoding.ASCII.GetBytes("Cho biet phep tinh can dung la | + | - | * |/|: ")); 
       data3 = ReceiveVarData(client); 
       phepToan = Encoding.ASCII.GetString(data3); 
       //a = Convert.ToString(Encoding.ASCII.GetString(data1)); 
       if (phepToan=="+") 
       { 
        foreach (byte byteValue in data1) 
        { 
         a = Convert.ToChar(byteValue); //It's always turn to Decimal values            
        } 
        foreach (byte byteValue in data2) 
        { 
         b = Convert.ToChar(byteValue); //It's always turn to Decimal values 
        } 
        result = a + b; 

        sent = SendVarData(client, Encoding.ASCII.GetBytes("Ket qua phep tinh: "+Convert.ToString(result))); 
       } 
       if (phepToan == "-") 
       { 

       } 
       if (phepToan == "*") 
       { 

       } 
       if (phepToan == "/") 
       { 

       } 
      } 
      Console.WriteLine("Disconnected from {0}", newclient.Address); 
      client.Close(); 
      newsock.Close(); 
      Console.ReadLine(); 
     } 
    } 
} 

へのターンの============== ==========================================クライアント側の コード

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 

namespace Client_Fix 
{ 
    class Program 
    { 
     private static int SendVarData(Socket s, byte[] data) 
     { 
      int total = 0; 
      int size = data.Length; 
      int dataleft = size; 
      int sent; 

      byte[] datasize = new byte[4]; 
      datasize = BitConverter.GetBytes(size); 
      sent = s.Send(datasize); 

      while (total < size) 
      { 
       sent = s.Send(data, total, dataleft, SocketFlags.None); 
       total += sent; 
       dataleft -= sent; 
      } 
      return total; 
     } 

     private static byte[] ReceiveVarData(Socket s) 
     { 
      int total = 0; 
      int recv; 
      byte[] datasize = new byte[4]; 

      recv = s.Receive(datasize, 0, 4, 0); 
      int size = BitConverter.ToInt32(datasize,0); 
      int dataleft = size; 
      byte[] data = new byte[size]; 

      while (total < size) 
      { 
       recv = s.Receive(data, total, dataleft, 0); 
       if (recv == 0) 
       { 
        data = Encoding.ASCII.GetBytes("exit "); 
        break; 
       } 
       total += recv; 
       dataleft -= recv; 
      } 
      return data; 
     } 

     public static void Main() 
     { 
      byte[] data = new byte[1024]; 
      int sent; 
      IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); 

      Socket server = new Socket(AddressFamily.InterNetwork, 
          SocketType.Stream, ProtocolType.Tcp); 

      try 
      { 
       server.Connect(ipep); 
      } 
      catch (SocketException e) 
      { 
       Console.WriteLine("Unable to connect to server."); 
       Console.WriteLine(e.ToString()); 
       return; 
      } 
      string input; 
      data = ReceiveVarData(server); 
      string stringData = Encoding.ASCII.GetString(data); 
      Console.WriteLine(stringData); 
      while (true) 
      { 

       data = ReceiveVarData(server);    
       Console.Write("Server: " + Encoding.ASCII.GetString(data)); 

       Console.WriteLine("You: " + input); 
       sent = SendVarData(server, Encoding.ASCII.GetBytes(input)); 

       data = ReceiveVarData(server); 
       Console.Write("Server: " + Encoding.ASCII.GetString(data)); 

       input = Console.ReadLine(); 
       //Console.SetCursorPosition(0, Console.CursorTop - 1); 
       Console.WriteLine("You: " + input); 
       sent = SendVarData(server, Encoding.ASCII.GetBytes(input)); 


       data = ReceiveVarData(server); 
       Console.Write("Server: " + Encoding.ASCII.GetString(data)); 

       input = Console.ReadLine(); 
       //Console.SetCursorPosition(0, Console.CursorTop - 1); 
       Console.WriteLine("You: " + input); 
       sent = SendVarData(server, Encoding.ASCII.GetBytes(input)); 

       data = ReceiveVarData(server); 
       Console.WriteLine("Server: " + Encoding.ASCII.GetString(data)); 
      } 

      Console.WriteLine("Disconnecting from server..."); 
      server.Shutdown(SocketShutdown.Both); 
      server.Close(); 
      Console.ReadLine(); 
     } 
    } 
} 
+3

これを[mcve]に減らし、クライアントコードとネットワーク経由で送信されるバイト数を表示してください。また、「整数ではない10進数」は、指定したコードでは意味をなさないことに注意してください。あなたのコードは決して 'decimal'型を使用しないので、あなたが意味することは本当に不明です。 –

+0

53文字 '5'のUnicodeコードポイント(旧ASCII)です。 あなたはあまりにも多くの部分をデコードする1つのステップをやっています。 – dlatikay

+0

ええええええええええええええええええ、私はまだこれをどのようなステップを見つける! –

答えて

0

のはbyteValueを想定してみましょうは、文字のコードポイント53である「5」。

その後

Convert.ToChar(byteValue) 

は大丈夫です53を与える、C#で文字が文字表内のその序である数値を、持っています。 、可能性が高いいくつかの憶測で再び、

var a = int.Parse(ASCIIEncoding.ASCII.GetString(new byte[] { bytevalue })); 

または::あなたの問題を解決するために

一つの方法は、このことでしょう。ここ

var a = int.Parse(ASCIIEncoding.ASCII.GetString(data1)); 

、文字桁の数値表現「5」 (53のように)ワイヤーを越えてくると、ASCIIテーブルで参照され、「5」が与えられ、次に必要な整数に解析されます。

しかし、コード全体の根本的な問題を解決するわけではありません。情報のビットをどのように符号化し、転送し、その後確実に信頼できる方法でデコードするかをより徹底的に計画する必要があります。

+0

ありがとう@dlatikay! –

関連する問題