2016-04-01 157 views
0

我正在为我的学校项目做些事情,它需要我构建一个允许来自服务器(侦听器)和客户端的通信的C#应用​​程序。问题是,我已经这样做了,但它根本不起作用,它会抛出太多的错误来纠正,而且肯定是严重的编码,我从来没有做过这种草率的事情。服务器和客户端连接之间的通信C#

我还希望很多客户端能够同时访问服务器,因此客户端列表中的活动人员。我在网上看到一个例子,但对于我试图实现的任务而言,这太复杂了。我只想与客户端和服务器进行简单的文本通信,并在需要时发送信息。我不希望它卡在一个while循环等待信息发送& whatnot。

对不起,如果我不好解释它。谢谢。

编辑

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)**为什么您使用两个线程来读取数据?这可能会导致严重的问题。要么摆脱'Thread1'或'retrieveData'。 ** B)**检查'clientCon.Connected == null'是无用的,因为'Boolean'值不能为空。 –

回答

0

从你解释什么,我真的不能弄清楚你想要什么,但我从猜测让我们说这取决于你想去的技术。我会建议使用WCF Windows Communication Foundation。 你可以在.NET FoundationTutorial to get started

相关问题