C # Network Programming Programming (3)

xiaoxiao2021-03-06  43

On the Network Programming C # (3) Author: Ma Jinhu www.ASPCool.com Time: 2002-6-25 18:00:50 Views: 6100

four. Key Steps and Solutions for Client Programming: (1). Connect to the specified port of the server: The local machine we use both both the server and the client, you can confirm the IP address to determine the server you want to connect. We use the "TCPClient" class when connecting, which provides TCP services on a higher abstraction level (higher than the socket class). The following code is to connect to the local machine (port 1234) and get the response stream: // Connect to the server port, here is the selected local machine as the server, you can change the server Try {myclient = new TCPClient by modifying the IP address. "LocalHost", 1234);} catch {messagebox.show ("Not connected to Server!"); Return;} // Creating a NetworkStream object Accepts and sends data NetWorkStream = myclient.getStream (); streamReader = New StreamReader; streamwriter = new streamwriter; (2). Implementation and sending data: In accepting and sending data, we still use the "NetWorkStream" class, because it is relatively simple to operate, specifically Implementation and acceptance or WriteLine () method of the "StreamWriter" class () method and "streamwriter" class in the "system.io" of the namespace. The specific implementation method is as follows: if (textBox1.text == ") {messagebox.show (" Please make sure the text box is not empty! "); TextBox1.focus (); return;} try {string s; // The current data stream is written to a string streamWriter.writeline (TextBox1.text); // Refresh the data streamwriter.flush () in the current data stream; // read a line from the current data stream, return value is character String s = streamreader.readline (); listbox1.items.add ("Read server-side sending content:" s);} catch (Exception EE) {MessageBox.show ("from server-side read data error, type To: " EE.TOSTRING ());} (3). The last step is the same as the server side, that is, to close the stream created in the program, as follows: streamReader.close (); streamwriter.close (); networkStream .Close ();

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

New Post(0)