首页 > 代码库 > UNIX网络编程(第三版 ) 测试代码 7.2
UNIX网络编程(第三版 ) 测试代码 7.2
#ifndef UNP_H_ #define UNP_H_ /** * @file unp.h * @brief global structure for unp * @author xiyanxiyan10 * @TODO 处理覆盖不全缺乏安全性,测试使用 * */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <arpa/inet.h> #include <signal.h> #include <errno.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <pthread.h> #include <errno.h> #define SERVER_PORT 8899 #define MAX_LISTEN 10 #define MAXN 1024 typedef unsigned int size_t; typedef signed int ssize_t; #endif
server
#include "unp.h" void str_echo(int sockfd){ ssize_t n; char buf[MAXN]; again: while( (n = read(sockfd, buf, MAXN) )> 0) write(sockfd, buf, MAXN); if(n < 0 && errno == EINTR) goto again; if(n < 0) strerror(errno); if(n == 0) return; } int main(int argc, char **argv) { int listen_fd; int conn_fd; pid_t child_fd; socklen_t client_len; struct sockaddr_in server_addr, client_addr; char client_str[INET_ADDRSTRLEN]; memset( (void *)&server_addr, 0, sizeof(struct sockaddr_in)); memset( (void *)&client_addr, 0, sizeof(struct sockaddr_in)); memset( (void *)client_str, 0, sizeof(client_str)); listen_fd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htons(INADDR_ANY); server_addr.sin_port = htons(SERVER_PORT); bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); listen(listen_fd, MAX_LISTEN); while(1){ conn_fd = accept( listen_fd, (struct sockaddr*)&client_addr, &client_len); child_fd = fork(); if(child_fd == 0){ close(listen_fd); str_echo(conn_fd); exit(0); } inet_ntop(AF_INET, (void *)&client_addr, client_str, INET_ADDRSTRLEN); fprintf(stderr, "conn_fd from %s\n", client_str); close(conn_fd); memset( (void *)&client_addr, 0, sizeof(struct sockaddr_in)); memset( (void *)client_str, 0, sizeof(client_str)); } return 0; }
client
#include "unp.h" void print_mss(int connfd){ int mss; socklen_t size = sizeof(mss); if( getsockopt(connfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &size) < 0){ fprintf(stderr, "get mss fail\n"); }else{ fprintf(stderr, "get mss success %u\n", mss); } } void str_cli(FILE *fp, int conn_fd){ char buf[MAXN]; while( fgets(buf, MAXN, fp) != NULL){ write(conn_fd,buf, MAXN); read(conn_fd, buf, MAXN); fputs(buf, stdout); print_mss(conn_fd); } } int main(int argc, char **argv) { int conn_fd; struct sockaddr_in server_addr; if(argc != 2){ fprintf(stderr, "server address needed\n"); return -1; } memset( (void *)&server_addr, 0, sizeof(struct sockaddr_in)); conn_fd = socket(AF_INET, SOCK_STREAM, 0); inet_pton(AF_INET, argv[1], (void*)&server_addr); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); if ( connect(conn_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0){ fprintf(stderr, "connect fail to server %s\n", argv[1]); exit(0); } str_cli(stdin, conn_fd); exit(0); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。