简单socket服务(二)实现多客户端向服务器发送数据

实现多客户端向服务器发送数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace MyAsyncServer
{
    class Program
    {
        private static byte[] byteData = new byte[1024];
        
        static void Main(string[] args)
        {

            startListen();
        }
        public static ManualResetEvent connEvent = new ManualResetEvent(false);
        public static ManualResetEvent receiveEvent = new ManualResetEvent(false);
        public static ManualResetEvent sendEvent = new ManualResetEvent(false);
        public static void startListen()
        {
            try
            {
                IPAddress ipAddress = IPAddress.Parse("192.168.1.101");
                IPEndPoint ipEnd = new IPEndPoint(ipAddress, 1002);
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverSocket.Bind(ipEnd);
                serverSocket.Listen(10);

                Console.WriteLine("server is start!");
                while (true)
                {
                    connEvent.Reset();
                    serverSocket.BeginAccept(
                        new AsyncCallback(AcceptedCallback),
                        serverSocket);
                    connEvent.WaitOne();
                }
               
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static void AcceptedCallback(IAsyncResult asynResult)
        {
            connEvent.Set();
            Socket server = (Socket)asynResult.AsyncState;
            Socket handler = server.EndAccept(asynResult);
            Console.WriteLine("a client is connected:"+handler.RemoteEndPoint);
            
            server.BeginAccept(
                new AsyncCallback(AcceptedCallback),
                server);
            Receive(handler);
        }

        public static void Receive(Socket handler)
        {
            handler.BeginReceive(
                 byteData,
                 0,
                 byteData.Length,
                 SocketFlags.None,
                 new AsyncCallback(ReceivedCallback),
                 handler);
        }

        public static void ReceivedCallback(IAsyncResult asynResult)
        {
            Socket handler = (Socket)asynResult.AsyncState;
            int length = handler.EndReceive(asynResult);

            string receivedString = Encoding.ASCII.GetString(byteData,0,length);
            Console.WriteLine("received message:"+receivedString);


        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MyAsyncClient
{
    class Program
    {

        static void Main(string[] args)
        {
            startClient();
        }

        public static ManualResetEvent connEvent = new ManualResetEvent(false);
        public static ManualResetEvent receiveEvent = new ManualResetEvent(false);
        public static ManualResetEvent sendEvent = new ManualResetEvent(false);
        public static byte[] byteData = new byte[1024];

        public static void startClient()
        {
            IPAddress ipAddress = IPAddress.Parse("192.168.1.101");
            IPEndPoint ipEnd = new IPEndPoint(ipAddress,1002);
            Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            Console.WriteLine("client is start!");
            client.BeginConnect(
                ipEnd,
                new AsyncCallback(ConnectedCallback), 
                client);
            connEvent.WaitOne();

           

        }


        public static void ConnectedCallback(IAsyncResult asyncResult)
        {
           
            Socket client = (Socket)asyncResult.AsyncState;
            Console.WriteLine("connected to server:"+client.RemoteEndPoint);
            client.EndConnect(asyncResult);
            //connEvent.Set();
            send(client, "I am client");
        }


        public static void send(Socket client,string message)
        {
            byteData = Encoding.ASCII.GetBytes(message);
            Console.WriteLine("send a message:"+message);
            client.BeginSend(
                byteData,
                0,
                byteData.Length,
                0,
                new AsyncCallback(SendCallback),
                client);
            sendEvent.WaitOne();
        }

        public static void SendCallback(IAsyncResult asyn)
        {
            Socket client = (Socket)asyn.AsyncState;
            client.EndSend(asyn);

        }
    }
}

 

转载于:https://www.cnblogs.com/liuxinls/archive/2013/02/12/2910192.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值