【发布时间】:2014-03-14 02:05:04
【问题描述】:
我正在尝试做聊天应用程序,我正在使用 tcp 套接字和线程问题是如何在同一个套接字上同时等待命令和发送命令
public void GetTextFather()
{
string Command = "";
string text = "";
while (Command == "")
Command = Functions.serverrecievetext(ip, port);
if (Command == "Text")
{
while (text == "")
text = Functions.serverrecievetext(ip, port);
if (text != "")
{
listBox1.Invoke((MethodInvoker)delegate { this.listBox1.Items.Add(name + ":" + text); });
Thread t = new Thread(GetTextFather);
t.Start();
}
}
if (Command == "Typing")
{
label1.Invoke((MethodInvoker)delegate { label1.Visible = true; });
Thread t = new Thread(GetTextFather);
t.Start();
}
if (Command == "NotTyping")
{
label1.Invoke((MethodInvoker)delegate { label1.Visible = false; });
Thread t = new Thread(GetTextFather);
t.Start();
}
}
发送按钮点击
private void button1_Click(object sender, EventArgs e)
{string text=textBox1.Text;
listBox1.Items.Add("You:" + text);
if (text.Length != 0)
{
if (!flag)
{Functions.ClientSendTextPortsixty("Text", port);
Functions.ClientSendTextPortsixty(text, port);
}
else
{Functions.ServerSendbyip("Text", ip, port);
Functions.ServerSendbyip(text, ip, port);
}
}
textBox1.Text = "";
}
函数 send 很简单,通过 socket 发送一个文本,receive 得到一个文本。 我的 GetTextSon() 与 GetTextFather 相同 如果您需要更多信息,请在下方评论
【问题讨论】: