makefile
src=$(wildcard *.c)
obj=$(patsubst %.c,%.c,$(src))
all:server client
server: server.o wrap.o
gcc server.o wrap.o -o server -Wall
client: client.o wrap.o
gcc client.o wrap.o -o client -Wall
%.c:%.c
gcc -c $< -Wall
.PHONY: clean all
clean:
-rm -rf server client $(obj)
错误处理函数头文件wrap.h
#ifndef __WRAP_H_
#define __WRAP_H_
void perr_exit(const char *s);
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr);
int Bind(int fd,const struct sockaddr *sa,socklen_t salen);
int Connect(int fd,const struct sockaddr *sa,socklen_t salen);
int Listen(int fd,int backlog);
int Socket(int family,int type,int protocol);
ssize_t Read(int fd,void *ptr,size_t nbytes);
int Close(int fd);
ssize_t Readn(int fd,void *vptr,size_t n);
ssize_t Writen(int fd,const void *vptr,size_t n);
ssize_t my_read(int fd,char *ptr);
ssize_t Readline(int fd,void *vptr,size_t maxlen);
#endif
错误处理函数wrap.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
void perr_exit(const char *s)
{
perror(s);
exit(-1);
}
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr)
{
int n;
again:
if((n=accept(fd,sa,salenptr))<0){
if((errno==ECONNABORTED)||(errno==EINTR))
goto again;
else
perr_exit("accept error");
}
return n;
}
int Bind(int fd,const struct sockaddr *sa,socklen_t salen)
{
int n;
if((n=bind(fd,sa,salen))<0)
perr_exit("Bind error");
return n;
}
int Connect(int fd,const struct sockaddr *sa,socklen_t salen)
{
int n;
printf("----------------in wrap before connect() \n");
n=connect(fd,sa,salen);
printf("----------------in wrap connect() return %d\n",n);
if(n<0){
perr_exit("connect error");
}
return 0;
}
int Listen(int fd,int backlog)
{
int n;
if((n=listen(fd,backlog))<0)
perr_exit("listen error");
return n;
}
int Socket(int family,int type,int protocol)
{
int n;
if((n=socket(family,type,protocol))<0)
perr_exit("socket error");
return n;
}
ssize_t Read(int fd,void *ptr,size_t nbytes)
{
ssize_t n;
again:
if((n=read(fd,ptr,nbytes))==-1){
if(errno==EINTR)
goto again;
else
return -1;
}
return n;
}
ssize_t Write(int fd,const void *ptr,size_t nbytes)
{
ssize_t n;
again:
if((n=write(fd,ptr,nbytes))==-1){
if(errno==EINTR)
goto again;
else
return -1;
}
return n;
}
int Close(int fd)
{
int n;
if((n=close(fd))==-1)
perr_exit("close error");
return n;
}
/*参三:应该读取的字节数*/
ssize_t Readn(int fd,void *vptr,size_t n)
{
size_t nleft;//usigned int 剩余未读取的字节数
ssize_t nread;//int 实际读到的字节数
char *ptr;
ptr=vptr;
nleft=n;//n 未读取字节数
while(nleft>0){
if((nread=read(fd,ptr,nleft))<0){
if(errno==EINTR)
nread=0;
else
return -1;
}else if(nread==0)
break;
nleft-=nread;
ptr+=nread;
}
return n-nleft;
}
ssize_t Writen(int fd,const void *vptr,size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr=vptr;
nleft=n;
while(nleft>0){
if((nwritten=write(fd,ptr,nleft))<=0){
if(nwritten<0&&errno==EINTR)
nwritten=0;
else
return -1;
}
nleft-=nwritten;
ptr+=nwritten;
}
return n;
}
static ssize_t my_read(int fd,char *ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buf[100];
if(read_cnt<=0){
again:
if((read_cnt=read(fd,read_buf,sizeof(read_buf)))<0){
if(errno==EINTR)
goto again;
return -1;
}else if(read_cnt==0)
return 0;
read_ptr=read_buf;
}
read_cnt--;
*ptr=*read_ptr++;
return 1;
}
/*readline --- fgets*/
//传出参数 vptr
ssize_t Readline(int fd,void *vptr,size_t maxlen)
{
ssize_t n,rc;
char c,*ptr;
ptr=vptr;
for(n=1;n<maxlen;n++){
if((rc=my_read(fd,&c))==1){
*ptr++=c;
if(c=='\n')
break;
}else if(rc==0){
*ptr=0;
return n-1;
}else
return -1;
}
*ptr=0;
return n;
}
服务端函数server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <strings.h>
#include "wrap.h"
#include <sys/wait.h>
#include "wrap.h"
#define SERV_PORT 8888
void wait_child(int signo)
{
while(waitpid(0,NULL,WNOHANG)>0);
return ;
}
int main(void)
{
pid_t pid;
int lfd,cfd;
struct sockaddr_in serv_addr,clie_addr;
socklen_t clie_addr_len;
char buf[BUFSIZ],clie_IP[BUFSIZ];
int n,i;
lfd=Socket(AF_INET,SOCK_STREAM,0);
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERV_PORT);
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
//inet_pton(AF_INET,"10.48.41.35",&serv_addr.sin_addr.s_addr);
Bind(lfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
Listen(lfd,128);
while(1){
clie_addr_len=sizeof(clie_addr);
cfd=Accept(lfd,(struct sockaddr *)&clie_addr,&clie_addr_len);
printf("client IP:%s,port:%d\n",
inet_ntop(AF_INET,&clie_addr.sin_addr.s_addr,clie_IP,sizeof(clie_IP)),
ntohs(clie_addr.sin_port));
pid=fork();
if(pid<0){
perror("fork error");
exit(1);
}else if(pid==0){
close(lfd);
break;
}else{
close(cfd);
signal(SIGCHLD,wait_child);
}
}
if(pid==0){
while(1){
n=Read(cfd,buf,sizeof(buf));
if(n==0){
close(cfd);
return 0;
}else if(n==-1){
perror("read error");
exit(1);
}else{
for(i=0;i<n;i++)
buf[i]=toupper(buf[i]);
write(cfd,buf,n);
write(STDOUT_FILENO,buf,n);//将客户端的输入输出到屏幕上
}
}
}
return 0;
}
在终端输入
make
./server
再启动n个终端都可以连接到服务器
nc 127.0.0.1 8888