Analysis of the socket programming in C # (4)

xiaoxiao2021-03-06  52

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

Client program: 1. Open VS.NET, create a new C # template for the "Windows Application" project, may be named "chatclient". 2. Arrange the interface. Add a listbox control (used to display a list of users), a RichTextBox control (to display chat messages and system messages), a TextBox control (for sending messages), a checkbox control (determine if it is a whisper), one The StatusBar control and four Button controls ("Connections", "Disconnect", "Start Record", "Send"). Attribute settings of each control can be found in the specific settings in the source code, here from omnidia. The image behind the interface is as follows: 3. The code written by the client program. When the client tries and the server side, a connection must be established and registered to the server side. The ESTABLISHCONNECTION () function uses a TCPCLIENT to get a connection and create a networkStream to send a message. Also, the port number and the server are consistent, all of which are 5555. The EstablishConnection () function is as follows: private void EstablishConnection () {statusBar1.Text = "Connecting to server"; try {clientsocket = new TcpClient (serveraddress, serverport); ns = clientsocket.GetStream (); sr = new StreamReader (ns) Connected = true;} catch (exception) {MessageBox.show ("Unable to connect to the server!", "Error", messageboxbuttons.ok, messageboxicon.exclamation; statusbar1.text = "Disconnected";}} When the server is successfully connected, the program uses the registerwithserver () function to send a conn command to the server side. This command first sends the name of the user, then obtains a list of other users from the server side, all user lists are displayed in the ListBox control.

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

New Post(0)