主要思路是关闭服务器端的标准输出,同时使用dup复制一个客户端的连接文件描述符占用值为1的文件描述符,printf就会直接输出到客户端。
书中的代码我感觉少关闭了dup复制的文件描述符,所以代码中补充了一行close操作。
服务器代码:
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
int main(int argc, char* argv[]){
if(argc <= 2){
printf("ip and port \n");
return 0;
}
char* ip = argv[1];
int port = atoi(argv[2]);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
int ret = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
assert(ret != -1);
ret = listen(sockfd, 5);
assert(ret != -1);
struct sockaddr_in clientaddr;
socklen_t clientaddr_len;
int connfd = accept(sockfd, (struct sockaddr*)&clientaddr, &clientaddr_len);
if(connfd < 0){
printf("errno is : %s \n", errno);
}else{
close(STDOUT_FILENO);
int dconnfd = dup(connfd);
printf("hello world \n");
close(connfd);
close(dconnfd);
}
close(sockfd);
return 0;
}
客户端代码:
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define BUFFER_SIZE 1024
int main(int argc, char* argv[]){
if(argc <= 2){
printf("ip and port and size!!!\n");
return 0;
}
char* ip = argv[1];
int port = atoi(argv[2]);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(port);
inet_pton(AF_INET, ip, &serveraddr.sin_addr);
if((connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr))) < 0){
printf("connect failure \n");
}else{
char buf[BUFFER_SIZE];
memset(buf, '\0', BUFFER_SIZE);
int ret = recv(sockfd, buf, sizeof(buf), 0);
printf("recv %d word: %s\n", ret, buf);
}
close(sockfd);
return 0;
}
运行效果截图:
服务器
客户端