Windows Sockets programming

Source: Internet
Author: User

the Internet is becoming more and more frequent in our lives. Networking has become a trend of social development. WinSocket is a common method of network programming.

First of all, we talk about the basic knowledge of computer networks. The first is the IP address, and each host in the network has an IP address. (a host can have multiple IPs, but in each network each host must have a unique IP, for example, your computer has two IP one for 192.168.1.34 and the other for 192.168.2.34   This shows that this host is in the two point 1 network segment. Also in the DOT 2 network segment inside   1 network segment on the computer can find 192.168.1.34,2 network segment computer is not found 192.168.1.34) network equivalent to a community, you equivalent to host, you in a community called XX, in the B community inside you may call yy. It is equivalent to having an identity card in a community where others can find you through this "id". Then come and see the protocol. The so-called protocol is a written communication rule. In fact, my Chinese, English are agreements, they are used to deal with people to communicate the agreement, and TCP/IP,UDP/IP are used in the Computer network communication protocol. The concept of a port. When host A through the network to send data to Host B, b host There are many processes, in the end, which process is receiving data? So we're going to assign a port number to each process. The port number corresponds to the process. TCP/IP programming corresponds to the client and server side. Server-side service delivery. The client requests the service. So the service side is a passive process. The client is an active process. This is the process of the server-side program. Initial network protocol WSAStartup, set up (socket) socket, bind (BIND) socket, bind the time with the native sockaddr (this record is the local IP and port number), in fact, the socket is bound to the IP and port of the machine, Listening (listen) is the socket set to listening mode for listening to the network. Acceptance (accept) when there is a client request service on the network, accept the customer's request, return a new socket socketconnet for this service, and record the client's IP and port with a sockaddr variable. Then it is sending and receiving data. When the data is received recv, the parameter of the function is the socketconnet established by this connection. sending data is send. One parameter of the function is the socket for this connection socketconnet . Finally, close the socket.

For clients: The client is an active process. His process is 1 to build sockets and sockaddr_in SVR. The SVR is used to record the IP and port number of the service. 2 want the server sends the connection request, the client does not need to bind the machine's, he is establishes a connection, uses the Connet function, wants you to go to the bank to transact the business, you do not stay in the bank year-round, as long as you want to transact the business the time to go once on the line. You do this one time. A connection request. 3 After the connection is complete, we can establish a contact to send and receive data. Finally, close the socket.

When the UDP protocol is transmitted, the connection is not established, so the reliability is lower than TCP. The server-side program flow for the UDP protocol is this: 1 the socket is established and the local sockaddr svr;2 bind (BIND) binds the socket to the local IP and port. 3 The SOCKADDR client is established to record client data and receive data. Recvfrom. The client's process is to establish socket and server sockaddr, send data, and accept data.


No matter what the deal. 1 servers are bound to the socketaddr and sockets of the native computer.

2 when you send and receive data, you need to know each other's sockaddr. The client is defined beforehand. And the service side is not know what kind of customer to apply for service, so the server side is in interactive get client sockaddr. The TCP server side is the time to accept the connection, record the socketconnet of this connection, and UDP does not establish a connection, is at the time of acceptance (recvfrom).

3 UDP has good real-time performance, poor reliability, poor real-time TCP, and good reliability.







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.