用C#编写一简单聊天程序
应用的理论知识:Socket,流操作,Tcp/Udp,单播,多播采用TcpClient类或UdpClient类一对一,一对多...
应用的理论知识:Socket,流操作,Tcp/Udp,单播,多播采用TcpClient类或UdpClient类一对一,一对多
展开
2个回答
2013-10-29
展开全部
客户端:namespace chatClient
{
public partial class Client : Form
{
delegate void SetTextCallback(string text);
private System.Windows.Forms.TextBox inputTextBox;
private System.Windows.Forms.TextBox displayTextBox;
private NetworkStream output;
private BinaryWriter writer;
private BinaryReader reader; private string message = "";
private Thread readThread;
private System.ComponentModel.Container components = null; public Client()
{
InitializeComponent(); readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();
} //[STAThread] //static void Main()
//{ Application.Run(new Client()); } protected void Client_Closing(object sender, CancelEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
} private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
writer.Write("客户:" + inputTextBox.Text);
this.displayTextBox.Text += "\r\n客户:" + this.inputTextBox.Text; this.inputTextBox.Text = ""; }//end if }//end try
catch (SocketException ioe)
{
displayTextBox.Text += "\n写入文件错误。";
}
}//end inputTextBox_KeyDown public void RunClient()
{
TcpClient client; try
{
this.SetText("正在连接\r\n");
client = new TcpClient();
client.Connect("localhost", 5000); output = client.GetStream(); writer = new BinaryWriter(output);
reader = new BinaryReader(output); //displayTextBox.Text += ;
this.SetText("\r\n 获取输入、输出流\r\n");
inputTextBox.ReadOnly = false; do
{
try
{
message = reader.ReadString();
this.SetText("\r\n" + message);
}
catch (Exception)
{ System.Environment.Exit(System.Environment.ExitCode); } } while (message != "服务器>>>terminate"); this.SetText("\r\n关闭连接。\r\n");
writer.Close();
reader.Close();
output.Close();
client.Close();
Application.Exit(); }//end try
catch (Exception err)
{ MessageBox.Show(err.ToString()); } }//end method private void SetText(string text)
{
// InvokeRequired需要比较调用线程ID和创建线程ID
// 如果它们不相同则返回true
if (this.displayTextBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.displayTextBox.Text += text;
}
}
}//end class
}
{
public partial class Client : Form
{
delegate void SetTextCallback(string text);
private System.Windows.Forms.TextBox inputTextBox;
private System.Windows.Forms.TextBox displayTextBox;
private NetworkStream output;
private BinaryWriter writer;
private BinaryReader reader; private string message = "";
private Thread readThread;
private System.ComponentModel.Container components = null; public Client()
{
InitializeComponent(); readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();
} //[STAThread] //static void Main()
//{ Application.Run(new Client()); } protected void Client_Closing(object sender, CancelEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
} private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
writer.Write("客户:" + inputTextBox.Text);
this.displayTextBox.Text += "\r\n客户:" + this.inputTextBox.Text; this.inputTextBox.Text = ""; }//end if }//end try
catch (SocketException ioe)
{
displayTextBox.Text += "\n写入文件错误。";
}
}//end inputTextBox_KeyDown public void RunClient()
{
TcpClient client; try
{
this.SetText("正在连接\r\n");
client = new TcpClient();
client.Connect("localhost", 5000); output = client.GetStream(); writer = new BinaryWriter(output);
reader = new BinaryReader(output); //displayTextBox.Text += ;
this.SetText("\r\n 获取输入、输出流\r\n");
inputTextBox.ReadOnly = false; do
{
try
{
message = reader.ReadString();
this.SetText("\r\n" + message);
}
catch (Exception)
{ System.Environment.Exit(System.Environment.ExitCode); } } while (message != "服务器>>>terminate"); this.SetText("\r\n关闭连接。\r\n");
writer.Close();
reader.Close();
output.Close();
client.Close();
Application.Exit(); }//end try
catch (Exception err)
{ MessageBox.Show(err.ToString()); } }//end method private void SetText(string text)
{
// InvokeRequired需要比较调用线程ID和创建线程ID
// 如果它们不相同则返回true
if (this.displayTextBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.displayTextBox.Text += text;
}
}
}//end class
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-29
展开全部
五分一个全套的程序,c#什么时候这么不值钱了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询