日期:2014-05-16  浏览次数:20958 次

asp.net Socket 实例
 
public class Socketclient
    {
        TextBox txt;
        TextBox send_txt;
        public Socketclient(TextBox txt, TextBox send_txt)
        {
            this.txt = txt;
            this.send_txt = send_txt;
        }
        public void StartClient()
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            // Connect to a remote device.
            try
            {
                // Establish the remote endpoint for the socket.
                // This example uses port 11000 on the local computer.
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 1234);
                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Connect the socket to the remote endpoint. Catch any errors.
                try
                {
                    sender.Connect(remoteEP);
                    // Console.WriteLine("Socket connected to {0}",sender.RemoteEndPoint.ToString());
                    txt.Text += "\r\n" + sender.RemoteEndPoint.ToString() + "\r\n";
                    // Encode the data string into a byte array.
                    byte[] msg = Encoding.ASCII.GetBytes(send_txt.Text + "<EOF>");
                    // Send the data through the socket.
                    int bytesSent = sender.Send(msg);
                    // Receive the response from the remote device.
                    int bytesRec = sender.Receive(bytes);
                    //Console.WriteLine("Echoed test = {0}",Encoding.ASCII.GetString(bytes,0,bytesRec));
                    txt.Text += "\r\n" + Encoding.ASCII.GetString(bytes, 0, bytesRec) + "\r\n";//接收远程服务器信息

                    // Release the socket.
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                }
                catch (ArgumentNullException ane)
                {
                    // Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
                    txt.Text += "\r\n" + ane.ToString() + "\r\n";
                }
                catch (SocketException se)
                {
                    // Console.WriteLine("SocketException : {0}",se.ToString());
                    txt.Text += "\r\n" + se.ToString() + "\r\n";
                }
                catch (Exception e)
                {
                    // Console.WriteLine("Unexpected exception : {0}", e.ToString());
                    txt.Text += "\r\n" + e.ToString() + "\r\n";
                }
            }
            catch (Exception e)
            {
                // Console.WriteLine( e.ToString());
                txt.Text += "\r\n" + e.ToString() + "\r\n";
            }
        }
    }
 public class SocketServer {
    
     int port;  //端口
     string host;//ip地址
     TextBox txt;
     /// <summary>
     /// 构造涵数
     /// </summary>
     /// <param name="ports">端口号</param>
     public SocketServer(int ports, string host,TextBox txt)
     {
       this.port=ports;
       this.host = host;
       this.txt = txt;
     }    
     //开始监听
    public void beginListen(){        
        try
        {
            IPAddress ip = IPAddress.Parse(host);//把ip地址字符串转换为IPAddress类型的实例
            IPEndPoint ipe = new IPEndPoint(ip, port);//用指定的端口和ip初始化IPEndPoint类的新实例
            ///创建socket并开始监听
            Socket