C # simple interaction in the console in the network programming mode

Source: Internet
Author: User

C #Network Programming-interaction between customer mode and console

Today, let's take a look at how to implement the interaction between the server and the client in C #, and how to implement the interaction through a small program. To implement such a simple small program, although there are not many technical points involved, it is indeed a little difficult to understand, it is designed to delegate, multi-thread, TCP/IP network programming and other technical points. First, we will give a brief introduction to these technical points. In view of my limited technical skills, it is difficult to enter the elegant hall, and my personal statement is inevitable to make some mistakes, please forgive me.

We know that a method has no type and cannot be passed as a parameter. A delegate can bind a method so that it can be passed as a parameter.

The operating system uses processes to separate the different applications they are executing. A thread is the basic unit for the operating system to allocate the processor time. In a process, multiple threads can execute code simultaneously.

TCP/IP (Transmission Control Protocol/Internet Protocol).Transmission Control Protocol/Internet ProtocolIs also called the network communication protocol. This Protocol is the most basic protocol of the Internet and the basis of the Internet international network. To put it simply, it is composed of the IP protocol of the network layer and the TCP protocol of the transport layer. TCP/IP
Is a communication protocol for computers connected to the Internet. TCP/IP defines how devices (not just computers) connect to the Internet and how data is transmitted between them.

We know that TCP and UDP services usually have a relationship between customers and servers. For example, a Telnet service process starts to be idle on the system and waits for a connection. The user uses the Telnet client program to establish a connection with the service process. The customer program writes information to the service process. The service process reads the information and sends a response. The customer program reads the response and reports it to the user. Therefore, this connection is duplex and can be used for reading and writing.

How are the multiple Telnet connections between the two systems confirmed and coordinated? The TCP or UDP connection uniquely uses the following four items in each information for confirmation:

  1. Source IP Address: the IP address of the package to be sent.
  2. Destination IP Address: the IP address of the received packet.
  3. Source Port: the port connecting to the source system.
  4. Destination Port: the port connecting to the destination system.

A port is a software structure used by a client program or service process to send and receive information. A port corresponds to a 16-bit number. A service process usually uses a fixed port, for example, SMTP uses 25. These port numbers are 'widely known 'because they need to communicate with the destination address when establishing a connection with a specific host or service.

After learning about this, we start to return to our applet. This program contains the server (which can receive client information and respond to the client) and the client (which can receive server information and respond to the server ), before writing a program, you must reference the following reference spaces.

 

Create a server and listen for waiting for client connection

 

How can I read data streams obtained from the client? We use a class specifically defined by Microsoft for reading data streams to operate streamreader. Of course, this class corresponds to the streamwriter class for writing data streams.

 

After this step, you can receive the information sent by the client. We can define a method specifically to receive client information (of course, it can also be written in the main function, it is best to write a method for subsequent operations ).

 

Because the main () method is static, we define the receiving client information method as a parameter so that the server program can read and respond to the client without affecting each other ,. In order to keep the program running, you do not need to close the stream if you use an endless loop (remember to close the stream under normal operations ).

With the receiving method, define a response method, which is similar to the receiving method.

Different from the receiving method, the parameters received by the response method are of the object type. This is because to enable concurrent execution of the receipt and response, you need to create a thread for the response method and delegate the creation thread, thread delegation has only two types. One is non-parameter, the other is parameter type, and the other needs to transmit a stream object. Only the object type can be defined.

In this step, the server is basically built. Now, return to the main () method and start to call the Receiving Method and response thread:

 

Now, the server is finished. Then, there is no big difference between the client and the server when building the client, except that the tcplistener listening connection is not needed.

First look at the main function of the client:

 

Receive server information:

 

Response Server:

 

In this step, the program has come to an end. Next let's take a look at the running result (first start the server and run the client ):

 

The end is now. Due to limited time and space, this program only implements one server/client mode. If you are interested, you can complete the program to enable a server to connect multiple clients for server broadcast.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.