2017-04-01 78 views
0

、私はプロジェクトを再構築してきましたが、SimpleTcpClient client;のためにそれは言う:TCP/IPクライアントとサーバーの基本的な使用および参照

抑制状態をエラーCS0246型または名前空間名 は「SimpleTcpServer」(あなたが使用して ディレクティブまたはアセンブリ参照が不足している?)

ないことの原因となることができるかわから見つかりませんでした、多分私はいくつかが参照逃しましたまたは使用することができます。

クライアント:

using System; 
using System.Text; 
using System.Windows.Forms; 

namespace Client 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     SimpleTcpClient client; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      button1.Enabled = false; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      client.WriteLineAndGetReplay(txtMessage.Text, TimeSpan.FromSeconds(5)); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      client = new SimpleTcpClient(); 
      client.StringEncoder = Encoding.UTF8; 
      client.DataRecevived += Client_DataReceived; 
     } 

     private void Client_DataReceived(object sender, SimpleTCP.Message e) 
     { 
      textBox1.Invoke((MethodInvoker)delegate() 
      { 
       textBox1.Text += e.MessageString; 
      }); 
     } 
    } 
} 

サーバー:あなたのコード内using SimpleTCP;が含まれていないため

using System; 
using System.Text; 
using System.Windows.Forms; 

namespace Server 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     SimpleTcpServer server; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      server = new SimpleTcpServer(); 
      server.Delimeter = 0x13; 
      server.StringEncoder = Encoding.UTF8; 
      server.DataReceived += Server_DataReceived; 
     } 

     private void Server_DataReceived(object sender, SimpleTCP.Message e) 
     { 
      txtStatus.Invoke((MethodInvoker)delegate() 
      { 
       txtStatus.Text += e.MessageString; 
       e.ReplyLine(string.Format("You said: {0}", e.MessageString)); 
      });   
     } 

     private void btnStart_Click(object sender, EventArgs e) 
     { 
      txtStatus.Text += "Server starting..."; 
      System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(txtHost.Text)); 
      server.Start(ip, Convert.ToInt32(txtPort.Text)); 
     } 

     private void btnStop_Click(object sender, EventArgs e) 
     { 
      if (server.IsStarted) 
      { 
       server.Stop(); 
      } 
     } 
    } 
} 
+1

あなたのコードは私の側で正常に動作します。完全なコードを含めるように質問を更新してもよろしいですか? –

+2

.NETにはすでに完全に優れた 'TcpListener'があります。ナゲットは必要ありません。ちょっとsayin ' – MickyD

+0

@Reousa Asteronこんにちは、私は – nikorio

答えて

0

は、あなたの質問に答えるために、あなたはこのエラーを取得します。

フォームの各の開始は、次のようになります。それに

using SimpleTCP; 
using System; 
using System.Net; 
using System.Windows.Forms; 

と、コードはまだいくつかの問題を持っているでしょう。

  1. お客様のクライアントフォームにclient.Connect(IP, PORT);はありません。 server < > client接続を作成するには、接続するリスニングサーバーとクライアントが必要です。あなたのケースでは、唯一のリッスンサーバーがあります。

  2. serverフォームでは、代わりにIPAddress.Parse()に電話する必要があります。使用例:IPAddress.Parse("127.0.0.1");

  3. あなたはマルチスレッドの任意の並べ替えを使用していないようSimpleTCPは非常に低いです。デフォルトでそうしない限り、あなたは、あなたのFormメンバーのいずれかをInvokeする必要はありません。

さらに、プロジェクト全体が混乱しているようです。あなたがまだ始まっているのであれば、彼らはより容認して使いやすいので、Console Applicationを使うことを強くお勧めします。

C# eventsの詳細と、それらの動作についてもお勧めします。

server < > clientプロジェクトはSimpleTCP、ご使用の構成はConsole Applicationです。

サーバー:

using System; 
using System.Net; 
using SimpleTCP; 
using System.Text; 

namespace server 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SimpleTcpServer server = new SimpleTcpServer(); //Instantiate server. 
      server.Delimiter = 0x13; //Config 
      server.StringEncoder = Encoding.UTF8; //Config 
      server.DataReceived += Server_DataReceived; //Subscribe to DataRecieved event. 
      IPAddress ip = IPAddress.Parse("127.0.0.1"); //IP Address using .Parse() 
      int port = 6500; //Port number 
      server.Start(ip, port); //Start listening to incoming connections and data. 
      Console.ReadKey(); //Pause the console. 
     } 

     static void Server_DataReceived(object sender, SimpleTCP.Message m) 
     { 
      Console.WriteLine(m.MessageString); //Write message to console. 
      string replyMessage = String.Format("You said {0}", m.MessageString); //This is the reply message 
      m.ReplyLine(replyMessage); //Send reply message back to client. 
     } 
    } 
} 

クライアント:

using System; 
using System.Net; 
using SimpleTCP; 
using System.Text; 

namespace client 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SimpleTcpClient client = new SimpleTcpClient(); //Instantiate the client 
      client.StringEncoder = Encoding.UTF8; //Config 
      client.DataReceived += Client_DataReceived; //Subscribe to the DataRecieved event. 
      client.Connect("127.0.0.1", 6500); //Connect to the server 
      while(true) //Keep the console open until you close it. 
      { 
       //As long as the console is open, every message you type will be sent to the server. 
       client.WriteLineAndGetReply(Console.ReadLine(), TimeSpan.FromSeconds(5)); 
      } 
     } 
     static void Client_DataReceived(object sender, Message m) 
     { 
      Console.WriteLine(m.MessageString); //Write recieved reply from server to console. 
     } 
    } 
} 

私はこれがあなたの旅にあなたを助け願って、グッドラック!

+0

私は何かを見逃している場合教えてください! –

+0

こんにちは、あなたのコードはうまく動作し、それは私の質問に答えているようですが、この答えは、 'System.Net.Sockets.SocketException'を避けるために良いだろう、サーバーがオフであり、クライアントtryie接続すると、例外がスローされます – nikorio

+0

@nikorioこのような場合には、「例外処理」と呼ばれるものがあります。私はコードをできるだけシンプルにしようとしましたが、あなたが合っているようにコードを拡張することができます。 「例外処理」についてはhttps://msdn.microsoft.com/en-us/library/ms173162.aspxを参照してください。学ぶべきことがたくさんあります。読んでください!がんばろう :) –

関連する問題