TCP/IP network programming (2) Socket protocol and its data transmission characteristics

Socket protocol and its data transfer characteristics

About the agreement

If two people who are far apart have a call, they must first decide on the call method. If one party chooses the phone, the other party must also choose the phone, otherwise the message will not be received.

In short, a protocol is an agreement made to complete data exchange.

create socket

#include <sys/socket.h>
int socket(int domain, int type, int protocol);

	- 成功返回文件描述符,失败返回-1
        domain		套接字中使用的协议族信息
        type		套接字数据传输类型信息
        protocol	计算机间通信中使用的协议信息

protocol family

The protocol classification information used in the socket is passed through the first parameter of the socket function, and this protocol classification information becomes the protocol family.

name protocol family
PF_INET IPv4 Internet Protocol Suite
PF_INET6 IPv6 Internet Protocol Suite
PF_LOCAL UNIX protocol suite for local communications
PF_PACKET The underlying socket protocol family
PF_IPX IPX Novell protocol suite

Socket type 1: Connection-oriented socket (SOCK_STREAM)

The characteristics of the transmission method are as follows:

  • Data will not disappear during transfer
  • Transfer data sequentially
  • There are no data boundaries in the transferred data

There is a buffer inside the socket that sends and receives data, which is a byte array. The data transmitted through the socket will be saved to this array, so receiving the data does not mean calling the read function immediately. As long as the array capacity is not exceeded, it is possible to read all of it with one read function call after the data fills the buffer. It is also possible Divide into multiple read function calls to read. In a connection-oriented socket, the number of calls to the read and write functions does not mean much, so there is no data boundary in a connection-oriented socket.

A summary of connection-oriented sockets is as follows: reliable, in-order delivery, byte-based connection-oriented data transmission sockets.

Socket type 2: Message-oriented socket (SOCK_DGRAM)

The characteristics of the transmission method are as follows:

  • Emphasis on fast transmission rather than transmission order
  • The transmitted data may be lost or damaged.
  • Transmitted data has data boundaries
  • Limit data size per transfer

A summary of message-oriented sockets is as follows: sockets that are unreliable, delivered out of order, and designed for high-speed transmission of data.

Final choice of protocol

The protocol family information and socket data transmission method have been passed through the strong two parameters of the socket function. This information is not enough to determine the protocol to be used, and a third parameter needs to be passed.

Passing the first two parameters creates the required socket, so in most cases you can pass 0 to the third parameter, unless you encounter this situation:

"There are multiple protocols with the same data transmission method in the same protocol family"

  • If it is a connection-oriented socket of the IPv4 protocol family, the third parameter uses IPPROTO_TCP

  • If it is a message-oriented socket of the IPv4 protocol family, use IPPROTO_UDP as the third parameter.

Connection-oriented sockets: TCP socket example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

void error_handling(char *message);

int main(int argc, char* argv[])
{
	int sock;
	struct sockaddr_in serv_addr;
	char message[30];
	int str_len=0;
	int idx=0, read_len=0;
	
	if(argc!=3){
		printf("Usage : %s <IP> <port>\n", argv[0]);
		exit(1);
	}
	
	sock=socket(PF_INET, SOCK_STREAM, 0);
	if(sock == -1)
		error_handling("socket() error");
	
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
	serv_addr.sin_port=htons(atoi(argv[2]));
		
	if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1) 
		error_handling("connect() error!");

	while(read_len=read(sock, &message[idx++], 1))
	{
		if(read_len==-1)
			error_handling("read() error!");
		
		str_len+=read_len;
	}

	printf("Message from server: %s \n", message);
	printf("Function read call count: %d \n", str_len);
	close(sock);
	return 0;
}

void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

Summarize

This is the second article in the "TCP/IP Network Programming" column. Readers are welcome to subscribe!

For more information, click GitHub . Welcome readers to Star

⭐Academic exchange group Q 754410389 is being updated~~~

Guess you like

Origin blog.csdn.net/m0_63743577/article/details/132587515