TCP Network Programming process
Sockets
STRUCTSOCKADDR{//Universal Socket Data structure
sa_family_tsa_family;//Protocol Family
charsa_data[14];//Protocol Family Data
}
Actual use of sockaddr_in, forced conversion at bind or connect
structsockaddr_in{//Ethernet Socket Data structure
U8sin_len;
u8sin_familly;
U16sin_port;
STRUCTIN_ADDRSIN_ADDR;
CHARSIN_ZERO[8];
}
Server-side: Socket ()->bind ()->listen ()->accept ()->read ()->write ()->close ()
Client: Socket ()->connect ()->write ()->read ()->close ()
Socket () Create socket, return file descriptor
Prototype: intsocket (int domain,int type,int protocol);
Domain setting communication, specifying the protocol family
Pf_unix,pf_local |
Local communication |
Pf_inet |
Ipv4internet protocol |
Pf_inet6 |
Ipv6internet protocol |
Pf_ipx |
|
Pf_netlink |
Kernel user Interface Devices |
pf_x25 |
|
pf_ax25 |
|
Pf_atmpvc |
|
Pf_appletalk |
|
Pf_packet |
Underlying package access |
The value and meaning of type
Sock_stream |
TCP connections |
Sock_dgram |
Udp |
Sock_seqpacket |
Serialization Package |
Sock_raw |
Provides access to the original network protocol |
Sock_rdm |
Provides a reliable type of data message, but may have a disorderly sequence of data |
Sock_packet |
Dedicated type, receiving data from device drivers |
protocol specifies the specific type of a protocol,
Bind () binds an address port
Prototype: intbind (int sockfd,const struct sockaddr *my_addr,socklen_t addrlen);
Listen () Listening on the local port
Prototype: intlisten (int sockfd,int backlog);
The backlog represents the length of the client waiting in the queue before the accept process.
Accept ()
Prototype: intaccept (int sockfd,struct sockaddr *addr,socklen_t *addrlen);
Addr is a pointer that returns a pointer to the address of the client.
Connect ()
Prototype: intconnect (int sockfd,struct sockaddr *,int addrlen);
Write ()
Read ()
Close ()
Shutdown ()
Prototype: intshutdown (int s,int how)
Allow more ways to close sockets
Shut_rd |
Cut Read |
Shut_wr |
Cut Write |
Shut_rdwr |
Cut read and write, same as close () |
Signal:
When trying to write, if the server is closed, get a sigpipe signal and terminate the process
This article is from the "No Front" blog, please be sure to keep this source http://qianyang.blog.51cto.com/7130735/1615335
TCP Network Programming process