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编程哈。