Network programming for communication between processes
TCP/UDP comparison
1. TCP connection-oriented ( such as dialing to establish a connection first ) UDP is connectionless , that is , there is no need to establish a connection before sending data
2. TCP provides reliable services. In other words , through TCP data connection transfer , error-free , do not lose , do not repeat , and arrive in order UDP best effort delivery , that is, does not guarantee reliable delivery
3. TCP is byte-oriented . In fact, TCP treats data as a series of unstructured byte streams. UDP is message-oriented UDP without congestion control . Therefore, network congestion will not reduce the sending rate of the source host ( for real-time Applications are very useful , such as IP telephony , real-time video conferencing, etc.
4. Each TCP connection can only be point-to-point UDP, supporting one-to-one , one-to-many , many-to-one and many-to-many interactive communications
5. TCP header overhead is 20 bytes ; UDP header overhead is small , only 8 words
6. The logical communication channel of TCP is a full-duplex reliable channel, while UDP is an unreliable channel
Port number function
A host with an IP address can provide many services , such as Web services, FTP services, SMTP services, etc.
These services can by 1 Ge P to achieve address. So , how does a host distinguish between different network services ? Obviously, you cannot rely on IP addresses alone , because the relationship between IP addresses and network services is one.
In fact , different services are distinguished by "IP address + port number " . The port provides an access channel ,
Servers are generally identified by well-known port numbers. For example , for each TCPIP achieve it , FTP server's TCP port number is 21, each Telnet server's TCP port number is 23, each TFTP ( Trivial File Transfer Protocol ) server UDP port number is 69
The scene of network programming is as shown in the figure:
Chart 1
Briefly explain the content of the picture:
- Specify to speak "Chinese" ( TCP/UDP connection protocol)
The code prototype is: int socket(int domain,int type,int peotocol);
//int domain: Specify the protocol family used, usually AF_INET, which means the Internet protocol family ( TCP/IP protocol family)
of the type int //: of the type reference to the specified socket type
//int peotocol: usually assigned the value " 0 "
- The IP address and port number in the picture are the addresses we need in the next step
We need to call our function
Bind() function: IP number port number and corresponding description word assignment function
The code prototypes are:
#include<sys/types.h>
#inlcude<sys/socket.h>
Int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
Features:
- Used to bind IP address and port number to socketfd
parameter:
2 sockfd
Is a socket descriptor
3 addr
It is a pointer to the sockaddr type that contains information such as the local IP address and port number, and points to the protocol address structure to be bound to sockfd . This address structure varies according to the address protocol family that the socket is created.
(3) Address translation API
Code prototype
Int inet_aton(const char* straddr.struct int_addr *addrp);
Convert the string form " 192.168.1.123 " into a format that the network can recognize
Char * inet_ntoa (struct in_addr inaddr);
Convert the ip address in network format into string form
( 4 ) Monitoring
Listen (); function: monitor setting function
The code prototypes are:
#include<sys/types.h>
#include<sys/socket.h>
Int listen(int sockfd,int backlog);
parameter:
- Sockfd:
sockfd is the server-side socket descriptor returned by the socket system call
2 backlog:
backlog specifies the maximum number of requests allowed in the request queue
( 5 ) Connection
The code prototypes are:
#include<sys/types.h>
#include<sys/socket.h>
Int accept(int sockfd,struct sockaddr *addr, socklen_t *addrlen);
Features:
- The accept function is called by the TCP server to return the next completed connection from the opposite end of the completed connection queue. If the completed queue is empty, then the process is put to sleep.
parameter:
2 sockfd
sockfd is the server-side socket descriptor returned by the socket system call
3 addr
Used to return the protocol address of the connected peer (client)
4 addrled
Length of client address
( 6 ) Sending and receiving of data
Byte stream reading function
Socket continuity byte read function: Read ();, Write (); , and the I / O reads the function is slightly different, because the number of bytes of their input or output than is possible than requested Less,
The code prototypes are:
Ssize_t write(int fd, const void *buf, size_t nbytes);
Ssize_t read(int fd, void *buf, size_t nbyte);
Of course, there are also some functions of Net I/O , such as: recv()/send(), ready()/writev(), recvmsg()/sendmsg(), recvfrom()/sendto(), etc.
( 7 ) The client connects to the host
Client connect function
Connect():
The code prototypes are:
#include<sys/types.h>
#include<sys/socket.h>
Int connect(int sockfd, const strct sockaddr *addr, socklen_t addrlen);
Features:
- This function is used for the client (client) after binding to establish a connection with the server
parameter:
- sockfd
Is the sockect descriptor of the destination server
2 addr
It is the address structure pointer of the IP address and port number of the server
3 addrlen
The address length is set to sizeof(struct sockaddr)
(4) Return value
Return 0 on success , return -1 when encountering an error , and errno contains the corresponding error code
sockt server and client development steps
- socket(): create a socket
- Bind() : Add information to the socket ( IP address and port number)
- Listen() : monitor network connections
- Accept() : After listening to a client, accept a connection
- Read(),write(),read() : data interaction
- Close() : Close Tao Jie character, disconnect
Code example:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int s_fd;
int n_read;
char readBuf[128];
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in)); //对数据的清空
memset(&c_addr,0,sizeof(struct sockaddr_in));
char *msg = "I get your connect";
// 1 socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET; //配置协议
s_addr.sin_port = htons(8888); //设置端口号“注意字节序”
inet_aton("192.168.1.88",&s_addr.sin_addr); //把字符串形式的“192.168.1.88”转化为网
络能识别的格式
// 2 bind
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in)); //绑定,把该地
址与s_fd 绑定在一起
// 3 listen
listen(s_fd,10); //监听
// 4 accept
int clen = sizeof(struct sockaddr_in);
int c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen); //接收客户端的信息
if(c_fd == -1){
perror("accept");
}
printf("get message: %s \n",inet_ntoa(c_addr.sin_addr)); //要把网络格式的IP地址
转化为字符串形式
// 5 read
n_read = read(c_fd,readBuf,128); //从c_fd把内容读到readBuf里面来
if(n_read != -1){
perror("read");
}else{
printf("get message: %d ,%s\n",n_read,readBuf);
}
// 6 writed
write(c_fd,msg,strlen(msg)); //信息回复
printf("connect\n");
return 0;
}