应该也是用Receive方法吧,谁能告诉我大概怎么写?非常感谢!
解决方案 »
- 大型设备 需要射频标识 怎么办? 求助!
- C# 怎么处理关闭事件
- C#中有类似java中Hibernate框架吗?
- form的closing事件为何触发2次?
- aspx.cs怎么判断访问者是浏览器还是播放器呢?
- RTF文件内容替换问题
- 向原始数据存储区提交行时出错
- IE Web Controls中treeview不能按树结构显示的问题
- 把窗体的TopMost设置为True ,如果放一个DataGrid哪么置顶就失效???
- 高手能否解决:.NET调用VB编译的DLL,为什么不支持非模态窗体呢??
- C#中如何实现对局域网计算机的重起和关机?????
- 使用SaveFileDialog到sf.ShowDialog()卡住了
{
tempSocket.Receive(byte_Re,0,byte_Re.Length,SocketFlags.None);
this.txtState.AppendText(Encoding.GetEncoding("gb2312").GetString(byte_Re)+"\n");
}应该是类似这样吧
private void OnReceive(IAsyncResult ar)
{
... // 接受和处理数据
theSocket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, new AsyncCallback(OnReceive), null); // 继续接收
}
/// 侦听处理线程
/// </summary>
private void accept()
{
while(!round)
{
try
{
m_accSocket = m_Socket.Accept();
if(m_accSocket.Connected)
{
Thread thread=new Thread(new ThreadStart(receive));
thread.Start();
m_AcceptCallBack(1);
}
}
catch
{
}
}
谢谢两位!
/// Tcp通信服务器类
/// </summary>
public class JhTcpServer
{
public JhTcpServer(int port):this("",port)
{
} public JhTcpServer(string ip,int port)
{
IPAddress ipAddress;
if (ip.ToUpper().Trim() == "")
{
ipAddress = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
}
else
{
ipAddress = IPAddress.Parse(ip);
}
server = new TcpListener(ipAddress,port);
adm = new CommDataMgr();
status = new Parm(this);
}
~JhTcpServer()
{
Stop();
}
/// <summary>
/// 连接成功事件
/// </summary>
public event CommEventHandler OnClientConnected; /// <summary>
/// 连接断开事件
/// </summary>
public event CommEventHandler OnClientDisconnected; /// <summary>
/// Tcp监听对象
/// </summary>
TcpListener server = null; /// <summary>
/// 客户端序号
/// </summary>
int clientindex = 0; /// <summary>
/// 开始监听
/// </summary>
public void Start()
{
clients.Clear();
clientindex = 99999999;
adm.Start();
server.Start();
status.Status = JhThreadStatus.Running;
listenThread = new Thread(new ThreadStart(this.ListenThread));
listenThread.Name = "JH tcp listenor";
listenThread.Start();
Console.WriteLine("JH tcp listenor started.");
}
/// <summary>
/// 监听线程
/// </summary>
Thread listenThread = null; /// <summary>
/// 停止接受连接
/// </summary>
public void Stop()
{
try
{
int i;
for (i = 0; i < this.clients.Count; ++i)
{
StopClient(i);
}
adm.Stop();
if (status.Status == JhThreadStatus.Running)
{
// listenThread.Abort();
lock(this)
{
status.Status = JhThreadStatus.Stop;
}
for (i = 0 ; i < 3; ++i)
{
if (status.Status == JhThreadStatus.Running)
{
Thread.Sleep(110);
}
}
if (status.Status == JhThreadStatus.Running)
{
listenThread.Abort();
}
}
server.Stop();
}
catch (Exception e)
{
}
}
/// <summary>
/// 断开指定索引的客户
/// </summary>
/// <param name="index">客户端索引</param>
public void StopClient(int index)
{
GetClient(index).Exit();
} /// <summary>
/// 发送数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public void Send(JhClient client,string data)
{
if (client != null)
{
byte[] dt = System.Text.Encoding.Default.GetBytes(data);
client.Send(dt);
}
} /// <summary>
/// 用最后一个客户端发送数据
/// </summary>
/// <param name="data"></param>
public void Send(string data)
{
Send(GetClient(clients.Count - 1),data);
} // /// <summary>
// /// 发送连接消息
// /// </summary>
// /// <param name="client"></param>
// private void SendConnected(JhClient client)
// {
// client.SendMessage(JhCommMessages.Connected);
// } /// <summary>
/// 数据分发管理者
/// </summary>
CommDataMgr adm ; Parm status = null; /// <summary>
/// 客户连接线程
/// </summary>
void ListenThread()
{
try
{
while (status.Status == JhThreadStatus.Running)
{
if (server.Pending() == true)
{
Socket client = server.AcceptSocket();//侦听到连接后创建客户端
JhClient jhClient = new JhClient(adm,client);
jhClient.ID = clientindex--;
jhClient.OnClosed += new CommEventHandler(this.Client_Disconnect);
jhClient.StartReceive();
SetClient(jhClient);
if (OnClientConnected != null)
{
OnClientConnected(jhClient,new CommEventArgs("Client connected: " + jhClient.Client.RemoteEndPoint.ToString()));
}
}
Thread.Sleep(100);
}
}
catch (ObjectDisposedException e2)
{
MessageBox.Show(e2.Message);
}
catch (InvalidOperationException e1)
{
MessageBox.Show(e1.Message);
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
}
finally
{
lock(this)
{
status.Status = JhThreadStatus.Exit;
}
Console.WriteLine("JH tcp listenor exited");
}
}
/// <summary>
/// 客户端连接端开处理
/// </summary>
/// <param name="sender">客户端对象</param>
/// <param name="e">事件对象</param>
void Client_Disconnect(object sender,CommEventArgs e)
{
if (clients.ContainsValue(sender))
{
clients.Remove(((JhClient)sender).ID);
}
if (this.OnClientDisconnected != null)
{
OnClientDisconnected(sender,e);
}
}
/// <summary>
/// 添加观察者
/// </summary>
/// <param name="ob"></param>
public void AddOb(IObserver ob)
{
adm.Add(ob);
}
Hashtable clients = new Hashtable(); /// <summary>
/// 根据内部id获取客户端
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public JhClient GetClient(int id)
{
return (JhClient)clients[id];
}
/// <summary>
/// 设置客户端对象
/// </summary>
/// <param name="client"></param>
public void SetClient(JhClient client)
{
int i;
for (i = 0 ; i < clients.Count; ++i)
{
if (((JhClient)clients[i]).ID == client.ID)
{
break;
}
}
clients[i] = client;
} }