30/09/2018, 18:21

Poll Server, gửi file bị core dump

mình đang làm poll server, phần send, recv string thì okay, nhưng đến khi gửi file thì nó vẫn chạy đc, nhưng mà nó bị core dump ? cái này minh cũng ko hiểu sao laị như thế??
server :

#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#define SERV_PORT 5500
#define LENG 300
#define OPEN_MAX 250
void inhoa(FILE*f1,FILE*f2){
    char ch;
     while((ch= getc(f1))!=EOF){
            ch = toupper(ch);
            putc(ch,f2);
            }
            fclose(f1);
            fclose(f2);
}
void transfer(int nsockfd){
        char rbuf[LENG],sbuf[LENG];
        char* fr_name = "a.txt";
        FILE *f1 = fopen(fr_name, "a");
        if(f1 == NULL){
            printf("[Server]: File %s Cannot be opened file.
", fr_name);
            exit(1);}
        else
        {
            bzero(rbuf, LENG); 
            int leng1 = 0;
            while((leng1 = recv(nsockfd, rbuf, LENG, 0)) > 0) 
            {
                fwrite(rbuf, sizeof(char), leng1, f1);
                bzero(rbuf, LENG);
                if (leng1 == 0 || leng1 != 100) 
                {
                    break;
                }
            }
            printf("[Server]: File has been received!
");
            fclose(f1); 
        }
            printf("[Server]: Processing file ... DONE!
");
            //processing file
            FILE *f2 = fopen("a.txt","r");
            FILE *f3 = fopen("g.txt","w");
            inhoa(f2,f3); 
            char* fs_name = "g.txt"; 
            printf("[Server]: Sending processed file to the Client...
");
            FILE *f4 = fopen(fs_name, "r");
            if(f4 == NULL)
            {
                printf("[Server]: Error when open file !");
                exit (1);
            }   
            bzero(sbuf, LENG); 
            int leng2; 
            while((leng2 = fread(sbuf, sizeof(char), LENG, f4))>0)
            {
                if(send(nsockfd, sbuf, leng2, 0) < 0)
                {
    
                    printf("[Server]: Error when send file 
");
                    exit (1);
                }
                bzero(sbuf, LENG);
            }
            remove("a.txt");
            remove("g.txt");
}
void err_sys(const char* x) 
{ 
    perror(x); 
    exit(1); 
}
int main(int argc, char **argv)
{
	int					i, maxi, listenfd, connfd, sockfd;
	int					nready;
	ssize_t				n;
	char				buf[LENG],ak[10],sign[10]="okay";
	socklen_t			clilen;
	struct pollfd		client[OPEN_MAX];
	struct sockaddr_in	cliaddr, servaddr;
	//create socket
	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd < 0)
	{
		perror("[Server]: can't create socket!
");
		exit(1);
	}
	printf("[Server]: Create socket Sucessfully!
");
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);
	//bind
	if(bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0){
		perror("[Server]: can't bind socket!");
		exit(1);
	}
	printf("[Server]: Bind Sucessfully with 127.0.0.1 Port 5500...
");
	//listen
	listen(listenfd, 10);
	printf("[Server]: Listening ....
");
	client[0].fd = listenfd;
	client[0].events = POLLRDNORM;
	for (i = 1; i < OPEN_MAX; i++)
		client[i].fd = -1;		
	maxi = 0;
	printf("[Server]: Poll processing....
");					
	for ( ; ; ) {
		nready = poll(client, maxi+1, 60000);

		if (client[0].revents & POLLRDNORM) {	/* new client connection */
			printf("[Server]: Has a client come !!!
");
			clilen = sizeof(cliaddr);
			connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
			//save decriptor
			for (i = 1; i < OPEN_MAX; i++)
				if (client[i].fd < 0) {
					client[i].fd = connfd;	
					break;
				}
			if (i == OPEN_MAX)
				err_sys("[Server]: Too many clients coming
");

			client[i].events = POLLRDNORM;
			if (i > maxi)
				maxi = i;			
			// no descriptor can read
			if (--nready <= 0)
				continue;			
		}
		//check incoming client to recv data
		for (i = 1; i <= maxi; i++) {
			if ( (sockfd = client[i].fd) < 0)
				continue;
			if (client[i].revents & (POLLRDNORM | POLLERR)) {
				transfer(sockfd);
				if (--nready <= 0)
					break;				/* all done */
			}
		}
	}
	return 0;
}

và đây là client

#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 5500
#define LENG 100
int leng(FILE*f){
        char c = fgetc(f);
        int i=0;
        while(c != EOF){
        c = fgetc(f);
        i++;
    }
    return i;
}
int main(int argc, char *argv[])
{
    int sockfd,nsockfd;
    long size;
    char rbuf[LENG],sbuf[LENG],ak[10] = "y"; 
    struct sockaddr_in server_addr;
    //create socket
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {  
        printf("[Client]: Socket cannot be created !
");
        exit (1);
    }
    //set up socket information
    server_addr.sin_family = AF_INET; 
    server_addr.sin_port = htons(PORT); 
    inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr); 
    bzero(&(server_addr.sin_zero), 8);
    //connect
    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
    {
        printf("[Client]: ERROR when connect to server!
");
        exit (1);
    }
    else 
        printf("[Client]: Connected to server at port %d...Succsess!
", PORT);
 /**********************************************************************************/
        //Send file to server
        printf("Enter source file name(ex:source/source.txt):	 ");
        char fs_name[40];
        scanf("%s",fs_name);
        printf("[Client]: FILE %s is being sent!
", fs_name);
        FILE *f1 = fopen(fs_name, "r");
        if(f1 == NULL)
        {
            printf("[Client]: ERROR: File %s not found.
", fs_name);
            exit (1);
        }
        if(leng(f1) == 0){
            printf("[Client]: Process Done!!
");
            exit (1);
        }
        // count byte sent
        fseek(f1, 0, SEEK_END);
        size=ftell (f1);
        rewind(f1);
        bzero(sbuf, LENG); 
        int leng1; 
        while((leng1 = fread(sbuf, sizeof(char), LENG, f1)) > 0)
        {
            if(send(sockfd, sbuf, leng1, 0) < 0)
            {
                printf("[Client]: ERROR when send file
");
                break;
            }
            bzero(sbuf, LENG);
        }
        printf("[Client]: File %s has been sent!
", fs_name); 
        printf ("[Client]: Sent bytes: %ld bytes.
",size);
  

    // Receive File from Server 
    printf("[Client]: result file is being Received! 
");
    char* fr_name = "result.txt";
    FILE *f2 = fopen(fr_name, "a");
    if(f2 == NULL){
        printf("[Client]: File Cannot be opened.
");
        exit(1);}
    else
    {
        bzero(rbuf, LENG); 
        int leng2 = 0;
        while((leng2 = recv(sockfd, rbuf, LENG, 0)) > 0)
        {
            fwrite(rbuf, sizeof(char), leng2, f2);
            bzero(rbuf, LENG);
            if (leng2 == 0 || leng2 != 100) 
            {
                break;
            }
        }
        printf("[Client]: Result.txt file has been Received!
");
        fclose(f2);
    }
    close (sockfd);
    printf("[Client]: Disconnect.
");
    return (0);
}

mong các bạn chỉ giáo

Mai Anh Dũng viết 20:23 ngày 30/09/2018

Gợi ý đoạn code có khả năng bị dump và dump ra cái gì thế?

Bài liên quan
0