Small example of socket programming under Windows

Source: Internet
Author: User
Tags new set

Service side
      1. First introduce header file Winsock2.h and library file Ws2_32.lib (development environment is vs2015)
      2. Loading socket libraries and creating sockets
      3. Bind sockets to an IP address and port
      4. Listening for connection requests from clients
      5. Receive or send a message
      6. Close socket, socket font
Client
      1. First introduce header file Winsock2.h and library file Ws2_32.lib (development environment is vs2015)
      2. To load a socket font, create a Socket object
      3. Making a connection request to the server
      4. Send and receive information
      5. Closing sockets and socket libraries
Comments

The WSAStartup structure mainly contains the Winsock version information supported by the system.

WSAStartup () is used to initialize Windows Sockets and returns the Wsadata struct, and only after the function has been called can the program invoke other Windows Sockets API functions for network communication. The first parameter of the function is the version number and the second is used to receive the returned wsadata struct body.

Socket PASCAL FAR socket (int af, int type, int protocol); The call receives three parameters: AF, type, protocol. Parameter AF Specifies the area in which the communication occurs: only Af_inet is supported in Windows, which is the internetwork zone. The parameter type describes the type of socket to be established. There are three kinds of: first, TCP streaming sockets (SOCK_STREAM) provides a connection-oriented, reliable data transmission service, which is error-free, non-repeatable, and received in order of sending. Internal flow control to avoid data flow overrun; Data is considered a byte stream, no length limit, and the second is datagram Socket (SOCK_DGRAM) provides a non-connected service. Packets are sent in a separate package, without error-free guarantees, data may be lost or duplicated, and the order of reception is confusing. The third is the original socket (SOCK_RAW), which allows direct access to lower-level protocols such as IP and ICMP. The parameter protocol describes the specific protocol used by the socket, and if the caller does not want to specifically specify the protocol used, set to 0, using the default connection mode.

int bind (int socket, struct socketaddr *address, int addr_len) function: Binds the created socket to the adress (contains IP and port information). Return value: Normal returns 0, when the error is returned-1 parameter socket description will be used by the socket. The parameter Addr_len describes the length of the parameter adress. The parameter adress describes the address that will be bound.

int listen (int socket, int backlog) function: Defines how many pending connections can be on the specified socket. Return value: 1 is returned when an error occurs. The parameter socket is the set of interface file descriptors that are returned by the calling socket (). The parameter backlog is the number of connections allowed in the incoming queue.

int accept (int socket, struct socketaddr *address, int addr_len) function: Receive client connection request. Return value: If the connection succeeds, the function returns a new set of interface file descriptors. Next, you can send (send ()) and receive (RECV ()) operations on this descriptor. Return On Error-1

Parameter socket: set interface file descriptor. A pointer to the address struct:sockaddr_in parameter. Parameter addrlen: length, often sizeof (struct sockaddr_in)

int connect (int socket, struct sockaddr *serv_addr, int addrlen) function: The client is used to connect to the server. return value: The 1 parameter socket socket interface file descriptor is returned when an error occurs. Parameter serv_addr contains the server's address and port information parameter Addrlen length, usually sizeof (struct sockaddr_in)

Approximate process:

Reference from:

Http://www.cnblogs.com/Sniper-quay/archive/2011/06/22/2086636.html

Small example of socket programming under Windows

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.