(unix网络编程)tcp回射程序一:基本实现

unix网络编程,本节来一例最基本的tcp回射程序,有一个唯一容易出错的地方就是回射程序的buffer会多次使用,所以在每次使用完毕或者使用前都记得用memset清零,不然所得到的字符串会有异常,现在贴上具体的代码。


客户端代码如下:

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

#define MAXLINE 4096
#define SERV_PORT 5000
#define LISTENQ 5

void str_echo(int sockfd) {
	ssize_t n;
	char buf[MAXLINE];

	for(;;) {
		while ((n = read(sockfd, buf, MAXLINE)) > 0) {
			if (write(sockfd, buf, n) < 0) {
				printf("write error:%s\n", strerror(errno));
			}
			memset(buf, 0x00, MAXLINE);
		}

		if (n < 0 && errno != EINTR) {
			printf("str_echo: read error:%s\n", strerror(errno));
		}
	}
}

int main(int argc, char **argv)
{
	int listenfd, connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;

	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("socket error:%s\n", strerror(errno));
		return -1;
	}

	memset(&servaddr, 0x00, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);

	if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
		printf("bind error:%s\n", strerror(errno));
		return -1;
	}

	if (listen(listenfd, LISTENQ) < 0) {
		printf("listen error:%s\n", strerror(errno));
		return -1;
	}

	for(;;) {
		clilen = sizeof(cliaddr);
		if ((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
			printf("listen error:%s\n", strerror(errno));
			return -1;
		}

		if ((childpid = fork()) == 0) {
			if (close(listenfd) < 0) {
				printf("close listenfd error:%s\n", strerror(errno));
			}
			str_echo(connfd);
			exit(0);
		}

		if (close(connfd) < 0) {
			printf("close connfd error:%s\n", strerror(errno));
		}
	}

}

服务器端代码如下:

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

#define SERV_PORT 5000
#define MAXLINE 4096

void str_cli(FILE *fp, int sockfd) {
	char sendline[MAXLINE], recvline[MAXLINE];

	while (fgets(sendline, MAXLINE, fp) != NULL) {
		if (write(sockfd, sendline, strlen(sendline)) < 0) {
			printf("write error:%s\n", strerror(errno));
		}

		if (read(sockfd, recvline, MAXLINE) == 0) {
			printf("server terminated:%s\n", strerror(errno));
		}
		
		if (fputs(recvline, stdout) == EOF) {
			printf("fputs over\n");
		}
		memset(sendline, 0x00, MAXLINE);
		memset(recvline, 0x00, MAXLINE);
	}
}

int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;

	if (argc != 2) {
		printf("tcpcli1 <IPaddres>\n");
		return -1;
	}

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("socket error:%s\n", strerror(errno));
		return -1;
	}

	memset(&servaddr, 0x00, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) < 0) {
		printf("inet_pton error:%s\n", strerror(errno));
		return -1;
	}

	if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
		printf("connect error:%s\n", strerror(errno));
		return -1;
	}

	str_cli(stdin, sockfd);
	exit(0);
}

回射功能基本实现了,具体的结果跟echo很像,但是这个例子可是用到了unix socket编程哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值