"Tcp-ip Learning Summary" Socket programming Basics/Network Programming Basics

Source: Internet
Author: User

Communication between the two network programs requires a link channel between them, which consists of a five-tuple
Five tuples:(protocol, local IP address, local port number, remote IP address, remote port number)

Socket programming principle Socket Socket Interface

Communication between network programs relies on the socket socket interface for communication.
For example: Host A above the QQ program needs to send messages to Host B above the QQ, the steps are:
① the QQ message above host A is stored in the socket above host a
② this socket through the network management software of host A through the network interface card to the Host B network interface card
③ Host B's network interface card receives the message and then passes it to Host B's network management software and then to Host B's socket.
④ then Host B has QQ read the information in the socket

Socket Interface Classification

① Flow Socket Interface (SOCK_STREAM) reliable connection-oriented data transfer service (for TCP)
② Datagram Socket Interface (SOCK_DGRAM) No connection unreliable two-way data transfer service (using with UDP)
③ Original Socket Interface (Sock_ram) rarely used

Socket Programming Principle

The ① server invokes the socket () function to open a service, which is to create a set of interfaces
② This socket bar binds a port to this socket with the bind () function
③ calls the Listen () function to listen for client requests
The ④ client also calls the socket () function to wear a socket for a request service, and then calls the Connect () function to request a connection
The ⑤ server calls the Accept () function to receive the request connection and then reads the data from the socket using the recv () function, calling the Send () function to send the data to the socket interface
⑥ closing the Socket interface resource

Establishment of the Winsock API socket interface Connection

①wsastartup () Check the installation of the protocol stack
②socket () function Create socket socket

SOCKET socket (int af,//地址族AF_INETinttype,//套接口类型SOCK_STREAM(TCP),SOCK_DGRAM(UDP)int protocol//协议字段IPPROTO_TCP, IPPROTO_UDP)

③bind () binds the address, which is to connect a set of interfaces with the host address and port number. No error returned 0

int bind(    SOCKET s,//套接口    conststruct sockaddr FAR* name,//IP地址和端口地址    int namelen//地址参数的长度)

About address parameters,

struct sockaddr_in {    short sin_family;//必须是AF_INET    u_short sin_port;//16位端口号    struct in_addr sin_addr;//IP地址    char sin_zero[8];//一般是0}

④ Listening Connection listen () when a set of interfaces is created and then bound, the call to the Listen () function listens for connection requests without errors returning 0

int listen(    SOCKET s,//已绑定了地址,还没有建立连接的套接口    int backlog//指定正在等待连接的最大队列长度)

⑤ Request Connection Connect () The client socket interface is created so you can call this function to connect

int connect(    SOCKET s,    conststruct sockaddr FAR* name,    int namelen)

⑥ receive connection accept () the server receives a connection request from the client

SOCKET accept(    SOCKET s,//处于监听状态的套接口    struct sockaddr FAR* addr,//用来存放发出连接的那个客户端的IP地址信息    int FAR* addrlen//客户端接口地址的长度)
Winsock API basic data Transfer data transfer function used by TCP

①send () function to send data on a connected set of interfaces

int send(    SOCKET s,//已建立连接的套接口    constchar FAR*buf,//字符缓冲区,内有将要发送的数据    int len,//即将发送的缓冲区的字符数    int flags//控制传输方式,可以是0)

②RECV () function to receive data from a socket interface

int recv(    SOCKET s,//已建立的套接口    char FAR* buf,//用于接收数据的缓冲区    int len,//缓冲区的长度    int flags//调用方式 ,正常为0)
Data transfer functions used by UDP

①recvfrom () Receive data

int recvfrom(    SOCKET s,//一个套接口    char FAR* buf,//接收数据的缓冲区    int len,//缓冲区长度    int flags,//调用操作方式    structfrom//源地址,向谁要信息    int FAR* fromlen//)

②sendto () Send data

int sendto(    SOCKET s,//套接口    constchar FAR* buf,//将要发送数据的缓冲区    int len,//buf缓冲区长度    int flags,//调用方式    conststruct sockaddr FAR* to,//目的套接口地址    int tolen//to所指地址长度)
Winsock API connection and shutdown socket interface

①shutdown () Close the read-write channel

shutdown(    SOCKET s,//套接口    int how//关闭时禁止那种操作)

②colsesocket () closing the socket connector

int closesocket(    SOCKET s)

③wsacleanup () terminating the use of Winsock

int WSACleanup();

"Tcp-ip Learning Summary" Socket programming Basics/Network Programming Basics

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.