关于Socket的编程以及信号量的进一步理解

今天在测试Socket的异步方式的使用方法时,进一步发现了对AutoResetEvent和ManualResetEvent的进一步理解。到底是先把ManuslResetEvent的例子代码贴出来还是把Socket的例子代码贴出来呢?

我决定先把关于Socket的例子代码贴出来再说。下面的代码也是摘自MSND,在“使用.NET Framework编程”这一部分中有一章讲述的是“套接字”,其中的示例代码包含了“异步客户端套接字示例”和“异步服务器套接字示例”,代码如下:

异步服务器套接字示例:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;


/// <summary>
/// 用于异步读取客户端数据的状态对象,根据文档,使用Socket通信的时候,
/// 在进行异步调用的情况下,需要一个对象
/// </summary>
public class StateObject {
    //客户端Socket
    public Socket workSocket = null;
    //接收缓冲区大小
    public const int BufferSize = 1024;
    //接收缓冲区
    public byte[] buffer = new byte[BufferSize];
 //接收到的数据字符串
    public StringBuilder sb = new StringBuilder(); 
}

public class AsynchronousSocketListener {
   
    // 从客户端接收数据,定义为静态
    public static string data = null;

    // 线程信号,这里采用的是ManualResetEvent,就是手动的
 // 恢复非终止状态的信号对象,另外,这个程序中的线程也是
 // 隐含的,而不是明确的线程对象,定义为静态
    public static ManualResetEvent allDone = new ManualResetEvent(false);

 /// <summary>
 /// 构造函数
 /// </summary>
    public AsynchronousSocketListener()
 {
    }

 /// <summary>
 /// 注意,这里定义的也是静态的方法
 /// </summary>
    public static void StartListening() {
        // 收取数据的数据缓冲区
        byte[] bytes = new Byte[1024];

        // 为Socket创建一个本地的终端点
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // 创建一个TCP/IP的socket
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );

        // 将socket绑定到本地终端点然后侦听到来的连接,
        // 这应该是Server的特点,在客户端不存在这种问题
        try {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true) {
                // 将事件置为非信号状态,这里是手动设定的区别于AutoResetEvent
                allDone.Reset();

                // 开始一个异步Socket以侦听连接
                Console.WriteLine("Waiting for a connection...");
                listener.BeginAccept( new AsyncCallback(AcceptCallback), listener );

                // 等待,直到一个连接被建立之后才继续
                allDone.WaitOne();
    
            }

        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("/nPress ENTER to continue...");
        Console.Read();
       
    }

    public static void AcceptCallback(IAsyncResult ar) {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket) ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar) {
        String content = String.Empty;
       
        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket.
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0) {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer,0,bytesRead));

            // Check for end-of-file tag. If it is not there, read
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1) {
                // All the data has been read from the
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. /n Data : {1}",
                    content.Length, content );
                // Echo the data back to the client.
                Send(handler, content);
            } else {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }
   
    private static void Send(Socket handler, String data) {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar) {
        try {
            // Retrieve the socket from the state object.
            Socket handler = (Socket) ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }


    public static int Main(String[] args) {
        StartListening();
        return 0;
    }
}

异步客户端套接字示例:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

// State object for receiving data from remote device.
public class StateObject {
    // Client socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 256;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

public class AsynchronousClient {
    // The port number for the remote device.
    private const int port = 11000;

    // ManualResetEvent instances signal completion.
    private static ManualResetEvent connectDone =
        new ManualResetEvent(false);
    private static ManualResetEvent sendDone =
        new ManualResetEvent(false);
    private static ManualResetEvent receiveDone =
        new ManualResetEvent(false);

    // The response from the remote device.
    private static String response = String.Empty;

    private static void StartClient() {
        // Connect to a remote device.
        try {
            // Establish the remote endpoint for the socket.
            // The name of the
            // remote device is "host.contoso.com".
            IPHostEntry ipHostInfo = Dns.Resolve("LiuyiNoteBook");
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Connect to the remote endpoint.
            client.BeginConnect( remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            // Send test data to the remote device.
            Send(client,"This is a test<EOF>");
            sendDone.WaitOne();

            // Receive the response from the remote device.
            Receive(client);
            receiveDone.WaitOne();

            // Write the response to the console.
            Console.WriteLine("Response received : {0}", response);

            // Release the socket.
            client.Shutdown(SocketShutdown.Both);
            client.Close();
           
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ConnectCallback(IAsyncResult ar) {
        try {
            // Retrieve the socket from the state object.
            Socket client = (Socket) ar.AsyncState;

            // Complete the connection.
            client.EndConnect(ar);

            Console.WriteLine("Socket connected to {0}",
                client.RemoteEndPoint.ToString());

            // Signal that the connection has been made.
            connectDone.Set();
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Receive(Socket client) {
        try {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ReceiveCallback( IAsyncResult ar ) {
        try {
            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            StateObject state = (StateObject) ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0) {
                // There might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

                // Get the rest of the data.
                client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
                    new AsyncCallback(ReceiveCallback), state);
            } else {
                // All the data has arrived; put it in response.
                if (state.sb.Length > 1) {
                    response = state.sb.ToString();
                }
                // Signal that all bytes have been received.
                receiveDone.Set();
            }
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Send(Socket client, String data) {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        client.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), client);
    }

    private static void SendCallback(IAsyncResult ar) {
        try {
            // Retrieve the socket from the state object.
            Socket client = (Socket) ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = client.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to server.", bytesSent);

            // Signal that all bytes have been sent.
            sendDone.Set();
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }
   
    public static int Main(String[] args) {
        StartClient();
        return 0;
    }
}

这两段代码。经过编译运行,没有问题,但是我将服务器端的代码做了改变,因为我想可能会需要一个Windows的窗体。这样我就把服务端的文件加入到了一个Windows应用程序当中,该程序中,我设置了一个Winform的窗体,该窗体有一个名为btnOK的按钮,在按钮中替代了原来示例程序中的Main方法,用它来启动服务器端的监听。也就是StartListening()这个方法,当然,从客户端来看这样的执行结果与原来的控制台程序没有什么差别,客户端依然会返回需要的结果:

D:/学习/VS2003Project/Socket>client
Socket connected to 192.168.1.49:11000
Sent 19 bytes to server.
Response received : This is a test<EOF>

D:/学习/VS2003Project/Socket>

然而在服务器端出现了一个让人不愿意看到的现象,就是窗体被锁住了,甚至连移动窗体的位置都移动不了了,我跟踪了一下,发现窗体被锁定的位置是StartListening()这个方法中的While(true)这个无限循环中的allDone.WaitOne()这个方法上。这时,我明白了allDone的所在的线程一定是被WaitOne()这个方法给挂起了,因为曾经使用过无限循环,但是如果加入了Thread.Sleep(0),还是可以给其他线程一些机会的,然而这一次一点用处都没有,因为我尝试了,无论这个语句加在什么位置上都没有用的。

这就验证了在用户界面程序当中不能使用WaitOne()这个方法,它会锁住程序的界面。我的办法是创建一个独立的线程在此线程中使用StartListening()这个方法。还好,这个方法符合线程的委托定义:是一个静态的方法,而且不带参数。

这样的改进确保了完成服务器的监听,而且不会影响界面的输入,唯一的一点问题是似乎CPU的占用略显明显一些?

当然把这个方法塞进一个线程中对其中的一些方法的使用是否有影响还不是搞的很清楚的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值