Multi-IO, multi-request, single server

  • 两个函数send and recv
1 num = send(s, addr_of_data, len_of_data, 0);
2 num = recv(s, addr_of_buffer, len_of_buffer, 0);
3 close(s);

s: socket s, 是其他client与其通信的接口。

addr_of_data/buffer: the address in storage of the buffer.
len_of_data/buffer: the size of this buffer.

flag: the last parameter. which indicates the how the data is to be sent. 0 tells TCP/IP to transfer the data
normally. The server uses the socket that is return from accept() call.

the above two function returns the amount of data that was sent or received.

close() deallocates the socket.

  • general steps for socket programming
  1. socket creation
    int sockfd = socket(domain, type, protocol)
    sockfd: socket descriptor, this is an interger(like a file handle).
    domain: interger,申明通信传输域。AF_LOCAL defined in the POSIX standard for communication beween processes on the same host. For communication between processes on different hosts connected by IPV4 we use AF_INET and AF_INET6 for processes conencted by IPV6.
    type: communication type. SOCK_STREAM: TCP(reliable, connected oriented), SOCK_DGRAM: UDP(unreliable, connectionless).
    protocol: protocol value for IP, which is 0. this is alwalys the same value in the IP header of a packet.
posted @ 2023-11-20 06:40  七块蛋糕  阅读(4)  评论(0编辑  收藏  举报