socket本地进程间通信(TCP和UDP)学习笔记

TCP

参考《Linux程序设计》

client主要用到socket和connect,server主要用到socket,bind,listen和accept

Client端:

#include
#include
#include
#include
#include
#include

#define UNIX_DOMAIN "./unix_tmp"

int main()
{
	int fd, ret, len;
	struct sockaddr_un un;
	char ch = 'A';

	//create socket
	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if(fd<0)
	{
		printf("socket error\n");
		return -1;
	}

	//name the socket
	un.sun_family=AF_UNIX;
	strcpy(un.sun_path, UNIX_DOMAIN);
	len = sizeof(un);

	//connect
	ret = connect(fd, (struct sockaddr*)&un, len);
	if(ret < 0)
	{
		printf("connect error\n");
		close(fd);
		return -1;
	}

	//write and read by socket fd
	write(fd, &ch, 1);
	read(fd, &ch, 1);
	printf("char from server = %c\n", ch);
	close(fd);

	return 0;
}

Server端:

#include
#include
#include
#include
#include
#include

#define UNIX_DOMAIN "./unix_tmp"

int main()
{
	int server_sockfd, client_sockfd;
	int server_len, client_len;
	struct sockaddr_un server_address;
	struct sockaddr_un client_address;

	//create server socket
	unlink(UNIX_DOMAIN);
	server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if(server_sockfd<0)
	{
		printf("socket error\n");
		return -1;
	}

	//name the socket
	server_address.sun_family = AF_UNIX;
	strcpy(server_address.sun_path, UNIX_DOMAIN);
	server_len = sizeof(server_address);
	bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

	//create a connection queue
	listen(server_sockfd, 5);
	while(1){
		char ch;
		printf("server waiting:\n");
	
	//accept a connection
		client_len = sizeof(client_address);
		client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len);
	//write and read by socket fd
		read(client_sockfd, &ch, 1);
		printf("char from client is %c\n", ch);
		ch++;
		write(client_sockfd, &ch, 1);
		close(client_sockfd);
	}
	close(server_sockfd);
	unlink(UNIX_DOMAIN);

	return 0;
}

gcc server.c -o server

gcc client.c -o client

UDP

udp_client主要用到socket和sendto,udp_server主要用到socket,bind和recvfrom,因为UDP不是面向连接的,所以和TCP有一点不同。

udp_client:

#include
#include
#include
#include
#include
#include
#include 

/*****************Client****************/

#define UNIX_DOMAIN "./udp_tmp"

int main()
{
	int fd, ret, len;
	struct sockaddr_un un;
	char str[24] ;
	memset(str, 0, sizeof(str));
	strcpy(str, "Hello World!");

	//create socket
	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if(fd<0)
	{
		printf("socket error\n");
		return -1;
	}

	//name the socket
	un.sun_family=AF_UNIX;
	strcpy(un.sun_path, UNIX_DOMAIN);
	len = sizeof(un);

	ret = sendto(fd, str, strlen(str)+1, 0, (struct sockaddr*)&un, sizeof(struct sockaddr_un));
	printf("ret = %d, errno = %d, %s\n", ret, errno, strerror(errno));

	return 0;
}

udp_server: 

#include
#include
#include
#include
#include
#include

/*****************Server****************/

#define UNIX_DOMAIN "./udp_tmp"

int main()
{
	int ret;
	int server_sockfd;
	int server_len;
	struct sockaddr_un server_address;

	//create server socket
	unlink(UNIX_DOMAIN);
	server_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if(server_sockfd<0)
	{
		printf("socket error\n");
		return -1;
	}

	//name the socket
	server_address.sun_family = AF_UNIX;
	strcpy(server_address.sun_path, UNIX_DOMAIN);
	server_len = sizeof(server_address);
	bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

	while(1){
		char str[1024]="";
		printf("server waiting:\n");
	
	//accept a connection
		ret = recvfrom(server_sockfd, str, 1024, 0, (struct sockaddr *)&server_address, (socklen_t *)&server_len);
	//write and read by socket fd
		str[ret]= '\0';
		printf("char from client is %s\n", str);
	}
	close(server_sockfd);
	unlink(UNIX_DOMAIN);

	return 0;
}

gcc udp_client.c -o udp_client

gcc udp_server.c -o udp_server

你可能感兴趣的:(socket本地进程间通信(TCP和UDP)学习笔记)