In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the client programming method of C# network programming". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C # network programming client programming (1). Connect to the designated port on the server side:
The local machine we use acts as both a server and a client, and you can determine which server you want to connect to by changing the IP address. We use the "TcpClient" class when connecting, which provides TCP services at a higher level of abstraction (higher than the socket class). The following code connects to the local machine (port 1234) and gets the response stream:
/ / Connect to the server port. In this case, the local machine is selected as the server. You can change the server try {myclient = new TcpClient ("localhost", 1234) by changing the IP address;} catch {MessageBox.Show ("not connected to the server!" ); return;} / / create networkStream objects to receive and send data through network sockets networkStream = myclient.GetStream (); streamReader = new StreamReader (networkStream); streamWriter = new StreamWriter (networkStream)
C # network programming client programming (2). Implement to accept and send data:
We still use the "NetworkStream" class in receiving and sending data, because it is relatively easy to operate on it, and the specific implementation of sending and receiving is through the ReadLine () method of the "StreamReader" class in the namespace "System.IO" and the WriteLine () method of the "StreamWriter" class. The specific implementation methods are as follows:
If (textBox1.Text = "") {MessageBox.Show ("Please make sure the text box is not empty!" ); textBox1.Focus (); return;} try {string s; / / write a string streamWriter.WriteLine (textBox1.Text) to the current data stream; / / refresh the data streamWriter.Flush () in the current data stream; / / read a line of characters from the current data stream, and the return value is the string s = streamReader.ReadLine () ListBox1.Items.Add ("read from the server:" + s);} catch (Exception ee) {MessageBox.Show ("error reading data from the server, type:" + ee.ToString ());}
C # network programming client programming (3). Close the stream created in the program:
StreamReader.Close (); streamWriter.Close (); networkStream.Close (); "what is the client programming method of C# network programming"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.