关于Socket异步的简单应用

服务器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket ServerSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            IPEndPoint serIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8000);
            ServerSocket.Bind(serIP);
            ServerSocket.Listen(0);
            Console.WriteLine("服务器已开始监听");
            asyncAccept(ServerSocket);

            Console.Read();  
            
        }
        public static void asyncAccept(Socket Ser) {
            Ser.BeginAccept(asyncResult =>
            {
                Socket Client = Ser.EndAccept(asyncResult);
                asyncSend(Client,"当前设备已连接");
                asyncRecive(Client);
                asyncAccept(Ser);
            }, null);
        }
        public static void asyncRecive(Socket ser){
            byte[] data = new byte[1024];
            ser.BeginReceive(data, 0, data.Length, SocketFlags.None, asyncResult => {
                int length = ser.EndReceive(asyncResult);
                Console.WriteLine(Encoding.UTF8.GetString(data));
            },null);
        }
        public static void asyncSend(Socket client,string  message) {
            byte[] data = Encoding.UTF8.GetBytes(message);
            client.BeginSend(data, 0, data.Length, SocketFlags.None, asyncResult => {
                int length = client.EndSend(asyncResult);
                Console.WriteLine("消息已发送");
            },null);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket Client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            Client.BeginConnect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000), asyncSesult => {
                Client.EndConnect(asyncSesult);
                Console.WriteLine("已连接服务器");
                asyncSend(Client,"一号设备已连接");
                asyncRecive(Client);
            },null);
            Console.Read();
        }
        public static void asyncRecive(Socket Ser) { 
            byte[] data=new byte[1024];
            Ser.BeginReceive(data, 0, data.Length, SocketFlags.None,asyncResult => {
                int length = Ser.EndReceive(asyncResult);
                Console.WriteLine(Encoding.UTF8.GetString(data));
            },null);
        }
        public static void asyncSend(Socket Ser,string message) {
            byte[] data = Encoding.UTF8.GetBytes(message);
            Ser.BeginSend(data, 0, data.Length, SocketFlags.None, asyncResult => {
                int length = Ser.EndSend(asyncResult);
                Console.WriteLine("消息已发送");
            },null);
        }
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值