zoukankan      html  css  js  c++  java
  • 简单socket服务(三)实现多客户端向服务器发送数据并保证服务器接收到数据

    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();
                string mess;
                while((mess=Console.ReadLine())!="exit")
                //for (int i= 0; i < 10; i++)
                {
                    send(client,mess+"\r\n");
                }
               
            }
            
    
            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);
    
            }
        }
    }
    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);
                handler.BeginReceive(
                     byteData,
                     0,
                     byteData.Length,
                     SocketFlags.None,
                     new AsyncCallback(ReceivedCallback),
                     handler);
    
            }
        }
    }
  • 相关阅读:
    iOS
    iOS
    iOS
    CS页面-Asp.net+Spring.Net.Framework--SNF快速开发平台3.0
    SNF快速开发平台3.0之-界面个性化配置+10种皮肤+7种菜单-Asp.net+MVC4.0+WebAPI+EasyUI+Knockout
    SNF快速开发平台3.0之BS页面展示和九大优点-部分页面显示效果-Asp.net+MVC4.0+WebAPI+EasyUI+Knockout
    MVC通用控件库展示-MVC4.0+WebAPI+EasyUI+Knockout--SNF快速开发平台3.0
    已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。
    转:ECharts图表组件之简单关系图:如何轻松实现另类站点地图且扩展节点属性实现点击节点页面跳转
    转:zTree树控件入门之checkbox:如何动态设置节点的checkbox选择框启用与禁用状态(chkDisabled)
  • 原文地址:https://www.cnblogs.com/liuxinls/p/2910500.html
Copyright © 2011-2022 走看看