socket

Server:

class Program

{
    static void Main(string[] args)
    {
        Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  //新建socket
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 8888);
        listenSocket.Bind(ep);  //绑定期监听主机IP和端口号
        listenSocket.Listen(0);  //最多可容纳的等待接收的传入连接数

        while (true)
        {
            if (listenSocket.Poll(-1, SelectMode.SelectRead))  //在指定的时间段内阻止执行,负数表示无限期的等待响应; 如果socket可读
            {
                Socket mySocket = listenSocket.Accept();  //提取第一个等待的请求,返回一个新的socket
                byte[] bytes = new byte[256];
                mySocket.Receive(bytes);  //接收socket中的数据
                Console.WriteLine(string.Format(Encoding.UTF8.GetString(bytes) + "\r\n"));

                byte[] msg = Encoding.UTF8.GetBytes("This is a test S->C" + DateTime.Now.ToString());
                mySocket.Send(msg);  //服务器发送返回信息
                mySocket.Shutdown(SocketShutdown.Both);  //关闭socket的发送和接收
                mySocket.Close();
            }
        }


        //TcpListener server = null;
        //try
        //{
        //    server = new TcpListener(IPAddress.Parse("127.0.0.1"), 13000);
        //    server.Start();
        //    Byte[] bytes = new Byte[256];
        //    String data = null;

        //    int j = 0;
        //    while (j < 2)//只接收两个请求。用true则无此限制
        //    {
        //        j = j + 1;
        //        TcpClient client = server.AcceptTcpClient();  //接收请求的客户端
        //        data = null;
        //        NetworkStream stream = client.GetStream();  //得到客户端的stream

        //        int i;
        //        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)  //读取数据到bytes
        //        {
        //            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
        //            Console.WriteLine(DateTime.Now.ToString() + " Received: " + data + "\r\n");

        //            data = data.ToUpper() + DateTime.Now.ToString();
        //            byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

        //            stream.Write(msg, 0, msg.Length);  //写数据,回发操作
        //            Console.WriteLine(" Sent back: " + data + "\r\n");
        //        }
        //        client.Close();
        //    }
        //}
        //catch (SocketException ex)
        //{
        //    Console.WriteLine(ex.ToString());
        //}
        //finally
        //{
        //    server.Stop();
        //}
    }

}

 

Client:

class Program
{
    static void Main(string[] args)
    {
        byte[] msg = Encoding.UTF8.GetBytes("This is a test C->S" + DateTime.Now.ToString());
        byte[] bytes = new byte[256];

        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  //新建socket
        s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.2"), 8888));  //连接主机/端口号

        int i = s.Send(msg);  //发送消息,返回消息字节数
        Console.WriteLine(string.Format("Sent {0} bytes.", i));

        i = s.Receive(bytes);  //接收服务器返回的消息
        Console.WriteLine(string.Format(Encoding.UTF8.GetString(bytes)));

        s.Close();

        
        //try
        //{
        //    TcpClient client = new TcpClient("127.0.0.1", 13000);
        //    NetworkStream stream = client.GetStream();  //得到client的stream,用来发送或接收data

        //    string message = " test data c->s ";
        //    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);  //转成二进制数据
            
        //    stream.Write(data, 0, data.Length);  //往stream里写入要发送的数据
        //    Console.WriteLine(DateTime.Now.ToString() + "Sent:" + message + "\r\n");

        //    data = new Byte[256];
        //    String responseData = String.Empty;

        //    Int32 bytes = stream.Read(data, 0, data.Length);  //读取第一组从服务器端返回的数据
        //    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);  //转成字符串数据
        //    Console.WriteLine(DateTime.Now.ToString() + "Received:" + responseData + "\r\n");

        //    stream.Close();  //关闭stream
        //    client.Close();  //关闭客户端
        //}
        //catch (ArgumentNullException ex)
        //{
        //    Console.WriteLine(ex.ToString());
        //}
        //catch (SocketException ex)
        //{
        //    Console.WriteLine(ex.ToString());
        //}
    }
}

posted @ 2010-04-21 17:34  DaCHun  阅读(324)  评论(0编辑  收藏  举报