网络通信的流程:

服务器端
申请套接字 -> 绑定套接字到本地,打开端口 -> 监听端口 -> 等待接受消息 -> 有消息之后,读取消息

客户端
申请套接字 -> 向服务端发起连接 -> 连接成功后,可以随时发送消息

啥是套接字?
http://baike.baidu.com/link?url=BfywKEhH0b4vASGaFSZw9Dwwh2hQgV2DdE4POM2_5Xl80RK1_2hbH3YFQaFzfrxKeqh8SbcMcgRTP-Wj88Zb5a

简单的一个服务器与一个客户端的连接,客户端发送消息,服务器接收消息

使用控制台运行程序,通过启动参数:c或者s来运行客户端或者服务器程序

一个小笔记(2):Socket网络编程-LMLPHP

一个小笔记(2):Socket网络编程-LMLPHP

一个小笔记(2):Socket网络编程-LMLPHP

一个小笔记(2):Socket网络编程-LMLPHP

一个小笔记(2):Socket网络编程-LMLPHP

一个小笔记(2):Socket网络编程-LMLPHP

相关函数的查询:

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

05-28 08:53