SuperSocket entry (1)-Telnet server and client request processing, supersocket-telnet

Source: Internet
Author: User

SuperSocket entry (1)-Telnet server and client request processing, supersocket-telnet

The console project in this article is debugged according to the official SuperSocket Telnet sample code. The official example code is the Telnet sample code.

Start my first Telnet console project tour:

Create console project: Open the vs program, file =, new =, and project

1. Download The SuperSocket dynamic library in two ways.

1. Add the dll file of the SuperSocket (SuperSocket. common. dll, SuperSocket. socketBase. dll, SuperSocket. socketEngine. dll) to reference this project, directly download the SuperSocket component on the official website, introduce the dll file:

 

2. I prefer this method through nuget. You can update the component version. (Note: I am using vs2015 to directly open nuget. If you are using an earlier version of vs, You need to download the nuget component)

Click Browse, enter supersocket, and select component installation.

After adding the component, You need to compile the config file into the content. log4net needs to use the config file.

 

View the overall directory of the introduced project. The red line of the box shows the introduction of supersocket components and files.

2. start and stop the test service using the officially provided code for starting the server

1 /// <summary> 2 // SuperSocket service starts or stops 3 /// </summary> 4 /// <param name = "args"> </param> 5 static void Main (string [] args) 6 {7 Console. writeLine ("press any key to start the SuperSocket service! "); 8 9 Console. ReadKey (); 10 Console. WriteLine (); 11 12 var appServer = new AppServer (); 13 14 // start Application Service port 15 if (! AppServer. Setup (2017) // listening port 201716 {17 Console. WriteLine ("service port failed to start! "); 18 Console. ReadKey (); 19 return; 20} 21 22 Console. WriteLine (); 23 24 // try to start Application Service 25 if (! AppServer. Start () 26 {27 Console. WriteLine ("service startup failed! "); 28 Console. ReadKey (); 29 return; 30} 31 32 Console. WriteLine (" service started successfully. Please press 'e' to stop the service! "); 33 34 while (Console. ReadKey (). KeyChar! = 'E') 35 {36 Console. writeLine (); 37 continue; 38} 39 40 // stop service 41 appServer. stop (); 42 43 Console. writeLine ("Service stopped! "); 44 Console. ReadKey (); 45}
Telnet Service Startup and Shutdown

 

Run the project:

After running successfully, you can view the logs log tracking file in the Debug folder. From the log, we can see that the server is successfully started, but this is useless, because we have not processed the customer's link and processed the customer's sent information. According to the official documentation, we continue to copy the code to improve our program.

3. Register client links and process client sending information

1. Process client connection and register session to create an event handling method

AppServer. NewSessionConnected + = new SessionHandler (appServer_NewSessionConnected );

2. Send the welcome information to the client in the event processing code.

Static void appServer_NewSessionConnected (AppSession session)

{

Session. Send ("Welcome to SuperSocket Telnet Server! ");

}

3. Use the Telnet client for testing (note: the telnet service must be installed before using telnet)

Run the telnet server

 

Run Windows + r command cmd and enter telnet localhost 2017. (Port 2017 is the port used to start listening in the Program)

Press enter to display the welcome message indicating that the connection between the client and the server is successful.

4. process the information sent by the client. I will continue to copy the code.

1 // <summary> 2 // client request processing 3 /// </summary> 4 // <param name = "session"> session </param> 5 /// <param name = "requestInfo"> Request Information </param> 6 7 static void appServer_NewRequestReceived (AppSession session, stringRequestInfo requestInfo) 8 {9 switch (requestInfo. key. toUpper () 10 {11 case ("ECHO"): 12 session. send (requestInfo. body); 13 break; 14 15 case ("ADD"): 16 session. send (requestInfo. parameters. select (p => Convert. toInt32 (p )). sum (). toString (); 17 break; 18 19 case ("MULT"): 20 21 var result = 1; 22 23 foreach (var factor in requestInfo. parameters. select (p => Convert. toInt32 (p) 24 {25 result * = factor; 26} 27 28 session. send (result. toString (); 29 break; 30} 31}
Client sends request processing

5. Register a request event

1 appServer.NewRequestReceived += new RequestHandler<AppSession, StringRequestInfo>(appServer_NewRequestReceived);
Register client request events

Running effect:

In addition to using the telnet client, there is also a tcp/udp test tool on the Internet that can be linked to the server.

Today's supersocket framework is getting started. I have limited skills and skills. If you have any mistakes, please let us know!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.