Simple TCP communication model

Posted by westexasman on Mon, 28 Oct 2019 15:14:44 +0100

  in TCP/IP protocol, "IP address + TCP or UDP port number" uniquely identifies a process in network communication, "IP address + port number" is called socket.

int socket(int domain, int type, int protocol); success file descriptor, failure-1

Streaming socket: non frame synchronization
 Datagram socket: frame synchronization

htonl(), htons() host byte order converted to network byte order
 The network byte order of nthl(), nths() is converted to the host byte order

inet_aton() converts a string to a 32-bit network byte order binary value
 inet_addr() same as above, return the changed address
 INET? Ntoa() converts a 32-bit network byte order binary address to a dotted decimal string

inet_pton() converts the address of IPV4/IPV6 to binary
 inet_ntop() converts binary to IPV4/IPV6 address

/usr/include/netinet/in.h
 #define in addr? Any ((in? Addr? T) 0x00000000) format

The client code is as follows:

#include <stdio.h>
#include <stdlib.h>  

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

#define SIZE 100

int main(int argc, char const **argv)
{
	if(argc != 3)
	{
		printf("Usage: %s <IP>:<PORT>\n", argv[0]);
		return -1;
	}
	
	//Establish
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd == -1)
	{
		perror("socket fail\n");
		return -1;
	}
	
	//Prepare the address of the opposite end
	struct sockaddr_in server;
	socklen_t len = sizeof(server);
	memset(&server, 0, len);
	
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr(argv[1]);
	server.sin_port = htons(atoi(argv[2]));
	
	//Connect to server
	int ret = connect(sockfd, (struct sockaddr *)&server, len);
	if(ret == -1)
	{
		perror("connect fail\n");
		return -1;
	}
	
	char buf[SIZE];
	char *find;
	while(1)
	{
		memset(buf, 0, SIZE);
		fgets(buf, SIZE, stdin);
		
		//Remove absorbed line breaks from fgets
		find = strchr(buf, '\n');
		if(find)
			*find = '\0';
		
		send(sockfd, buf, SIZE, 0);	
		if(!strncmp(buf, "exit", 4))
			break;
	}
	
	return 0;
}

The server-side code is as follows

#include <stdio.h>
#include <stdlib.h>  

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

#define SIZE 100

int main(int argc, char const **argv)
{
	if(argc != 2)
	{
		printf("Usage: %s <PORT>\n", argv[0]);
		return -1;
	}

	//Establish
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd == -1)
	{
		perror("socket fail\n");
		return -1;
	}
	
	struct sockaddr_in server,client;
	socklen_t len = sizeof(server);
	memset(&server, 0, len);
	
	server.sin_family = AF_INET;
	server.sin_port = htons(atoi(argv[1]));
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	
	//binding
	int ret = bind(sockfd, (struct sockaddr *)&server, len);
	if(ret == -1)
	{
		perror("bind fail\n");
		return -1;
	}

	//Monitor
	ret = listen(sockfd, 3);
	if(ret == -1)
	{
		perror("listen fail\n");
		return -1;
	}
	
	//Waiting for the connection request of the other party
	len = sizeof(client);
	int clientfd = accept(sockfd, (struct sockaddr *)&client, &len);
	
	printf("new connection: %s:%hu\n",inet_ntoa(client.sin_addr), ntohs(client.sin_port));
	
	char buf[SIZE];
	while(1)
	{
		memset(buf, 0, SIZE);
		recv(clientfd, buf, SIZE, 0);
		printf("recv: %s\n",buf);
		
		if(!strncmp(buf, "exit", 4))
			break;
	}
	
	return 0;
}

Topics: socket network