Analysis on socket programming in C #

Source: Internet
Author: User

C # Is a new language launched by Microsoft with VS.net. As an emerging language, it has strong C ++ and RAD features such as VB. Moreover, Microsoft launched C # mainly to combat Sun's Java. Everyone knows the powerful functions of Java, especially in network programming. Therefore, C # Naturally lags behind others in network programming. This article will introduce some basic knowledge about implementing socket (Sockets) programming in C #, so that you can have a general understanding of this. First, I will introduce the concept of socket.

Basic concepts of sockets:

Socket is the cornerstone of communication and the basic operation unit for network communication that supports TCP/IP protocol. The socket can be seen as the two-way communication endpoint of processes between different hosts. It forms a programming interface between a single host and the entire network. The socket exists in the communication domain. The communication domain is an abstract concept introduced to process General threads through socket communication. Sockets usually exchange data with sockets in the same domain (Data Exchange may also cross the boundaries of the domain, but some interpreter must be executed at this time ). Various processes use the same domain to communicate with each other using Internet Protocol clusters.

Sockets can be classified based on the communication nature, which is visible to users. Generally, applications only communicate with sockets of the same type. However, as long as the underlying communication protocol permits, different types of sockets can also communicate. There are two different types of sockets: stream socket and datagram socket.

Working principle of sockets:

To communicate over the internet, you need at least one pair of sockets, one running on the client, which we call ClientSocket, And the other running on the server, which we call ServerSocket.

Based on the connection start method and the target to which the local socket is to be connected, the connection process between sockets can be divided into three steps: server listening, client request, and connection confirmation.

A server listener is a server socket that monitors the network in real time, instead of locating a specific client socket, but waiting for a connection.

A client request is a connection request initiated by a client socket. The target is a server socket. Therefore, the client socket must first describe the socket of the server to be connected, point out the address and port number of the socket on the server, and then submit a connection request to the socket on the server.

Connection Confirmation means that when the server socket listens to or receives a connection request from the client socket, it responds to the request from the client socket and creates a new thread, send the description of the server socket to the client. Once the client confirms the description, the connection is established. The server socket continues to be in the listening status, and continues to receive connection requests from other client sockets.

Socket programming example in C:

By briefly introducing the basic concepts of sockets and the basic principles for implementing socket programming, I think you have a preliminary understanding of socket programming. However, the above is only about the basic concepts and principles. It still requires some work to be applied. The best way to really understand basic concepts and principles is to manually create an instance. Next I will introduce you to an example of using C # To implement socket programming-Chat Room program.

This program is based on the C/S (server/client) architecture. The program contains a server-side application and a client application. First, run the server-side application on the server, and the server listening starts as soon as the application runs. Then, you can open the client application on the client. After the program is opened, it can be connected to the server application, that is, client requests. After the connection is confirmed, the client user can chat with other client users. There is no limit on the number of clients, and the "whisper" chat mode is also supported, and chat records are supported. So this is a very good example of learning socket programming. In addition, the multi-thread mechanism is also used in the program to process the information of each client. After each client and server are connected successfully, a thread is established between them. After multithreading is used, the client does not affect each other, even if one of the errors occurs, it does not affect the other.

Related Article

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.