Socket communication port Binding problem answer

Source: Internet
Author: User

If you want to get a deeper understanding of how it works: Read "UNIX network Programming"

Socket, we generally refer to a long connection, but there is a short connection, the correct explanation is:

1. If it is a short link, use the Sync socket. such as HTTP servers, forwarding servers, and so on.

2. If it is a long link, use an asynchronous socket. such as communication systems (Qq/fetion), webgame and so on.

Introduce me to the construction of the environment:

1. A mobile phone, iphone, used to do the client

2. A simulator, on Mac computer side, iphone Simulator, used to do the service side

3. Mobile phone and simulator are all in the same network segment;

Successful run Effect:

The first design of success:

1. The IP of the client is set to the IP of the server, and the port setting is the binding port set by the server;

2. The server IP is set to the real IP intranet address + bound port of the computer Mac, and the port is set by the servers themselves;

The second design of the success scenario:

1. The IP of the client is set to the IP of the server, and the port setting is the binding port set by the server;

2. Server-side IP is set to receive all IP (set method reference below code) + bound port, the port is set by the servers themselves;

1. Q: When the client and the server need to bind port and IP,

2. Q: What is the purpose of the client bound port? What is the role of binding IP?

3. Q: What is the role of the server-side binding port? What is the role of binding IP?

    • Server bound port, is to provide services on this port, so to speak, the service side will have a lot of different ports, to provide different services, so must bind the port;
    • Server-bound IP, the purpose of the binding server IP is to limit the service-side process created by the socket to accept only those destinations for this IP address of the client connection, limit, that is, if the client's destination IP is not a server, you can not communicate with the server;
    • If you do not want to limit it, refer to the following code:
SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);

The connection syntax used for the 1.UDP connection is:

SendTo () is a UDP datagram sent to a specified address, a struct sockaddr_in type that indicates where the UDP data is sent to.

Recvfrom () is a UDP datagram that is received from the specified address. The struct sockaddr_in type that indicates where the UDP datagram is received.

Refer to the environment of the user to build: The different place is:

Generally speaking on the internet is the same application, if so, the server and client IP and port settings should be interpreted as:

Because the server is always listening to have a client connection, if the server does not bind the IP and port, when the client on-line how to connect to the server, so the server to bind IP and port, and the client is not required, the client is active to the server to make a request, Because the server is already bound to the IP and port, so the client on-line to the IP and port to make a request, because the customer began to send data (send on-line request), the system assigns a random port to the client, this port and the client's IP will be sent to the server along with the online request, Once the service receives a live request, it can then receive the IP and port of the customer who sent the request, and the server can then take advantage of the IP and port to respond to the message from the client.

Reference: http://blog.csdn.net/suxinpingtao51/article/details/11809011

Socket communication port Binding problem answer

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.