C#客户端/服务器聊天应用程序中的多线程问题

时间:2016-03-15 22:10:18

标签: c# multithreading tcplistener

  

我有一个使用C#的TCP服务器/客户端聊天程序。该服务器接收来自客户的消息并重新发送到目标。

     

当客户端连接到服务器时,服务器为他创建一个新线程。

     

我的问题是服务器只接收来自客户的一条消息,但没有收到任何来自他的新消息。

     

当我进行调试时,我注意到服务器收到消息并挂起在客户端线程中但不再运行。

服务器代码:

namespace server
{
public partial class Form1 : Form
{
    private ArrayList alSockets;
    private delegate void UpdateLogCallback(string strMessage);


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
        textBox1.Text = "my ip is " + IPHost.AddressList[0].ToString();
        alSockets = new ArrayList();
        Thread thdListener = new Thread(new ThreadStart(listenerThread));
        thdListener.Name = "thdListener";

        thdListener.Start();

    }
    public void listenerThread()
    {
        TcpListener tcpc = new TcpListener(IPAddress.Any,6666);
        tcpc.Start();

        while (true)
        {
            Socket HndSoc = tcpc.AcceptSocket();
            if (HndSoc.Connected)
            {
                string str2 = "\n" + HndSoc.RemoteEndPoint.ToString() + " connected";
                this.Invoke(new UpdateLogCallback(this.update2),new object[] {str2});

                lock (this)
                {
                    alSockets.Add(HndSoc);

                }
                ThreadStart thdstHandler = new ThreadStart(handlerThread);
                Thread thdHandler = new Thread(thdstHandler);
                thdHandler.Name = "thdHandler";
                thdHandler.Start();

             }
            else if(!HndSoc.Connected)
            {
                string str2 = "\n" + HndSoc.RemoteEndPoint.ToString() + " desconnected";
                this.Invoke(new UpdateLogCallback(this.update2), new object[] { str2 });

            }

            }
        }
    public void handlerThread()
    {
        byte [] rx = new byte [1024];
        Socket hanso = (Socket) alSockets[alSockets.Count-1];
       NetworkStream ns = new NetworkStream(hanso);
       // NetworkStream  ns = hanso. 
        ns.Read(rx,0,rx.Length);
   //     textBox1.Text = "\n";
     //   textBox1.Text =  Encoding.ASCII.GetString(rx);
        string str = Encoding.ASCII.GetString(rx);
       // update(str);
        this.Invoke(new UpdateLogCallback(this.update), new object[] { str });
       // ns.Close();
       // hanso = null;
        rx = new byte[1024];
        ns.Read(rx, 0, rx.Length);

    }

如果需要,这是客户端代码:

namespace simple_clint
{
public partial class Form1 : Form
{
    byte[] tx = new byte[1024];
  //  IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"),6666);

    TcpClient tcp1 = new TcpClient("127.0.0.1", 6666);


    public Form1()
    {
        InitializeComponent();
    }

    private void send_Click(object sender, EventArgs e)
    {
        tx = Encoding.ASCII.GetBytes(textBox1.Text);
        try
        {
            NetworkStream ns = tcp1.GetStream();
            ns.Write(tx, 0, tx.Length);
          //  ns.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

感谢kajacx。我需要在 handlerThread()中添加循环 新代码:

<?php
// Require the library
require_once('path/to/cryptolib.php');
// Generate a token of 16 char with the library by calling the randomString method.
$token = CryptoLib::randomString(16);
// Generate a salt with the library by calling the generateSalt method
$salt = CryptoLib::generateSalt();
// Hash the token with the salt that was generated
$hash = CryptoLib::hash($token, $salt);

// Salt and hash are then stored in the database.

// $hash and $salt are gotten later from the database, and the token is provided via a POST variable by the user
$isHashCorrect = CryptoLib::validateHash($hash, $_POST['token']);

// If isHashCorrect is true, the user has provided the correct token.
?>

我删除了最后两行