"Windows SOCKET+UDP Server Client"

Source: Internet
Author: User

Windows sockets+UDP Server Client
      Winsock is the standard for socket under Windows .           1.UDP Socket Programming :
UDP (User Datagram Protocol) is a non-connected, unreliable data transmission, characterized by its simplicity and quickness. There is no need to establish a connection (without the Connect, accept function) compared to the TCP,UDP, and the connection is not terminated after the data is sent and received. The UDP-based program avoids the overhead of TCP operation and has better performance in efficiency and speed. UDP is non-connected, there may be data loss, delays, and these need to be handled by the application itself.           2.UDP Basic functions :           To Create a socket: Socket Socket (int af, int type, int protocol); the first parameter AF: Represents a family address, and network programming generally uses af_inet macros. the second parameter type: Indicates the connection type, and TCP optionally targets the connection SOCK_STREAM,UDP datagram Sock_dgram. the third parameter protocol: represents the Protocol, which is set to IPPROTO_UDP using the Af_inet family address UDP connection. To bind a socket:
int bind(SOCKET s, const struct SOCKADDR *saddr,int namelen); first parameter: The socket that needs to be bound. The second parameter: corresponding to the af_inet, using the struct sockaddr_in contains the protocol, IP, port and other information. third parameter: corresponds to af_inet, using struct sockaddr_in structure size. This function binds the socket to the protocol, IP address, port port number, and is relative to the "name" unique identifier of 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); first parameter: socket for each other's socket. second parameter: The buffer that sends the data. The third parameter: The size of the data buffer. Fourth parameter: State of emergency, generally this is 0. The fifth parameter: optional, corresponding af_inet, the other IP, port and other socket address identification sockaddr_in. Sixth parameter: optional, corresponds to af_inet, using sockaddr_in structure size. This function is used for UDP to send data. receive Data : int recvfrom(SOCKET S, char *buf, int len, int flags, struct sockaddr *from, int *fromlen );           first parameter: socket for each other's socket. The second parameter: a buffer that receives data. The third parameter: The size of the data buffer. Fourth parameter: State of emergency, generally this is 0. The fifth parameter: optional, corresponding af_inet, return the other IP, port and other socket address identification sockaddr_in. Sixth parameter: optional, corresponds to af_inet, using sockaddr_in structure size. This function is used for UDP to receive data. To bind a socket:
int bind(SOCKET s, const struct SOCKADDR *saddr,int namelen); first parameter: The socket that needs to be bound. The second parameter: corresponding to the af_inet, using the struct sockaddr_in contains the protocol, IP, port and other information. third parameter: corresponds to af_inet, using struct sockaddr_in structure size. This function binds the socket to the Protocol, IP, and port number, relative to the "name" of the socket, so that the other process can find the socket by this identifier. Close Socket :
int closesocket(SOCKET s); The parameter is the socket. This function is used to close the socket. 3. Kind : run the server program in VC6.0 first, and then open a VC6.0 to run the client program:The udpsever server waits for information from the client, prints it after it receives the message, and then sends the current time to the client. udpclient client sends a "hello,server!" to the server Information, and then receives the time information from the server and prints it, repeating it three times.
Operating Effect:
UDP client program: UDP Server program:

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.