[Windows socket programming + Server Client]

Source: Internet
Author: User

Windows Socket programming and Server Client example
WinsockWindows Socket Standard.Socket is based on a computer network and provides inter-process communication between different processes on the same system or different machines connected by a LAN. For example:
The socket identifies the IP address and Port number. You can locate a socket in the LAN and transmit data to each other through the socket process. For example, to communicate between process A and process B through A socket, process A creates A socket with an IP address and A unique identifier of the port number. process B also creates an IP address, the socket uniquely identified by the port number. process A and process B can send and receive information through the socket of the other party.
1. database support:
Winsock API functions are supported by WS2_32.DLL and can be accessed through WS2_32.LIB. Before Windows socket programming, you must initialize WS2_32.DLL and use the han function WSAStartup to complete initialization.# Include <winsock2.h># Pragma comment (lib, "ws2_32.lib") // load ws2_32.lib staticallyWS2_32.DLL initialization:IntWSAStartup (WORD wVersionRequested, LPWSADATA lpWSAData );The first parameter of this function is WS2_32.DLL, which is usually set to MAKEWORD (2, 0). The second parameter is a pointer to WSADATA, which is used to return the configuration information of WS2_32.DLL. This function must be called before Socket programming.WS2_32.DLL release:
Int WSACleanup ();This function is used to release WS2_32.DLL. This function is called when the Winsock function is not required. 2. Basic API functions:
Create a socket:SOCKETsocket (int af, int type, int protocol );The first parameter af indicates the family address. Generally, the AF_INET macro is used.The second parameter "type" indicates the connection type. It is optional for connection SOCK_STREAM and the datagram SOCK_DGRAM. SOCK_STREAM is generally used.The third parameter protocol indicates the protocol. When AF_INET is used, it is set to IPPROTO_TCP.Bind socket:
Intbind (SOCKET s, const struct sockaddr * saddr, int namelen );The first parameter: the socket to be bound.The second parameter: Generally, struct sockaddr_in is used to contain the protocol, IP address, port, and other information.Third parameter: The structure size of struct sockaddr_in is generally used.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.Listening socket:
Intlisten (SOCKET s, int nQueueSize );The first parameter is the socket of the listener.The second parameter is the maximum number of connection requests in the socket listener queue.This function listens to socket connection requests.Request connection:
Intconnect (SOCKET s, const struct sockaddr * saddr, int namelen );The first parameter is the socket of the local process of the socket.The second parameter: the socket address, such as the Peer IP address and port, identifies sockaddr_in.The third parameter is the size of the sockaddr_in structure.Accept connection requests:
SOCKETaccept (SOCKET s, struct sockaddr * addr, int * addrlen );The first parameter: socket is the socket to be listened on.The second parameter is a sockaddr pointer, which will write the sockaddr_in information of the sender.The third parameter is the size of the sockaddr_in struct.This function is used to accept a socket connection request, return a new connection socket (essentially the socket of the requester), and send and receive data through this connection socket.Send data:
Int send (SOCKET s, const char * buf, int len, int flags );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.This function is used to send data to the socket of the other party, and the size of the sent data is returned successfully.Send data:
Int recv (SOCKET s, char * buf, int len, int flags );The first parameter: socket is the socket of the other party.The second parameter is the buffer for receiving data.The third parameter is the buffer size.The fourth parameter is an emergency. Generally, this parameter is 0.This function is used to receive the data sent by the other party and returns the size of the data sent successfully.Disable socket:
Intclosesocket (SOCKET s );The parameter is socket.This function is used to close the socket.

 

3. Cow test:

In VC6.0, first run the server program, then open a VC6.0, and run the client program.


Running effect:


Server programs:

Client 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.