"Windows Socket+udpserverclient"

Source: Internet
Author: User

Windows sockets+udpserverclient
      Winsock is the standard for socket under Windows .           1.UDP Socket Programming :
UDP (User Datagram Protocol) is a no-connection. Unreliable transmission of data, its characteristics are simple and fast. Compared with the TCP,UDP does not need to establish a connection (no need to connect, accept function), after the data sent to receive, do not need to terminate the connection. The UDP-based program avoids the overhead of TCP execution and performs better in both efficiency and speed. UDP is not connected, there may be data loss, delay. 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);first number of parameters AF: represents the family address. Network programming generally uses af_inet macros. the second parameter, type: Indicates the connection type, and TCP is optional for 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. second parameter: corresponding af_inet, using struct sockaddr_in including protocol. Ip,port and other information.

third parameter: corresponding af_inet, using struct sockaddr_in structure size. The function will be the socket with the protocol, IP address. The portport number is tied up. A unique identifier relative to the "name" of the socket. So that the other process can find the socket through this sign.

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: A buffer for sending data. Third parameter: The size of a data buffer. Fourth parameter: State of emergency, generally this is 0.

fifth number of parameters: Optional. Corresponding af_inet, the other IP. The socket address, such as port, identifies sockaddr_in. Sixth parameter: optional, corresponding 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. second parameter: A buffer that receives data. Third parameter: The size of a data buffer. Fourth: State of emergency. Generally this is 0. fifth number of parameters: Optional. Corresponding af_inet. Returns the other IP. The socket address, such as port, identifies sockaddr_in.

          sixth:   Optional, corresponding af_inet, using SOCKADDR_ In structure size.           This function is used for UDP to receive data. bind socket :
         int  bind ( socket s, const struct SOCKADDR *saddr,int namelen );           FIRST parameter:   The socket to be bound.           second:   corresponding af_inet, The use of struct sockaddr_in includes protocols, Ip,port and other information.

          third parameter:   corresponding af_inet. Use struct SOCKADDR_IN structure size.           This function binds the socket to the protocol, the Ip,port number. A unique identifier relative to the "name" of the socket. So that the other process can find the socket through this sign. close socket :
          int  closesocket (socket s ";           number of sockets.           This function is used to close the socket.

                     3. Kind :           Execute the server program in VC6.0, and then open a VC6.0 Execute client program:           Udpseverserver waiting for information from the client. When you receive the message, print it, and then send the time to the client today.           UdpClient the client sends a " hello,server! " Information, and then receives the time information from the server and prints it three times over again.
execution effect:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbg92zwnvzgvszxnz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
UdpClient Program:udpserver Program:

"Windows Socket+udpserverclient"

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.