客户端的代码
class client
{
public void mehod()
{
TcpClient tcp = new TcpClient();
tcp.Connect(IPAddress.Parse("192.168.0.168"), );
NetworkStream stream = tcp.GetStream();
string cmd = "demo TESTING";
byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray());
stream.Write(outbytes, , outbytes.Length);
byte[] buffer = new byte[];
int len = stream.Read(buffer, , buffer.Length);
string msg = System.Text.Encoding.ASCII.GetString(buffer, , len);
Console.WriteLine("客户端执行。。。。。。");
new server().method();
tcp.Close();
Console.ReadLine();
}
}
服务端的代码:
class server
{
public void method()
{
IPAddress ipaddress = IPAddress.Parse("192.168.0.168");
TcpListener listener = new TcpListener(ipaddress, 23850);
listener.Start();
Console.WriteLine("服务端执行");
Console.ReadLine();
Socket socket = listener.AcceptSocket();
byte[] buffer = new byte[1024];
socket.Receive(buffer);
string message = "hello";
byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray());
socket.Send(outbytes, message.Length, 0);
socket.Close();
}
}