c#网络编程问题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatClient
{
public partial class Form1 : Form
{
private bool isExit = false;
private delegate void SetListBoxCallBack(string str);
private SetListBoxCallBack setlistboxcallback;
private delegate void SetRichTextBoxReceiveCallBack(string str);
private SetRichTextBoxReceiveCallBack setRichTextBoxReceiveCallBack;
private TcpClient client;
private NetworkStream ns;
private ManualResetEvent allDone = new ManualResetEvent(false);
public Form1()
{
InitializeComponent();
setlistboxcallback = new SetListBoxCallBack(SetListBox);
setRichTextBoxReceiveCallBack = new SetRichTextBoxReceiveCallBack(SetRichTextBoxReceive);
}
private void button1_Click(object sender, EventArgs e)
{
//声明客户端的对象
client = new TcpClient(AddressFamily.InterNetwork);
IPAddress[] serverIP = Dns.GetHostAddresses(Dns.GetHostName());
//采用的是异步的方式
AsyncCallback requestCallBack = new AsyncCallback(RequestCallBack);//回调函数RequestCallBack
allDone.Reset();
//开始连接
client.BeginConnect(serverIP[0],51888,requestCallBack,client);//这一行出错,说不支持此协议版本
listBox1.Invoke(setlistboxcallback,string.Format("本机终结点:{0}",client.Client.LocalEndPoint));
listBox1.Invoke(setlistboxcallback,"开始与服务器连接");
allDone.WaitOne();
}
private void RequestCallBack(IAsyncResult iar)
{
allDone.Set();
try
{
client = (TcpClient)iar.AsyncState;
client.EndConnect(iar);
listBox1.Invoke(setlistboxcallback,string.Format("与服务器{0}连接成功",client.Client.RemoteEndPoint));
ns = client.GetStream();
DataRead dataRead = new DataRead(ns,client.ReceiveBufferSize);
ns.BeginRead(dataRead.msg, 0, dataRead.msg.Length, ReadCallBack, dataRead);//回调函数ReadCallBack
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback ,e.Message);
return;
}
finally
{
}
}
private void ReadCallBack(IAsyncResult iar)
{
try
{
DataRead dataRead = (DataRead)iar.AsyncState;
int recv = dataRead.ns.EndRead(iar);
//在文本框里设置得到的数据
richTextBox2.Invoke(setRichTextBoxReceiveCallBack,Encoding.UTF8.GetString(dataRead.msg,0,recv));
if (isExit == false)
{
dataRead = new DataRead(ns,client.ReceiveBufferSize);
ns.BeginRead(dataRead.msg,0,dataRead.msg.Length,ReadCallBack,dataRead);
}
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback,e.Message);
}
finally
{}
}
private void SetListBox(string str)
{
listBox1.Items.Add(str);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.ClearSelected();
}
private void SetRichTextBoxReceive(string str)
{
richTextBox2.AppendText(str);
}
private void button2_Click(object sender, EventArgs e)
{
SendString(richTextBox1.Text);
richTextBox2.Clear();
}
private void SendString(string str)
{
try
{
byte [] bytesdata=Encoding.ASCII.GetBytes(str+"\r\n");
ns.BeginWrite(bytesdata, 0, bytesdata.Length, new AsyncCallback(SendCallBack), ns);//回调函数SendCallBack
ns.Flush();
}
catch(Exception e)
{
listBox1.Items.Add(e.Message);
}
finally
{}
}
private void SendCallBack(IAsyncResult iar)
{
try
{
ns.EndWrite(iar);
}
catch(Exception e)
{
listBox1.Invoke(setlistboxcallback,e.Message);
}
finally
{}
}
}
}