码迷,mamicode.com
首页 > Windows程序 > 详细

C# 客户端服务端的编写

时间:2014-12-09 22:48:32      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   os   sp   on   div   art   

客户端的代码 
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();
        }
    }

 

C# 客户端服务端的编写

标签:style   blog   ar   color   os   sp   on   div   art   

原文地址:http://www.cnblogs.com/codemouserman/p/4154216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!