Project summary--simple and simple socket network programming

Source: Internet
Author: User

Directory (?) [-] Preface one official explanation socket mechanism two popular understandings socket Three example server code server interface client code Client interface Four summary establish a socket binding port listen for a connection request from a server to accept a client specifically preface:

Why there is the concept of the title, I would like to have not actively heard of the socket network programming people to read the topic may have been blindfolded, in order to well let everyone into the scene, first of all to say a need to use this thing business needs.

First of all, we should be clear that the socket network programming is based on the CS model, such as the computer room fee system, in the computer room fee system may encounter different computer room use of the charging system is the use of the same database. However, for some of the displayed information, you can only update to the form every time you query the database. Take the simplest main interface of the current number of machines, there are room A and room B two rooms, and they are using the same database, and then two on duty teachers at the same time work, but the engine room A to increase or reduce the number of people on the machine , the room B teacher on duty is unable to receive the message in real time and update his form. This causes a temporary error in the information displayed by the form. Then we use the socket network programming.

first, the official interpretation of the socket mechanism:

The most common scenario in network programming is the Client/server (client/server) model. In this scenario, the client application requests services from the server program. A service program listens to requests for services at a well-known address, that is, the service process is dormant until a client requests a connection to the address of the service. At this point, the service program is "awakened" and provides service to the customer-respond appropriately to the customer's request.

In order to facilitate this client/server model of network programming, in the early 90, Microsoft joined several other companies to develop a set of Windows network programming interface, that is, the WindowsSockets specification, it is not a network protocol, but a set of open , a network programming interface under Windows that supports multiple protocols. Now the Winsock has basically implemented the Protocol independent, you can use Winsock to invoke the functionality of a variety of protocols, but the more commonly used is the TCP/IP protocol. The socket actually provides a communication port on the computer that can communicate with any computer with a socket interface through this port. The application is transmitted over the network and the received information is implemented through this socket interface.

second, the popular understanding of the socket

We can simply interpret the socket as a conduit between different computer programs on the network, and throw a bunch of data from the end of a pipe, from the B side of the pipe (and perhaps from C, D, E, F ...). End up). The port of the pipe is uniquely identified by two factors, that is, the IP address of the machine and the port number used by the program. The meaning of the IP address everyone knows that the so-called port number is a programmer to specify a number, many well-known Trojan programs all day on the network scan different port number is to obtain a port can be connected to destroy. The more famous port number has HTTP 80 port, of course, we recommend that you write the program do not use too small port number, they are generally occupied by the system, also do not use some well-known ports, generally use 1000~5000 within the port is better.

The socket can support the sending and receiving of data, and it defines a variable called a socket. A socket is first created when the data is sent, and then a ip/port is sent using methods such as send to the socket, and the receiver first creates the socket and then binds the socket to a ip/port. All data sent to this port is read by functions such as the recv of this socket. The same as reading the data in the file.

Third, examples:

Said so much I think we have a simple understanding of the socket, of course, or to the code in depth understanding.

Before looking at the code, but also to do the final explanation, socket programming must use the client and server, although the next example is only one-to-one communication, but in fact, and a one-to-many communication is a principle.

1. Server Code

First look at the server code:

[CSharp]  View plain copy print? /*********************************************************   *  Developer: Hanyi    *  Create Time:2013/9/27 13:51:48   *  Description: Socket network becomes instance server    *  version:1.0   *    All rights reserved: Information technology accelerated            * ******************* /   using system;   using  system.collections.generic;   using system.componentmodel;   using System.Data;    using system.drawing;   using system.linq;   using system.text;    using system.windows.forms;   using system.net;   using  system.net.sockets;      namespace server   {        public partial class server : Form       {    &Nbsp;          Socket s = null;            IPEndPoint iep = null;            byte[] buf = new byte[1024];            Socket worker = null;               public server ()             {               initializecomponent ();                 control.checkforillegalcrossthreadcalls = false;            }           //start the server. --Hanyi            privAte void button1_click_1 (object sender, eventargs e)             {                //Create a channel  &n

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.