zoukankan      html  css  js  c++  java
  • 简单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!");
                    serverSocket.BeginAccept(
                        new AsyncCallback(AcceptedCallback),
                        serverSocket);
                    connEvent.WaitOne();
                   
                }
                catch (Exception)
                {
                    throw;
                }
            }
            public static void AcceptedCallback(IAsyncResult asynResult)
            {
                
                Socket server = (Socket)asynResult.AsyncState;
                Socket handler = server.EndAccept(asynResult);
                Console.WriteLine("a client is connected:"+handler.RemoteEndPoint);
                connEvent.Set();
                server.BeginAccept(
                    new AsyncCallback(AcceptedCallback),
                    server);
    
                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();
    
                send(client,"I am client");
    
            }
    
    
            public static void ConnectedCallback(IAsyncResult asyncResult)
            {
               
                Socket client = (Socket)asyncResult.AsyncState;
                Console.WriteLine("connected to server:"+client.RemoteEndPoint);
                client.EndConnect(asyncResult);
                connEvent.Set();
               
            }
    
    
            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);
            }
    
            public static void SendCallback(IAsyncResult asyn)
            {
                Socket client = (Socket)asyn.AsyncState;
                client.EndSend(asyn);
    
            }
        }
    }
  • 相关阅读:
    windows中android SDK manager安装更新sdk很慢,或者出现Done loading packages后不动甚至没有任何可用包
    实用小函数
    UML类图几种关系的总结
    TextView自动换行
    Android launchMode=singleInstalce 与onActivityResult
    红黑树
    二叉查找树相关算法
    添加google账户时无法与服务器建立连接
    ubuntu 下eclipse svn更改用户
    ubuntu adb: command not found
  • 原文地址:https://www.cnblogs.com/liuxinls/p/2910178.html
Copyright © 2011-2022 走看看