/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmServer());
} private void frmServer_Load(object sender, System.EventArgs e)
{
txtServer.Text = Dns.GetHostName();
} 监听#region 监听
//开始监听
private void btnListen_Click(object sender, System.EventArgs e)
{
try
{
IPHostEntry myHost = new IPHostEntry();
myHost = Dns.GetHostByName(txtServer.Text);
string IPstring = myHost.AddressList[0].ToString();
myIP = IPAddress.Parse(IPstring);
}
catch
{
MessageBox.Show("您输入的IP地址格式不正确,请重新输入!");
} try
{
myServer = new IPEndPoint(myIP,Int32.Parse(txtPort.Text.Trim()));
mySocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
mySocket.Bind(myServer);
mySocket.Listen(50); //IPEndPoint aaIP = (IPEndPoint)mySocket.LocalEndPoint;
//int aa = aaIP.Port;
//MessageBox.Show(aa.ToString()); txtState.AppendText("主机"+txtServer.Text+"端口"+txtPort.Text+"开始监听..\r\n");
//线程
Thread thread = new Thread(new ThreadStart(target));
thread.Start();
}
catch(Exception ee)
{
txtState.AppendText(ee.Message+"\r\n");
}
} //线程同步方法target
private void target()
{
while(true)
{
//设为非终止
myReset.Reset(); mySocket.BeginAccept(new AsyncCallback(AcceptCallback),mySocket);
//阻塞当前线程,直到收到请求信号
myReset.WaitOne();
}
} //异步回调方法AcceptCallback
private void AcceptCallback(IAsyncResult ar)
{
//将事件设为终止
myReset.Set(); Socket listener = (Socket)ar.AsyncState;
handler = listener.EndAccept(ar); //获取状态
StateObject state = new StateObject();
state.workSocket = handler; txtState.AppendText("与客户建立连接。\r\n");
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes("已经准备好,请通话!"+"\r\n");
//开始发送
handler.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SendCallback),handler);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
} //线程
Thread thread = new Thread(new ThreadStart(begReceive));
thread.Start();
} //异步回调方法 SendCallback
private void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
} //线程同步方法begReceive
private void begReceive()
{
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReadCallback),state);
} //异步回调方法ReadCallback
private void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject) ar.AsyncState;
Socket tt = state.workSocket; //结束读取并获取读取字节数
int bytesRead = handler.EndReceive(ar);
state.sb.Append(System.Text.Encoding.BigEndianUnicode.GetString(state.buffer,0,bytesRead));
string content = state.sb.ToString();
state.sb.Remove(0,content.Length);
rtbIncept.AppendText(content+"\r\n"); //重新开始读取数据
tt.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReadCallback),state);
}#endregion 监听
//发送信息
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
byte[] byteData = System.Text.Encoding.BigEndianUnicode.GetBytes(rtbSend.Text+"\r\n"); //开始发送数据
handler.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SendCallback),handler);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
} //停止监听
private void btnStop_Click(object sender, System.EventArgs e)
{
try
{
mySocket.Close();
txtState.AppendText("主机"+txtServer.Text+"端口"+txtPort.Text+"停止监听"+"\r\n");
}
catch
{
MessageBox.Show("监听尚未开始,关闭无效!");
}
} }
同步线程和异步线程的使用,回调函数的意义
原创
©著作权归作者所有:来自51CTO博客作者biyusr216的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:企业开发基础设施--类厂服务
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
.net 线程如何回调函数 线程回调 java
在实际开发过程中遇到的java多线程情况不多,但是使用的组件,框架中则很多用到了多线程技术,java面试时也会考到,所以看看多线程的知识还是很有必要的。
.net 线程如何回调函数 多线程 java xml