重新连接TCP / IP后,操作和调用不起作用

时间:2013-10-15 14:29:37

标签: c# winforms tcp action invoke

我正在使用TCP / IP开展聊天项目。当客户端连接时,他收到一条消息,但在断开连接并再次连接后,他没有收到消息。我检查并看到重新连接后Action方法和Invoke方法无法正常工作。你能救我吗?

这是客户端代码

namespace Client{
  public partial class frmClient : Form
  {
    private TcpClient tcpClient;
    private Comm comm;


    public frmClient()
    {
        InitializeComponent();
        txtNickName.Focus();
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {

        tcpClient = new TcpClient("127.0.0.1", 8200);
        MessageBox.Show("tcpClient = new TcpClient(127.0.0.1, 8200)");
        comm = new Comm();
        comm.GotMessage += new MessageHandler(comm_GotMessage);
        MessageBox.Show("comm.GotMessage += new MessageHandler(comm_GotMessage)");
       // comm.GotMessage += comm_GotMessage;
        comm.Connect(tcpClient.Client);
        MessageBox.Show("comm.Connect(tcpClient.Client)");
        disconnectToolStripMenuItem.Enabled = true;
        btnConnect.Enabled = false;
        grbChat.Enabled = true;
        grbNickName.Enabled = false;
        comm.Send(new ConnectMessage(txtNickName.Text));
        MessageBox.Show("comm.Send(new ConnectMessage(txtNickName.Text))");
        txtMessage.Focus();
        // lstUsers.Items.Add(txtNickName.Text);

    }

   *****This part is the one who doesn't work after reconnecting*****


    private void comm_GotMessage(object sender, MessageEventArgs e)
    {
        ***Action<BaseMessage> add = addMessage;
        MessageBox.Show(" Action<BaseMessage> add = addMessage");
        Invoke(add, e.BaseM);
        MessageBox.Show(" Invoke(add, e.BaseM)");***
        lstMessages.EnsureVisible(lstMessages.Items.Count - 1);

    }

    private void addMessage(BaseMessage baseM)
    {
        ListViewItem lvi = new ListViewItem();
        lvi.ForeColor = baseM.Color;
        lvi.Font = baseM.Font;
        lvi.Text = baseM.Message;
        lstMessages.Items.Add(lvi);
    }


    private void txtNickName_TextChanged(object sender, EventArgs e)
    {
        if (txtNickName != null)
            btnConnect.Enabled = true;

        return;
    }


    private void btnSend_Click(object sender, EventArgs e)
    {
        comm.Send(new ChatMessage(txtMessage.ForeColor,txtMessage.Font,txtNickName.Text,txtMessage.Text));
        MessageBox.Show("comm.Send(new ChatMessage(txtMessage.ForeColor,txtMessage.Font,txtNickName.Text,txtMessage.Text))");
        if(lstMessages.Items.Count>1)
        lstMessages.EnsureVisible(lstMessages.Items.Count - 1);


        txtMessage.Clear();
        txtMessage.Focus();
    }


    private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
        {
            comm.Send(new ChatMessage(txtMessage.ForeColor, txtMessage.Font, txtNickName.Text, txtMessage.Text));
            MessageBox.Show("comm.Send(new ChatMessage(txtMessage.ForeColor, txtMessage.Font, txtNickName.Text, txtMessage.Text))");
            lstMessages.EnsureVisible(lstMessages.Items.Count - 1);
            txtMessage.Clear();
            txtMessage.Focus();
        }
    }


    private void btnChangeColor_Click(object sender, EventArgs e)
    {
        DialogResult dr = colorDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            txtMessage.ForeColor = colorDialog1.Color;
            txtMessage.Focus();
        }
    }


    private void btnChangeFont_Click(object sender, EventArgs e)
    {
        DialogResult dr = fontDialog1.ShowDialog();
        if (dr == DialogResult.OK)
        {
            txtMessage.Font = fontDialog1.Font;
            txtMessage.Focus();
        }
    }


    private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
    {
        comm.Send(new ChatMessage(Color.DarkRed, txtMessage.Font, txtNickName.Text, " has been DISCONNECTED..."));
        MessageBox.Show("comm.Send(new ChatMessage(Color.DarkRed, txtMessage.Font, txtNickName.Text,  has been DISCONNECTED...))");
        txtNickName.Clear();
        lstUsers.Items.Clear();
        lstMessages.Items.Clear();
       // tcpClient.Client.Close();
        tcpClient.Close();
        MessageBox.Show("tcpClient.Close()");
        comm.Dispose();
        MessageBox.Show("comm.Dispose()");
        //tcpClient = null;
        btnConnect.Enabled = true;
        disconnectToolStripMenuItem.Enabled = false;
        grbChat.Enabled = false;
        grbNickName.Enabled = true;
        //LingerState.Enabled = true;
        //tcpClient.LingerState = new LingerOption(true, 10);


    }


    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }

0 个答案:

没有答案