Client code:
-sharpinclude <arpa/inet.h>
-sharpinclude <unistd.h>
-sharpinclude <stdio.h>
-sharpinclude <sys/types.h>
-sharpinclude <sys/stat.h>
-sharpinclude <string.h>
-sharpinclude <fcntl.h>
-sharpinclude <stdlib.h>
-sharpinclude <sys/socket.h>
-sharpdefine SA struct sockaddr
int main(int argc,char**argv)
{
int sockfd;
char fname[25];
int len;
struct sockaddr_in servaddr,cliaddr;
struct stat stbuf;
//Connect
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port=htons(10086);
connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
char buffer[BUFSIZ];
FILE *f;
//File Path
char cwd[1024];
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/2");
strcat(cwd,argv[1]);
write(sockfd,cwd,1024);
// send md5
char command[50],ret[100],*token,*md5;
FILE *fp;
sprintf(command,"md5sum %s",argv[1]);
fp = popen(command,"r");
fgets(ret,sizeof(ret)-1,fp);
token = strtok(ret," ");
md5 = token;
send(sockfd,md5,100,0);
//File
int fd = open(argv[1],O_RDONLY);
if(fd < 0){
perror("open()");
}
fstat(fd,&stbuf);
sendfile(sockfd,fd,0,stbuf.st_size);
fclose(fp);
close(sockfd);
printf("the file was sent successfully\n");
}
EpollServer Code:
-sharpinclude <stdio.h>
-sharpinclude <sys/types.h>
-sharpinclude <string.h>
-sharpinclude <stdlib.h>
-sharpinclude <sys/socket.h>
-sharpinclude <arpa/inet.h>
-sharpinclude <unistd.h>
-sharpinclude <sys/wait.h>
-sharpinclude <sys/stat.h>
-sharpinclude <sys/msg.h>
-sharpinclude <sys/ipc.h>
-sharpinclude <errno.h>
-sharpinclude <sys/epoll.h>
-sharpdefine SA struct sockaddr
-sharpdefine LISTENQ 5
-sharpdefine FILEPATH "/etc/passwd"
-sharpdefine PROJID 123456
-sharpdefine MAXFDS 64
struct msgbuf {
long mtype;
char mtext[BUFSIZ];
};
void sighandler(int signum);
int main(int argc,char**argv)
{
int fd,listenfd,epoll_count;
struct sockaddr_in servaddr;
pid_t pid;
struct epoll_event event;
struct epoll_event *events;
//set signal trigger
signal(SIGTERM,sighandler);
//init socket
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd == -1){
perror("socket()");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
//init epoll
int epoll_fd = epoll_create1(0);
if(epoll_fd == -1){
perror("epoll_create1()");
exit(1);
}
event.events = EPOLLIN | EPOLLET;
event.data.fd = listenfd;
if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,listenfd,&event) == -1){
perror("epoll_ctl()");
exit(1);
}
events = calloc(MAXFDS, sizeof(event));
//set listen addr and port
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(10086);
//bind
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
printf("start listen\n");
//listen
listen(listenfd,LISTENQ);
//init message queue
int msgid;
key_t key;
struct msqid_ds msg_buf;
struct msgbuf send_buf;
char tmp[BUFSIZ];
key = ftok(FILEPATH, PROJID);
if (key == -1) {
perror("ftok()");
exit(1);
}
msgid = msgget(key, IPC_CREAT|IPC_EXCL|0600);
if (msgid == -1) {
perror("msgget()");
exit(1);
}
if (msgctl(msgid, IPC_STAT, &msg_buf) == -1) {
perror("msgctl()");
exit(1);
}
//fork 4 proc
for(int i = 0;i < 4;iPP){
pid = fork();
if(pid == 0){
//send pid
send_buf.mtype = 1;
sprintf(tmp,"%d",getpid());
strncpy(send_buf.mtext, tmp, strlen(tmp));
if (msgsnd(msgid, &send_buf, strlen(send_buf.mtext), 0) == -1){
exit(1);
}
//recv socket
while(1){
//epoll listen
epoll_count = epoll_wait(epoll_fd,events,MAXFDS,-1);
for(int i = 0;i < epoll_count;iPP){
if(events[i].events & EPOLLERR){
close(events[i].data.fd);
continue;
}
//listen to data fd
if(events[i].data.fd == listenfd){
struct sockaddr_in cliaddr;
socklen_t cliaddr_len = sizeof(cliaddr);
int connfd = accept(listenfd,(SA*)&cliaddr,&cliaddr_len);
if(connfd < 0){
if(errno == EAGAIN || errno == EWOULDBLOCK){
//do nothing
printf("accept return EAGAIN or EWOULDBLOCK");
}
else{
perror("accept()");
}
}
event.data.fd = connfd;
event.events = EPOLLIN | EPOLLET;
if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,connfd,&event) == -1){
perror("epoll_ctl()");
}
continue;
}
//logic
else{
if(events[i].events & EPOLLIN){
char buffer[BUFSIZ];
FILE *fp;
int cfd = events[i].data.fd;
send(cfd,"hello",5,0);
//Action
/*
if(read(cfd,buffer,BUFSIZ) == 0){
perror("read()");
}
if (strncmp(buffer,"PUSH",4) == 0){
//recv file
printf("nothing\n");
}
else if (strncmp(buffer,"PULL",4) == 0){
//send file
printf("nothing\n");
}
*/
// first is file name
int byte_r = read(cfd,buffer,BUFSIZ);
if(byte_r == 0){
perror("read()");
}
printf("filename %s\n",buffer);
//check md5
char command[50],ret[100],*token,*md5,mymd5[100];
FILE *myfp;
if(recv(cfd,mymd5,100,0) < 0){
perror("recv()");
}
printf("md5 %s\n",mymd5);
//check file exist
if(access(buffer,F_OK) != -1){
sprintf(command,"md5sum %s",buffer);
myfp = popen(command,"r");
fgets(ret,sizeof(ret)-1,myfp);
token = strtok(ret," ");
md5 = token;
if(strcmp(mymd5,md5) == 0){
printf("File same\n");
continue;
}
fclose(myfp);
}
fp = fopen(buffer,"w");
//File
char Content[BUFSIZ];
while(1){
if((byte_r = read(cfd,Content,BUFSIZ)) > 0){
printf("Content %s\n",Content);
int byte_w = fprintf(fp,"%s",Content);
}
else{
perror("read()");
}
}
fclose(fp);
}
}
}
}
}
}
while(1){
wait(NULL);
}
close(listenfd);
}
void sighandler(int signum){
printf("Caught signal %d,coming out..\n",signum);
//get pid & kill
int msgid;
key_t key;
struct msgbuf buf;
key = ftok(FILEPATH, PROJID);
if (key == -1) {
perror("ftok()");
exit(1);
}
msgid = msgget(key, 0);
if (msgid == -1) {
perror("msgget()");
exit(1);
}
for(int i = 0;i < 4;iPP) {
if (msgrcv(msgid, &buf,BUFSIZ, 1, 0) == -1) {
perror("msgrcv()");
exit(1);
}
printf("childproc: %s\n", buf.mtext);
pid_t childpid;
childpid = atoi(buf.mtext);
kill(childpid,SIGKILL);
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl()");
exit(1);
}
kill(getpid(),SIGKILL);
}
Single Server:
-sharpinclude<stdio.h>
-sharpinclude<sys/types.h>
-sharpinclude<string.h>
-sharpinclude<stdlib.h>
-sharpinclude<sys/socket.h>
-sharpinclude<arpa/inet.h>
-sharpinclude<unistd.h>
-sharpdefine SA struct sockaddr
-sharpdefine LISTENQ 5
int main(int argc,char**argv)
{
int fd,sockfd,listenfd,connfd;
pid_t childpid;
socklen_t client;
struct sockaddr_in servaddr,cliaddr;
//Listen
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(10086);
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
printf("start listen\n");
listen(listenfd,LISTENQ);
client=sizeof(cliaddr);
connfd=accept(listenfd,(SA*)&cliaddr,&client);
char buffer[BUFSIZ];
FILE *fp;
// first is file name
int byte_r = read(connfd,buffer,BUFSIZ);
if(byte_r == 0){
perror("read()");
}
//check md5
char command[50],ret[100],*token,*md5,mymd5[100];
FILE *myfp;
if(recv(connfd,mymd5,100,0) < 0){
perror("recv()");
}
printf("md5 %s\n",mymd5);
if(access(buffer,F_OK) != -1){
sprintf(command,"md5sum %s",buffer);
myfp = popen(command,"r");
fgets(ret,sizeof(ret)-1,myfp);
token = strtok(ret," ");
md5 = token;
if(strcmp(mymd5,md5) == 0){
printf("File same\n");
}
}
fp=fopen(buffer,"w");
//File
while(1){
int byte_r = read(connfd,buffer,BUFSIZ);
if(byte_r == 0){
break;
}
int byte_w = fprintf(fp,"%s",buffer);
}
close(sockfd);
fclose(fp);
fclose(myfp);
printf("the file was received successfully\n");
}
Work perfect in Single Server but Epoll show error read Connection reset by peer
I wondering why Epoll server reset the connection,what"s wrong with my code?
Could anybody help me?
gcc Client.c-o C
gcc Single.c-o Single
gcc Epoll_server.c-o Mul_S
for example:
. / C local file (transfer file to Server side)