2011-02-07 25 views
0

我想建立一個聊天,基本上我使用了調用函數是什麼線程。
我能夠讀取服務器發送的內容,但我只能寫入一次。我試圖完成這一點,但不知道如何寫服務器每次服務器:
(考慮到我以前寫在控制檯應用程序的形式和服務器工作正常...即問題不是與服務器) 。Winforms,調用和有問題的按鈕

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     Button btn1 = new Button(); 
     btn1.Click += button1_Click; 
    } 
    StreamReader sr; 
    StreamWriter sw; 
    TcpClient connection; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     connection = new TcpClient("127.0.0.1", 5000); 
     sr = new StreamReader(connection.GetStream()); 
     sw = new StreamWriter(connection.GetStream()); 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

     Thread t2 = new Thread(Reader); 
     t2.Start(connection); 
    } 

    string msg; 
    public void Reader(object o) 
    { 

     TcpClient con = o as TcpClient; 
     if (con == null) 
      return; 
     while (true) 
     { 
      msg = sr.ReadLine(); 

      Invoke(new Action(Output)); 
     } 
    } 

    public void Output() 
    { 
     ChatScreen.Text = msg;//set the message on the screen 
    } 
    string textinput; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     textinput = InputLine.Text; 
     sw.WriteLine(textinput);// this thing, writes once, multiple clicks wont send a new line to the server :(..the problem is in this button 
     sw.Flush(); 

    } 


} 

我以爲做的是連接按鈕,這樣就可以做多次點擊..eg btn.Click()..或在運行的WriteLine與調用線程(但我的直覺說使按鈕單擊幾次將使程序工作

+0

以及有關這一主題,是否有任何異常情況發生(並打印到調試輸出窗口)? – Timbo 2011-02-07 10:51:09

+0

我將我的代碼重新設置爲一個月前的版本,現在它可以工作。當我關閉應用程序時,我在invoke方法中遇到異常。這是正常的嗎? – 2011-02-07 11:04:19

+0

我的另一個問題是我如何在winforms中創建私人消息。我想創建一些標籤/按鈕,它們會出現在側邊欄上,點擊它們,打開一個窗口,你只能與該會員聊天! – 2011-02-07 11:05:15

回答

1

當您關閉表單時,如果不是,當您嘗試執行調用時,您需要停止線程進程,它會失敗,因爲表單已處理,它可以不能用來執行調用,你可以重寫dispose方法來停止讀者線程,或者你可以使用onclose方法,或者你可以檢查讀者進程是否有可用的控制(它不會被處理)和如果它不可用完成讀取過程。

您應該防止讀取器進程也會多次啓動以避免錯誤,因此您需要在線程運行時禁用該按鈕。

編輯:

您可以使用類似下面的代碼來讀取多行,當你關閉窗體停止線程。

private bool mbIsRunning = true; 

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
     lock (this) 
     { 
     mbIsRunning= false; 
     } 
    } 

    private bool IsRunning 
    { 
     get 
     { 
      lock(this) 
      { 
       return mbIsRunning; 
      } 
     } 
    } 


    string msg; 
    public void Reader(object o) 
    { 

     TcpClient con = o as TcpClient; 
     if (con == null) 
      return; 
     while (IsRunning) 
     { 
      msg = reader.ReadLine(); 
      string line; 
      while((line = reader.ReadLine()) != null) 
      { 
       msg = msg + Enviroment.NewLine + line; 
      } 

      Invoke(new Action(Output)); 
     } 
    } 
+0

我也有一個問題,每條消息後開始一個新行,一旦它被髮送到我已經得到的richTextBox ..我試圖做\ n或\ r ,,但沒有幫助 – 2011-02-07 11:37:18

0

運行你的代碼,我得到了一堆錯誤 - 從TcpClient拋出一個異常,等等。然而,假設你沒有發佈你的所有代碼,我會建議嘗試......捕捉所有你的函數,然後在catch中找出斷點,看看問題是什麼。檢查異常 - 異常應該只在特殊情況下拋出 - 所以你的代碼應該真的沒有這樣做。

0

我做的concat我的服務器代碼:

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      TcpListener server = new TcpListener(IPAddress.Any, 5000); 
      server.Start(); 
      Console.WriteLine("Server started"); 
      string word = ""; 
      savedObject saved = new savedObject(); 

      while (true) 
      { 
       TcpClient connection = server.AcceptTcpClient(); 
       Console.WriteLine("connection accepted"); 
       ThreadPool.QueueUserWorkItem(saved.ProssecClient, connection); 
      } 
     } 
    } 
} 





class savedObject 
{ 
    Dictionary<string, StreamWriter> dic = new Dictionary<string, StreamWriter>(); 
    StreamReader[] sr1 = new StreamReader[100]; 
    StreamWriter[] sw1 = new StreamWriter[100]; 
    string[] name = new string[100]; 
    int m; 
    int a; 
    int g; 
    string word; 

    public string AllWords(string sit) 
    { 
     word += sit + " ";// here i concat them 
     return word; 
    } 


    public string word2() 
    { 
     return word; 
    } 



    public void ProssecClient(object o) 
    { 


     TcpClient connection = o as TcpClient; 
     if (connection == null) 
     { 
      return; 
     } 
     StreamReader sr = new StreamReader(connection.GetStream()); 
     StreamWriter sw = new StreamWriter(connection.GetStream()); 
     sr1[a++] = new StreamReader(connection.GetStream()); 
     sw1[m++] = new StreamWriter(connection.GetStream()); 
     string word2 = ""; 
     sw.WriteLine("Please, fill your name: "); 
     name[g++] = sr.ReadLine(); 

     if (name[g] != null && sw1[m] != null) 
     { 
      dic.Add(name[g], sw1[m]); 
     } 
     try 
     { 
      while (true) 
      { 
       int i = 0; 
       word2 = AllWords(sr.ReadLine()); 

       for (i = 0; i < 3; i++) 
       { 
        if (sw1[i] != null) 
        { 
         sw1[i].WriteLine(name[i] + ": " + word2);// here is the words that are sent.. 

         sw1[i].Flush(); 

        } 

       } 

      } 
     } 
     catch { Console.WriteLine("client left"); } 
    } 

}