2016-04-01 23 views
0

私は学校のプロジェクトで何かしています。サーバー(リスナー)とクライアントの両方の通信を可能にするC#アプリケーションを作成する必要があります。問題は、私はこれをやったことがありますが、それはまったく動作しません、それは修正するにはあまりにも多くのエラーをスローし、間違いなくひどくコード化されている、私は前にこの厄介な何かをしたことはありません。サーバーとクライアントの間の通信C#

私はまた、多くのクライアントが同時にサーバにアクセスできるようにしたいので、アクティブな人のクライアントリストです。私は1つの例をオンラインで見ましたが、達成しようとしていたタスクにとっては複雑すぎました。私は、クライアントとサーバーの両方に簡単なテキスト通信を行い、必要なときにいつでも情報を送信したいだけです。私はそれが情報が送信されるのを待っているwhileループで立ち往生したくないです&他のもの。

申し訳ありませんが、私はそれをひどく説明しました。ありがとう。

編集

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

using System.Windows.Forms; 
using System.Threading; 
using System.Net; 
using System.Net.Sockets; 

namespace CentralHubGUI 
{ 
    class chCon 
    { 



    static TcpClient clientCon; 
    static NetworkStream stream; 
    public static bool currentlyConnected; 

    static string buffer; 

    string ip = ""; 
    int port = 0; 

    Thread thread1 = new Thread(receiveData_Stage1); 
    //Thread thread2 = new Thread(); 
    //Thread thread3 = new Thread(); 

    public chCon(string ipAddress, int portNumber) 
    { 
     ip = ipAddress; 
     port = portNumber; 

     connectToConsole(); 
    } 

    public void connectToConsole()//mitigates the connection 
    { 
     if (ip.Trim() == "") 
      ip = "127.0.0.1"; 

     if (port == 0) 
      port = 2647; 

     try 
     { 
      clientCon = new TcpClient(ip, port); 
      stream = clientCon.GetStream(); 

      sendData("#101" + (char)13); //first bit of data is sent on accepted client 

      Thread retrieveData = new Thread(receiveData_Stage1); 
      retrieveData.Start(); 
      currentlyConnected = true; 

      thread1.Start(); //starting to read the server for information 
     } 

     catch(Exception e) // if the connection being naughty ;) 
     { 
      currentlyConnected = false; 
      MessageBox.Show("Exception caught:\n" + e); 
     } 

    } 

    public void disconnectFromConsole() 
    { 
     try 
     { 
      if (clientCon.Connected || clientCon.Connected == null) 
      { 
       thread1.Abort(); 

       byte[] msg = System.Text.Encoding.ASCII.GetBytes("#103"); 
       stream.Write(msg, 0, msg.Length); 
       stream.Close(); 
       clientCon.Close(); 

       currentlyConnected = false; 
      } 
      else 
      { 
       MessageBox.Show("Cannot disconnect from a connection that has not started."); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error thrown during disconnection - UNKNOWN"); 
     } 
    } 

    internal void sendData(string data)//sends data to the server 
    { 
     Byte[] dataConv = System.Text.Encoding.ASCII.GetBytes(data); 

     stream.Write(dataConv, 0, dataConv.Length); 

    } 

    public static void receiveData_Stage1() //builds a buffer through a loop 
    { 
     while (true) 
     { 
      Byte[] response = new Byte[256]; 
      string responseData = String.Empty; 

      // Read the first batch of the TcpServer response bytes. 
      Int32 bytes = stream.Read(response, 0, response.Length); 
      responseData = System.Text.Encoding.ASCII.GetString(response); 

      if(responseData.Trim() != "") 
       buffer = buffer + responseData; 
     } 
    } 

    public static string receiveData_Stage2() //requests the buffer through a return value string 
    { 
     string bufferTemp; 
     bufferTemp = buffer; 

     buffer = string.Empty; 

     return bufferTemp; 
    } 

    public void closeConnection() 
    { 
     stream.Close(); 
     clientCon.Close(); 
     thread1.Abort(); 
    } 

    } 
    /* 
    while (true) 
     { 
      if (Program.currentlyConnected == true) 
      { 
       Thread.Sleep(100); 
       buffer = Program.receiveData_Stage2(); 
       if (buffer != "") 
        richTxtBoxConsole.AppendText(buffer + "\n"); 
      } 
      else 
      { 
       guiConsoleWriteLine("Connection is not active."); 
       break; 
      } 

     } 
    */ 
} 
+0

にいくつかの良い記事があります。投稿した内容を投稿してください。そうでなければ広すぎる質問です。 – Divisadero

+0

この質問には深刻な作業が必要です。あなたが試したこととあなたが遭遇したエラー/問題が解決できないことを正確に示してください。 – nodots

+0

** A)**なぜデータを読み取るのに2つのスレッドを使用していますか?これは深刻な問題を引き起こす可能性があります'Thread1'または' retrieveData'を取り除いてください。 ** B)** '' BooleanConnection' == null'をチェックすることは、 '' Boolean''値がnullであることができないので役に立たない。 –

答えて

0

あなたは私は本当にあなたが欲しいものを見つけ出すが、私の推測からのそれはあなたが行きたいの技術に依存して言わせすることはできません説明したものから。 WCF Windows Communication Foundationを使用することをお勧めします。 .NET FoundationTutorial to get started

関連する問題