A little note (2): Socket network programming

Source: Internet
Author: User

The flow of network communication:

Server-side
Request socket, bind socket to local, open port---listening port--wait for message---Read message


Client
Request socket-to-server initiated connection, you can send a message at any time after successful connection


What is a socket?
http://baike.baidu.com/link?url=BfywKEhH0b4vASGaFSZw9Dwwh2hQgV2DdE4POM2_5Xl80RK1_2hbH3YFQaFzfrxKeqh8SbcMcgRTP-Wj88Zb5a

A simple connection between a server and a client, the client sends a message, the server receives the message

Run the program with the console by using the startup parameter: C or S to run the client or server program

Query for related functions:

Int
Wsaapi
WSAStartup (
_in_ WORD wversionrequested,
_out_ Lpwsadata Lpwsadata
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fwsastartup); K ( WSAStartup); K (devlang-c%2b%2b); K (targetos-windows) &rd=true

Int
Wsaapi
WSACleanup (
void
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fwsacleanup); K ( WSACleanup); K (devlang-c%2b%2b); K (targetos-windows) &rd=true

SOCKET
Wsaapi
Socket
_in_ int AF,
_in_ int type,
_in_ INT protocol
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fsocket); K (socket) ; K (devlang-c%2b%2b); K (targetos-windows) &rd=true

Int
Wsaapi
Closesocket (
_in_ SOCKET S
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fclosesocket); K ( closesocket); K (devlang-c%2b%2b); K (targetos-windows) &rd=true

Int
Wsaapi
Bind
_in_ SOCKET S,
_in_reads_bytes_ (namelen) const struct SOCKADDR FAR * name,
_in_ int Namelen
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fbind); K (bind); K ( DEVLANG-C%2B%2B); K (targetos-windows) &rd=true

Int
Wsaapi
Listen
_in_ SOCKET S,
_in_ int Backlog
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2flisten); K (Listen) ; K (devlang-c%2b%2b); K (targetos-windows) &rd=true

SOCKET
Wsaapi
Accept
_in_ SOCKET S,
_out_writes_bytes_opt_ (*addrlen) struct sockaddr far * addr,
_inout_opt_ int FAR * Addrlen
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (winsock2%2faccept); K (Accept) ; K (devlang-c%2b%2b); K (targetos-windows) &rd=true

Int
Wsaapi
Recv
_in_ SOCKET S,
_out_writes_bytes_to_ (len, return) __out_data_source (NETWORK) Char FAR * buf,
_in_ int Len,
_in_ int Flags
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (WINSOCK2%2FRECV); K (recv); K ( DEVLANG-C%2B%2B); K (targetos-windows) &rd=true

Int
Wsaapi
Connect
_in_ SOCKET S,
_in_reads_bytes_ (namelen) const struct SOCKADDR FAR * name,
_in_ int Namelen
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winsock2%2fconnect); K ( connect); K (devlang-c%2b%2b); K (targetos-windows) &rd=true

U_long
Wsaapi
HTONL (
_in_ U_long Hostlong
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (winsock2%2fhtonl); K (htonl); k (devlang-c%2b%2b); K (targetos-windows) &rd=true

U_short
Wsaapi
Htons
_in_ U_short Hostshort
);
Msdn:
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (winsock2%2fhtons); K (htons); k (devlang-c%2b%2b); K (targetos-windows) &rd=true

A little note (2): Socket network programming

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.