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 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个socket对像,如果用udp协议,则要用SocketType.Dgram类型的套接字
            s.Bind(ipe);//绑定EndPoint对像(端口和ip地址)
            s.Listen(0);//开始监听
            txt.Text += "等待客户端连接";
           // Console.WriteLine("等待客户端连接");
            //定义循环,以便可以简历多次连接
            while (true)
            {
                Socket temp = s.Accept();//为新建连接创建新的socket               
                while (true)
                {
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    txt.Text += "\r\n" + recvStr + "\r\n";
                    if (recvStr.IndexOf("<EOF>") > -1)
                    {
                        break;
                    }
                }                
                //给client端返回信息
               // Console.WriteLine("server get message:{0}", recvStr);//把客户端传来的信息显示出来
               
                string sendStr = "jpeg upload OK";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                temp.Send(bs, bs.Length, 0);//返回信息给客户端
                temp.Shutdown(SocketShutdown.Both);
                temp.Close();            

            }
        }
        catch (Exception ex)
        {
            string str = ex.ToString();
            txt.Text += "\r\n" + str + "\r\n";
        }
    }
}}

 

客户端按钮事件

   protected void Button1_Click(object sender, EventArgs e)
        {
                Socketclient client = new Socketclient(TextBox2, TextBox3);
                client.StartClient();
           
        }


//服务端监听执行线程

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SocketServer socket = new SocketServer(1234, "192.168.88.196", TextBox1);
                //启动线程
                Thread thread = new Thread(new ThreadStart(socket.beginListen));
                thread.Start();
                // 在应用程序启动时运行的代码
                //(new System.Threading.Thread(new System.Threading.ThreadStart(new Class1().CreatSocket))).Start();//开辟一个新线程
            }
        }


aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JpegForSocketServer.aspx.cs" Inherits="WebForAndroid.JpegForSocketServer" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Height="206px" TextMode="MultiLine" Width="700px" ></asp:TextBox>
    </div>
        <hr />
        <asp:TextBox ID="TextBox2" runat="server" Height="197px" TextMode="MultiLine" Width="376px"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" Height="119px" TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Height="52px" OnClick="Button1_Click" Text="SEND" Width="138px" />
    </form>
</body>
</html>


图:

asp.net Socket 实例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。