Linux网络编程实现客户端服务端收发数据功能

客户端代码

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

int main(void)
{
	int st = socket(AF_INET, SOCK_STREAM, 0);//初始化socket

	struct sockaddr_in addr; // 定义一个IP地址的结构
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;// 设置结构地址类型为TCP/IP地址
	addr.sin_port = htons(8080); // 制定一个端口号:8080,htons:将short类型从host字节类型转到net字节类型
	// 将字符类型的IP地址转化为int,赋给addr结构
	//addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	addr.sin_addr.s_addr = inet_addr("192.168.10.210");

	//
	if (connect(st, (struct sockaddr *)&addr, sizeof(addr)) == -1)
	{
		printf("connect failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	char s[1024];
	memset(s, 0, sizeof(1024));
	strcpy(s, "hello world");

	if (send(st, s, strlen(s), 0) == -1)
	{
		printf("send failed %s\n", strerror(errno));
	}
	//sleep(100);
	close(st);

	return EXIT_SUCCESS;
}


服务端代码

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

int main(int arg, char *args[])
{
	int st = socket(AF_INET, SOCK_STREAM, 0); //初始化socket

	int on = 1;
	if (setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
	{
		printf("setsockopt failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	struct sockaddr_in addr; // 定义一个IP地址结构
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET; // 将addr结构的属性定位为TCP/IP地址
	addr.sin_port = htons(8080); // 将本地字节顺序转化为网络字节顺序
	addr.sin_addr.s_addr = htonl(INADDR_ANY); // INADDR_ANY代表这个server上所有的地址

	// 将ip与server程序绑定
	if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
	{
		printf("bind failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	// server端开始listen
	if (listen(st, 20) == -1)
	{
		printf("listen failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}
	//printf("listen success\n");
	char s[1024];
	int client_st = 0;
	struct sockaddr_in client_addr; // 表示client端的IP地址
	int i;
	for (i = 0; i < 5; i++)
	{
		memset(&client_addr, 0, sizeof(client_addr));
		socklen_t len = sizeof(client_addr);
		// accept会阻塞,直到有客户端连接过来,accept返回client的socket描述符
		client_st = accept(st, (struct sockaddr*) &client_addr, &len);
		if (client_st == -1)
		{
			printf("accept failed %s\n", strerror(errno));
			return EXIT_FAILURE;
		}

		/* 第一个版本
		memset(s, 0, sizeof(1024));
		if (recv(client_st, s, sizeof(s), 0) == -1)
		{
			printf("recv failed %s\n", strerror(errno));
			close(client_st);
			return EXIT_FAILURE;
		}
		printf("revc is %s\n", s);
		close(client_st);*/

		// 第二个版

		/*while (1)
		{
			memset(s, 0, sizeof(1024));
			if (recv(client_st, s, sizeof(s), 0) > 0)
			{
				printf("revc is %s\n", s);
			} else
			{
				printf("recv failed %s\n", strerror(errno));
				break;
			}
		}
		close(client_st);*/

		// 第三个版本
		while (1)
		{
			memset(s, 0, sizeof(1024));
			int rc = recv(client_st, s, sizeof(s), 0);// 是阻塞调用
			if (rc > 0)
			{
				printf("revc is %s\n", s);
			} else
			{
				if (rc == 0)
				{
					printf("client socket closed\n");
				}
				else
				{
					printf("recv failed %s\n", strerror(errno));
				}
				break;
			}
		}
		close(client_st);


	}
	close(st);
	return 0;
}

makefile文件内容

.SUFFIXES:.c .o

CC=gcc
SRCS1=sock.c
SRCS2=server.c

OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)
EXEC1=client
EXEC2=server

all: $(OBJS1) $(OBJS2)
	 $(CC) -o $(EXEC1) $(OBJS1)
	 $(CC) -o $(EXEC2) $(OBJS2)
	 @echo '----------------ok------------'

.c.o:
	$(CC) -Wall -g -o $@ -c $<

clean:
	rm -f $(OBJS1)
	rm -f core*



你可能感兴趣的:(Linux编程学习)