头文件
#ifndef UNP_H_
#define UNP_H_
/**
* @file unp.h
* @brief global structure for unp
* @author mhw
* @copyright galaxywind
* @date 2013/11/06
* @note
*
*/
#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
#endif
服务端
#include "unp.h"
void str_echo(int sockfd){
int 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);
sleep(1);
fprintf(stderr, "close client\n");
close(sockfd);
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;
}
客服端
#include "unp.h"
void str_cli(FILE *fp, int conn_fd){
char buf[MAXN];
char *ptr;
again:
while( (ptr = fgets(buf, MAXN, fp)) != NULL){
write(conn_fd,buf, MAXN);
read(conn_fd, buf, MAXN);
fputs(buf, stdout);
}
if(errno == EINTR)
goto again;
if(!ptr){
fprintf(stderr, "read EOF\n");
}
if( EWOULDBLOCK == close(conn_fd)){
fprintf(stderr, "get fin from server timeout\n");
}else{
fprintf(stderr, "success\n");
}
}
int main(int argc, char **argv)
{
int conn_fd;
struct linger ling;
socklen_t size;
struct sockaddr_in server_addr;
if(argc != 2 && argc != 3){
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(argc == 3){
ling.l_onoff = 1;
ling.l_linger = atoi(argv[1]);
size = sizeof(ling);
if( getsockopt(conn_fd, SOL_SOCKET, SO_LINGER, &ling, &size) < 0){
fprintf(stderr, "set linger fail\n");
}else{
fprintf(stderr, "set linger success\n");
}
}
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);
}