Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the connection between C# server and client

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to achieve the connection between the C# server and the client. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Timing of the connection between the C# server and the client: when the server starts listening on the port, the client can be created to establish a connection with it. This is done by creating a type instance of TcpClient on the client side. Every time you create a new TcpClient, you create a new socket Socket to communicate with the server, and .net automatically assigns a port number to the socket. As mentioned above, the TcpClient class simply wraps the Socket. When you create an instance of the TcpClient type, you can specify the address and port number of the remote server in the constructor. At the same time of creation, a connection request ("handshake") is sent to the remote server, and once successful, the connection between the two is established. You can also use an overloaded no-argument constructor to create an object, and then call the Connect () method, passing in the remote server address and port number in the Connect () method to establish a connection to the server.

It is important to note that whether you use a parameterized constructor to connect to the server, or establish a connection to the server through the Connect () method, it is a synchronous method (or blocking, called block in English). It means that the client cannot proceed with subsequent operations until the client successfully connects to the server and the method returns, or the server does not exist and throws an exception. There is also a method called BeginConnect () that implements asynchronous connections so that the program is not blocked and can perform subsequent operations immediately, because the connection may take a long time to complete due to problems such as network congestion. There are a lot of asynchronous operations in network programming, everything is from simple to difficult, we will discuss asynchronous operations later, now only look at synchronous operations.

Create a new console application project, named ClientConsole, which is our client, and then add the following code to create a connection to the server:

Example of connection between C# server and client:

Class Client {static void Main (string [] args) {Console.WriteLine ("Client Running..."); TcpClient client = new TcpClient (); try {client.Connect ("localhost", 8500); / / Connect to the server} catch (Exception ex) {Console.WriteLine (ex.Message); return;} / / print the server information Console.WriteLine ("Server Connected!! {0}-> {1} ", client.Client.LocalEndPoint, client.Client.RemoteEndPoint); / / press Q to exit}} / / the server connects with the client

In the code above, we connect to the server by calling the Connect () method. Then we print the connection message: the local Ip address and port number, as well as the remote Ip address and port number to which we are connected. The Client property of TcpClient returns a Socket object whose LocalEndPoint and RemoteEndPoint properties contain local and remote address information, respectively. Run the server first, and then run this code. You can see that the output on both sides is as follows:

/ / Server: Server is running... Start Listening. / / client: Client Running. Server Connected! 127.0.0.1purl 4761-> 127.0.0.1purl 8500

We see that the port number used by the client is 4761. As mentioned above, this port number is randomly selected by .NET, it does not need us to set it, and the port number is different each time it is run. Open the Command prompt again, enter "netstat-a", and you can see the following C# server connecting to the client to implement the instance output:

TCPjimmy:8500 0.0.0.0:0 LISTENING TCPjimmy:8500 localhost:4761 ESTABLISHED TCPjimmy:4761 localhost:8500 ESTABLISHED

From this we can draw several important information: 1, port 8500 and port 4761 have established a connection, this port 4761 is the port that the client uses to communicate with the server; 2, port 8500 remains in the listening state after establishing a connection with the client. This means that a port can communicate with multiple remote ports. Obviously, the default port used by the well-known HTTP is 80, but how many browsers a Web server has to communicate with through this port.

After reading the above, do you have any further understanding of how to connect the C# server to the client? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report