[Windows socket + UDP Server Client]

Source: Internet
Author: User

Windows Socket + UDP Server Client
WinsockWindows Socket Standard.1. UDP socket programming:
UDP (User Datagram Protocol) is a connectionless and unreliable data transmission. It features simplicity and quickness. Compared with TCP, UDP does not need to establish a connection (connect and accept functions are not required). After sending and receiving data, the connection does not need to be terminated. The UDP-based program avoids the overhead of TCP operation and performs better in terms of efficiency and speed.UDP is connectionless and may result in data loss and delay, which must be processed by the application itself.2. UDP basic functions:Create a socket:SOCKET socket (int af, int type, int protocol );The first parameter af indicates the family address. AF_INET macro is generally used for network programming.The second parameter type indicates the connection type. TCP can be used to connect SOCK_STREAM and UDP datagram SOCK_DGRAM.The third parameter protocol indicates the protocol. Set it to IPPROTO_UDP when using the AF_INET address UDP connection.Bind socket:
Int bind (SOCKET s, const struct sockaddr * saddr, int namelen );The first parameter: the socket to be bound.The second parameter corresponds to AF_INET and uses struct sockaddr_in to include the Protocol, IP address, port, and other information.The third parameter: corresponds to AF_INET, and uses the struct sockaddr_in structure size.This function binds the socket to the protocol, IP address, and Port number, which is a unique identifier compared to "name" the socket, so that other processes can find the socket through this identifier.Send data:Int sendto (SOCKET s, const char * buf, int len,Int flags, const struct sockaddr * to, int tolen );The first parameter: socket is the socket of the other party.The second parameter is the buffer for sending data.Third parameter: data buffer size.The fourth parameter is an emergency. Generally, this parameter is 0.Fifth parameter: (optional) specifies the socket address, such as AF_INET, peer IP address, and port, to identify sockaddr_in.Sixth parameter: (optional) The size of the sockaddr_in structure corresponding to AF_INET.This function is used to send data over UDP.Receive data:Int recvfrom (SOCKET s, char * buf, int len,Int flags, struct sockaddr * from, int * fromlen );The first parameter: socket is the socket of the other party.The second parameter is the buffer for receiving data.Third parameter: data buffer size.The fourth parameter is an emergency. Generally, this parameter is 0.Fifth parameter: (optional) it corresponds to AF_INET, And the socket address, such as the IP address of the other party, is returned to identify sockaddr_in.Sixth parameter: (optional) The size of the sockaddr_in structure corresponding to AF_INET.This function is used to receive data from UDP.Bind socket:
Int bind (SOCKET s, const struct sockaddr * saddr, int namelen );The first parameter: the socket to be bound.The second parameter corresponds to AF_INET and uses struct sockaddr_in to include the Protocol, IP address, port, and other information.The third parameter: corresponds to AF_INET, and uses the struct sockaddr_in structure size.This function binds the socket to the protocol, IP address, and port number. It is a unique identifier relative to the socket name, so that other processes can find the socket through this identifier.Disable socket:
Int closesocket (SOCKET s );The parameter is socket.This function is used to close the socket.3. Cow test:Run the server program in VC6.0, and then open a VC6.0 to run the client program:The udver server waits for the information from the client, prints the information, and sends the current time to the client.The UDPclient client sends a "Hello, server!" message to the server !" Information, and then receive and print the time information from the server, repeated three times.
Running effect:
UDP client program:UDP server program:

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.