日期:2014-05-17 浏览次数:21053 次
private Thread _Thread;
private TcpListener _TcpListener;
_TcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
_TcpListener.Start();
_Thread = new Thread(new ThreadStart(StartListen));
_Thread.Start();
private void StartListen()
{
while(true)
{
TcpClient client = _TcpListener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
StreamReader sReader = new StreamReader(stream);
string content = sReader.ReadToEnd();
string responseContent = "hello";
//执行逻辑操作
byte[] bytes =System.Text.Encoding.UTF8.GetBytes(responseContent);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
client.Close();
}
}
TcpClient tcpClient = new TcpClient(ip, port);
NetworkStream networkStream = tcpClient.GetStream();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("请求的指令");
networkStream.Write(bytes, 0, bytes.Length);
//这里如何获取服务端的返回值
networkStream.Close();
tcpClient.Close();