110,502
社区成员
发帖
与我相关
我的任务
分享
//这段代码是在一个异步方法中 其中m_tcpListener是一个TCPListener的实例
while (true)
{
TcpClient m_tcp = await m_tcpListener.AcceptTcpClientAsync();
NetworkStream ns = m_tcp.GetStream();
string Nowdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
string s = "消息过去了啊!!!!!!" + Nowdate;
Byte[] sbyt = Encoding.UTF8.GetBytes(s);
uint len = (uint)sbyt.Length;
ns.Write(BitConverter.GetBytes(len), 0, sizeof(uint));
ns.Write(sbyt, 0, sbyt.Length);
ns.Close();
try
{
m_tcp.Close();
this._textBox.Text = "Cut Successd";
}
catch (Exception)
{
}
}
//_SR是一个streamsocket 实例
HostName hServer = new HostName("127.0.0.1");
try
{
await _SR.ConnectAsync(hServer, "3400");
this.isConnect = true;
this.ServicesINFO.Text = "Succese";
}
catch (Exception exception)
{
_SR.Dispose();
quit = false;
this.isConnect = false;
}
/*........里面是一些接受字符串的代码..........*/
try
{
_SR.Dispose();
_SR = null;
this.PortInfo.Text = "cut successed";
}
catch(Exception ex)
{
}