一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net中c#socket应用实例

时间:2022-06-25 08:23:03 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

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(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(SendSAE_Completed);

                SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
                byte[] buffer = new byte[2048];
                RecieveSAE.SetBuffer(buffer, 0, buffer.Length);
                RecieveSAE.Completed += new EventHandler(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(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(SendSAE_Completed);

                SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
                byte[] buffer = new byte[2048];
                RecieveSAE.SetBuffer(buffer, 0, buffer.Length);
                RecieveSAE.Completed += new EventHandler(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 查看运行成果

 

热门栏目