Simple network communication with examples - Double machine interconnection

xiaoxiao2021-04-05  252

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.parse (TextBox3.Text.toString ()); // Remote host port try {tcpclient = new TcpClient (TextBox1.text, port); // creation TcpClient object instance} catch (Exception le) {MessageBox.Show ( "TcpClient Error:" le.Message);} string strDateLine = DateTime.Now.ToShortDateString () "" DateTime.Now.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 String String String Content = strdateline words; // to send content sw.write (content); // 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:

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

New Post(0)