#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<netinet/in.h>
int main()
{
int sockfd;
int connfd;
int status;
struct sockaddr_in server;
struct sockaddr_in client;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(1234);
server.sin_addr.s_addr=INADDR_ANY;
if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))==-1)
{
perror("bind error");
exit(1);
}
if(listen(sockfd,5)==-1)
{
perror("listen error");
exit(1);
}
socklen_t addrlen=sizeof(client);
while(1)
{
connfd=accept(sockfd,(struct sockaddr*)&client,&addrlen);
int pid;
pid=fork();
if(pid<0)
{
perror("error");
exit(1);
}
else if(pid>0)
{
close(connfd);
continue;
}
else
{
int result=0;
while(1)
{
char ent[100];
recv(connfd,&ent,sizeof(ent),0);
printf("%s\n",ent);
//------------------------------------------------//
if(strcmp(ent,"bye")==0)
break;
int i;
int n=0;
int len=strlen(ent);ent[len]='\0';
for(i=0;i<=len;i++)
{
if( ent[i]>='0' && ent[i]<='9' )
{
n=n*10+ent[i]-48;
}
else
{
if(n!=0||ent[i]=='\0')
{
result+=n;
printf("result--->%d\n",result);
}
n=0;
}
}
//-----------------------------------------------//
}
printf("result : %d \n",result);
write(connfd,&result,sizeof(result));
break;
}
}
close(connfd);
//close(sockfd);
}