Winsock网络通信过程

Windows Sockets是Windows下得到广泛应用的、开放的、支持多种协议的网络编程接口。

1、面向连接的流的方式调用过程(TCP):

                    Server端                                    Client端

     调用WSAStartup()初始化Winsock       调用WSAStartup()初始化Winsock
                        |                                                  |
     调用socket()创建一个监听Socket         调用socket()创建一个会话Socket
                        |                                                  |
     调用bind()为监听Socket指定通讯对象                  |
                        |                                                   |
     调用listen()设置等待连接状态                              |
                        |                                                   |
     调用accept()接收连接并生成会话scocket              |
                        |                                                   |
                        |                                       调用connect()与Server端连接
                        |                                                   |
     调用send和recv进行会话                        调用send和recv进行会话
                        |                                                   |
     closesocket()关闭socket   closesocket()关闭socket

 

2、面向无连接的数据报的方式调用过程(UDP):

                   Server端                                           Client端

     调用WSAStartup()初始化Winsock       调用WSAStartup()初始化Winsock
                        |                                                       |
     调用socket()创建一个监听Socket         调用socket()创建一个会话Socket
                        |                                                       |
                        |                                                       |
     调用sendto和recvfrom进行会话           调用sendto和recvfrom进行会话
                        |                                                       |
     closesocket()关闭socket                    closesocket()关闭socket

你可能感兴趣的:(Visual,C++)