asp.net中c#socket應用執行個體

來源:互聯網
上載者:User
 代碼如下 複製代碼

Server using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SocketTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket serverSk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            serverSk.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8596));
            serverSk.Listen(1);

            SocketAsyncEventArgs AcceptSAE = new SocketAsyncEventArgs();
            AcceptSAE.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptSAE_Completed);
            serverSk.AcceptAsync(AcceptSAE);

            Console.ReadLine();
        }

        static void AcceptSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket serverSk = sender as Socket;
            if (e.SocketError == SocketError.Success)
            {
                serverSk = e.AcceptSocket;
                SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs();
                byte[] data = System.Text.Encoding.UTF8.GetBytes("OK,Just Put IT!");
                SendSAE.SetBuffer(data, 0, data.Length);
                SendSAE.Completed += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed);

                SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
                byte[] buffer = new byte[2048];
                RecieveSAE.SetBuffer(buffer, 0, buffer.Length);
                RecieveSAE.Completed += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed);

                serverSk.ReceiveAsync(RecieveSAE);
                serverSk.SendAsync(SendSAE);
            }
            else
                Console.WriteLine("接管收集通訊端串連懇求失敗!具體原因請本身調試!");
        }


        static void RecieveSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket sk = sender as Socket;
            byte[] data = e.Buffer;  //重視這裡,如何取接洽關係到通訊端的發送接管的緩衝區中的值。
            string msg = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine("Message received: " + msg);

           // sk.DisconnectAsync();//你看看 該怎麼做呢?
        }

        static void SendSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket sk = sender as Socket;
            if (e.SocketError == SocketError.Success)
            {
                Console.WriteLine("Send complete!");

                byte[] data = e.Buffer;  //重視這裡,如何取接洽關係到通訊端的發送接管的緩衝區中的值。
                string msg = System.Text.Encoding.UTF8.GetString(data);
                Console.WriteLine("What you sent: " + msg);
            }
        }
    }
}


 

2 用戶端代碼

 代碼如下 複製代碼

Cilent using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SocketTestTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket clientSk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SocketAsyncEventArgs ConnectSAE = new SocketAsyncEventArgs();
            ConnectSAE.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8596);
            ConnectSAE.Completed += new EventHandler<SocketAsyncEventArgs>(ConnectSAE_Completed);
            clientSk.ConnectAsync(ConnectSAE);
            Console.ReadLine();
        }

        static void ConnectSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket clientSk = sender as Socket;
            if (e.SocketError == SocketError.Success && clientSk.Connected)
            {
                SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs();
                byte[] data = System.Text.Encoding.UTF8.GetBytes("I want put all funy things together!");
                SendSAE.SetBuffer(data, 0, data.Length);
                SendSAE.Completed += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed);

                SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
                byte[] buffer = new byte[2048];
                RecieveSAE.SetBuffer(buffer, 0, buffer.Length);
                RecieveSAE.Completed += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed);

                //先調用非同步接管,再調用非同步發送。讓你體驗到非同步明顯非一般的感觸感染。
                clientSk.ReceiveAsync(RecieveSAE);
                clientSk.SendAsync(SendSAE);
            }
        }

        static void RecieveSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket sk = sender as Socket;
            byte[] data = e.Buffer;  //重視這裡,如何取接洽關係到通訊端的發送接管的緩衝區中的值。
            string msg = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine("Message received: "+msg);

            //sk.DisconnectAsync();//你看看 該怎麼做呢?
        }

        static void SendSAE_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket sk = sender as Socket;
            if (e.SocketError == SocketError.Success)
            {
                Console.WriteLine("Send complete!");
            }
        }

       
    }
}

3 調試運行

先調試運行辦事器項目

 

再調試運行用戶端項目

 

4 查看運行成果

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.