C# 客户端服务端的编写

客户端的代码 
class client { public void mehod() { TcpClient tcp = new TcpClient(); tcp.Connect(IPAddress.Parse("192.168.0.168"), 23850); NetworkStream stream = tcp.GetStream(); string cmd = "demo TESTING"; byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray()); stream.Write(outbytes, 0, outbytes.Length); byte[] buffer = new byte[1024]; int len = stream.Read(buffer, 0, buffer.Length); string msg = System.Text.Encoding.ASCII.GetString(buffer, 0, 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();
        }
    }

 

posted @ 2014-12-09 21:24  初入程序猿  阅读(2265)  评论(0编辑  收藏  举报