Asynchronous TCP connection server side

xiaoxiao2021-04-06  242

TcpTranslate is a transfer auxiliary class, and this code is not complete. To be renewed .....

Using

System;

Using

System.IO;

Using

System.Text;

Using

System.net;

Using

System.net.sockets;

Using

System.collections.Generic;

Namespace

SocketServer

...

{/ ** ////

/// Network communication event model commission /// /// /// Connection class public delegate void NetEvent (Object sender, tcptranslate e); ////// /// listening port, accept multipliers TCP connection /// public class tcpserver ... {Event Definition #Region Event Definition / ** //// /// Receive Client Connection Event /// Public Event NetEvent Connected; / ** // // /// /// public event NetEvent disconnect; #endregion field #Region field private int _MAXSOCKETS = 1000; // Maximum customer connection private iPndPoint_IEP; / Listening to the endpoint private tcplistener _Listener; // listening to private dictionary _Session; #endregion property #Region property / ** //// /// current customer connection number /// Public int connectioncount ... {get ... {RET URN _Session.count;}} / ** //// /// All socket /// /// constructor /// /// listening port number /// Maximum customer connection public TCPServer (int map, int port) ... {_maxsockets = maxsockets;

_IEP = New IpendPoint (ipaddress.parse (ip), port);} public tcpserver (int port): this (1000, "0.0.0.0", port) ... {} #endregion common method #Region utility / * * ////

/// Start the server program, start listening to client request /// public void start () ... {_SESSION = New Dictionary (); _Listener = New tcplistener (_Listener.Start (10); // listening to client connection request _Listener.BeginAcceptSocket (New AsyncCallback (ClientConnect), _Listener;} private void clientconnect (IasyncResult ar) ... {// Accept customer's connection, have connections Socket Socket _socket = _listener.EndAcceptSocket (ar); TcpTranslate trans = new TcpTranslate (_socket); trans.DisConnect = new NetEvent (trans_DisConnect); // start accepting data trans.StartReceive (); // client Connection Success Event IF (Connected! = NULL) Connected (th IS, TRANS; TCPTranslate.writeline () "IS connection ... Num" _Session.count.toString ()); if (_Session.Containskey (trans.RemoteEndPoint)) ... { _Session [trans.remoteendpoint] = Trans.} else ... {_Session.Add (Trans.RemoteEndPoint, Trans.RemoteEndPoint, Trans.}) ... {// Continue to listen to client connection request _Listener.BeginAcceptsocket (New AsyncCallback (ClientConnect), _Listener;

转载请注明原文地址:https://www.9cbs.com/read-132331.html

New Post(0)