Client:
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char * argv[])
{
struct sockaddr_in sockaddr;
int sockfd[100],connectfd;
struct linger mling;
int i,ii,retval,len,pid;
char *str;
if(argc<3)
{
printf("please enter three value!!\n");
exit(-1);
}
/*
signal(SIGINT,SIG_IGN);
signal(SIGHUP,SIG_IGN);
signal(SIGALRM,SIG_IGN);
signal(SIGCHLD,SIG_IGN);
*/
str=(char *)malloc(1024);
sockaddr.sin_family=AF_INET;
/*
sockaddr.sin_port=htonl(atoi(argv[1]));
*/
sockaddr.sin_port=htons(99999);
sockaddr.sin_addr.s_addr=inet_addr(argv[2]);
len=sizeof(sockaddr);
printf("*****%d*****\n",atoi(argv[1]));
printf("IP:%s\tlength=%d\n",inet_ntoa(sockaddr.sin_addr.s_addr),strlen(inet_ntoa(sockaddr.sin_addr.s_addr)));
mling.l_onoff=1;
mling.l_linger=5;
for(ii=0;ii<=100;ii++)
{
pid=fork();
if(pid<0)
{
perror("run fork is error!\n");
exit(0);
}
if(pid>0)
{
sleep(1);
continue;
}
if(pid==0)
{
sockfd[ii]=socket(AF_INET,SOCK_STREAM,0);
printf("this is socket descriptor:%d\n",sockfd[ii]);
if(sockfd[ii]<=0)
{
perror("run socket is error!\n");
close(sockfd[ii]);
exit(-1);
}
retval=setsockopt(sockfd[ii],SOL_SOCKET,SO_LINGER,&mling,sizeof(struct linger));
if(retval<0)
{
printf("run setsockopt set so_linger is error!\n");
close(sockfd[ii]);
exit(-1);
}
connectfd=connect(sockfd[ii],(struct sockaddr *)&sockaddr,len);
printf("have run connect!!%d\n",connectfd);
if(connectfd<0)
{
perror("run connect is error!\n");
close(sockfd[ii]);
sleep(1);
break;
}
for(i=0;i<100;i++)
{
sprintf(str,"%d%d%d%d",i,i,i,i);
retval=send(sockfd[ii],str,strlen(str),0);
printf("retval=%d\n",retval);
if(retval!=strlen(str))
{
perror("have not sent sucessful!!\n");
}
memset(str,'\0',1024);
retval=read(sockfd[ii],str,1024);
if(retval<0)
{
perror("have not recive sucessful!!\n");
}
if(retval==0)
{
break;
}
printf("this is recive from server=%s\n",str);
}
close(sockfd[ii]);
sleep(1);
}
}
}
Server:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SOCKNUM 5
void texit();
int main(int argc,char * argv[])
{
struct sockaddr_in sockaddr;
int sockfd,clientfd;
struct sockaddr_in clientaddr;
struct linger mling;
int i,retval,len,pid,len1,PORT;
char *str,*IP,*tmpstr;
fd_set readfds,writefds;
if(argc<2)
{
printf("please enter two value!!\n");
exit(-1);
}
signal(SIGINT,texit);
signal(SIGHUP,SIG_IGN);
signal(SIGALRM,SIG_IGN);
signal(SIGCHLD,SIG_IGN);
str=(char *)malloc(1024);
tmpstr=(char *)malloc(1024);
IP=(char *)malloc(20);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
perror("run socket is error!\n");
close(sockfd);
exit(-1);
}
sockaddr.sin_family=AF_INET;
sockaddr.sin_port=htons(strtol(argv[1],NULL,0));
sockaddr.sin_addr.s_addr=htonl(INADDR_ANY);
len=sizeof(sockaddr);
retval=bind(sockfd,(struct sockaddr *)&sockaddr,len);
if(retval<0)
{
printf("bind sockfd is error!\n");
close(sockfd);
exit(-1);
}
listen(sockfd,SOCKNUM);
printf("run is here!!\n");
printf("%d\n",strtol(argv[1],NULL,0));
len1=sizeof(clientaddr);
while(1)
{
clientfd=accept(sockfd,(struct sockaddr *)&clientaddr,&len1);
if(clientfd<0&errno==ECONNABORTED)
{
perror("run accept is error!\n");
close(clientfd);
continue;
}
pid=fork();
if(pid==0)
{
close(sockfd);
printf("in child process!!\n");
IP=inet_ntoa(clientaddr.sin_addr);
PORT=ntohs(clientaddr.sin_port);
printf("aaaaaa\n");
printf("this is client IP:%s\n",IP);
printf("this is client PORT:%d\n",PORT);
sleep(2);
//for(i=0;i<100;i++)
{
retval=read(clientfd,str,1024);
printf("test after read\n");
if(retval<0)
{
perror("have not recive sucessful!!\n");
}
printf("this is recive from client=%s\n",str);
memset(tmpstr,'\0',1024);
sprintf(tmpstr,"0000|FILENAME");
retval=send(clientfd,tmpstr,strlen(str),0);
if(retval!=strlen(str))
{
perror("have not sent sucessful!!\n");
}
if(i==50)
{
shutdown(clientfd,1);
break;
}
}
exit(0);
}
if(pid<0)
{
perror("run fork() is error!!\n");
close(clientfd);
close(sockfd);
break;
}
close(clientfd);
}
close(sockfd);
free(str);
free(IP);
}
void texit()
{
printf("this is program have been interrupt!\n");
exit(0);
}
Makefile:
CC=cc
CFLAGS=-O
SRC=tserver.c tclient.c
HEADER= $(HOME)/include
LIB= -lsocket -lnsl
.SUFFIXES:
.SUFFIXES:.c .o
ALL=tserver tclient
all:$(ALL)
tserver:tserver.o
$(CC) $(CFLAGS) -o $@ $? $(LIB)
tclient:tclient.o
$(CC) $(CFLAGS) -o $@ $? $(LIB)
.c.o:
$(CC) $(CFLAGS) -c $<
clean:
rm -f *.o
rm -f tserver
rm -f tclient