Analysis of the socket programming in C # (2)

xiaoxiao2021-03-06  41

Analysis of socket programming in C # (2) Author: Wang Kaiming www.ASPCool.com Time: 2002-4-10 21:17:46

Below, I will introduce this example to you: server-side programs: 1. Open vs.net, create a new C # template for the "Windows Application" project, may wish to name "ChatServer". 2. Arrange the interface. Simply add a ListBox control on the interface, which is mainly used to display some information about the client's user. The image is as follows: 3. The code written by the server-side program. For the server, the main role is to listen to the client's connection request and confirm its request. The program opens a startLisTening () thread. private void StartListening () {listener = new TcpListener (listenport); listener.Start (); while (true) {try {Socket s = listener.AcceptSocket (); clientsocket = s; clientservice = new Thread (new ThreadStart (ServiceClient) ClientService.Start ();} catch (eXception e) {console.writeline (E.TOString ());}}} The thread is always running. Once the server is received to receive a connection request from the client, it opens a serviceclient () thread to serve the client. When a connection is established, each client is given a socket that belongs to it. At the same time, an object of a Client class is established. This object contains some related information of the client, which is saved in an array list. The CLIENT class is as follows (you can see the client.cs file in the source code): use system; use system.threading; namespace chatserver {using system.net.sock; using system.net; ////// Client Summary Description .

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

New Post(0)