Visual C # .NET Network Program Development - TCP (3)

xiaoxiao2021-03-06  43

Visual C # .Net web application development -Tcp papers (3) Author: Song Hua www.ASPCool.com time: 2002-5-5 17:48:38 Views: 6526

Comprehensive use of the above knowledge, the following example implements a simple network communication - dual-machine interconnect, the application is compiled for the client and the server. The client creates a connection to the server, send a connection to the remote host to request the connection signal, and send a conversation content; the remote host receives the connection from the customer, send a confirmation of the connection to the client, and receives and displays the client's conversation content.

On this basis, play your creativity, you can fully develop a chat room based on programming language (C #)! Client Main Source Code: Public Void Sendmeg () // Send Information {Try {INT Port = Int32.Pars (TextBox3.Text.tostring ()); // Remote Host Port Try {TcpClient = New Tclient (TextBox1.text, Port); // Create a TCPClient object instance} Catch (Exception le) {MessageBox.show ("TcpClient Error:" Le.MESSAGE); TolongTimeString (); // Get sending client time NetStream = tcpclient.getStream (); // Get network stream sw = new streamwriter (NetStream); // Create textWriter, write characters in the stream String Words = TextBox4.Text; // String content = strdateline words to be sent; // to send content sw.write (); // write stream sw.close (); // Close stream writer NetStream.close (); // Turn off the network stream TcpClient.close (); // Turn off the client connection} catch (exception ex) {messagebox.show ("Sending Message Failed!" EX.MESSAGE);} textbox4.text = ""; // Clear} Server-side main source code: public void startlisten () // User request {// receivemeg (); islinked = false; // connection flag try {int port = int32.parse (TextBox1.Text.toString ))); // Local standby-listening port serv Erlistener = new TCPListener (port); // Create a TCPListener object instance serverListener.Start (); // Start listening} catch ("can't Start Server EX.MESSAGE); RETURN } Islinked = true; while (true) // Enter the unlimited loop Wait for the user to connect {TRY {tcpclient = serverListener.accepttcpclient (); // Create a client connection object NetStream = tcpclient.getStream (); // Get network stream SR = New StreamReader (NetStream); // Flow Reader} Catch (Exception Re) {MessageBox.Show (Re.Message);} String Buffer = "; String Received ="

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

New Post(0)