[C언어][소켓프로그래밍]While 문으로 돌리는데 처음 한번만 전송이 됩니다 고수님들 도와주세요
글쓴이: baw518 / 작성시간: 목, 2011/12/15 - 2:30오전
정각이 되면 클라이언트에서 압축한 파일을 서버로 보내는 소스입니다.
한번은 보내지는데 다음 정각 부터는 안가네요 간단한거 같은데 뭐가 문젠지 모르겠습니다.도와주세요
아래는 소스입니다.
------------------------------------------------------------------------------------
client.c
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<fcntl.h> #include<arpa/inet.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<pthread.h> #include<time.h> #define BUFSIZE 4096 #define MAX 512 char mib(); int main (int argc, char**argv[]) { int fd; char buf[BUFSIZ]; struct tm *systime; time_t t; int c_socket; struct sockaddr_in c_addr; int retval; int n; if(argc!=3){ printf("Usage : %s <IP> <PORT> \n", argv[0]); exit(1); } c_socket=socket(PF_INET,SOCK_STREAM,0); if(c_socket == -1) printf("socket() error"); memset(&c_addr,0,sizeof(c_addr)); c_addr.sin_family=AF_INET; c_addr.sin_addr.s_addr=inet_addr(argv[1]); c_addr.sin_port=htons(atoi(argv[2])); retval = connect(c_socket, (struct sockaddr*)&c_addr,sizeof(c_addr)); if(retval == -1) printf("connect() error!"); while(1){ t = time(NULL); systime = localtime(&t); if ( systime -> tm_min == 0) { //printf("%d\n",systime -> tm_min); if( systime -> tm_sec == 0) { //printf("%d\n",systime -> tm_sec); int year,mon,day,hour; char date[BUFSIZ]; year = systime->tm_year +1900; mon = systime->tm_mon +1; day = systime->tm_mday; hour = systime->tm_hour; sprintf(date,"%d년%d월%d일%02d시",year,mon,day,hour); printf ("%s\n",date); mib(); sleep(1); FILE *fp = fopen(date, "rb"); if(fp == NULL) printf("fopen() error"); char filename[256]; memset(filename, 0, sizeof(filename)); sprintf(filename, date); retval = write(c_socket, filename, sizeof(filename)); if(retval == -1) printf("write() error"); fseek(fp, 0, SEEK_END); int totalbytes = ftell(fp); retval = write(c_socket, (char *)&totalbytes, sizeof(totalbytes)); if(retval == -1) printf("write() error"); char buf[BUFSIZE]; int numread; int numtotal = 0; rewind(fp); while(1) { numread = fread(buf, 1, BUFSIZE, fp); if(numread > 0) { retval = write(c_socket, buf, numread); if(retval == -1) printf("write() error!"); numtotal += numread; } else if(numread == 0 && numtotal == totalbytes) { printf("file trans complete : %d bytes\n", numtotal); break; } else { printf("file I/O error"); } } fclose(fp); } } } close(c_socket); return 0; } char mib(){ system("rdate -s time.time2.kriss.re.kr && hwclock -w"); system("last > last.txt"); system("ps > ps.txt"); system("ifconfig > ifconfig.txt"); system("uptime > uptime.txt"); system("ls > ls.txt"); system("df -h > df.txt"); system("tar -cvf `date +%Y년%m월%d일%H시` last.txt ps.txt ifconfig.txt uptime.txt ls.txt df.txt"); system("rm -rf *.txt"); return 0; }
server.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #define BUFSIZE 4096 void err_quit(char *msg) { fputs(msg, stderr); fputc('\n', stderr); exit(1); } int recvn(int s, char *buf, int len) { int received; char *ptr = buf; int left = len; while(left > 0) { received = read(s, ptr, left); if(received == -1) return -1; else if(received == 0) break; left -= received; ptr += received; } return (len - left); } int main(int argc, char *argv[]) { int retval; int listen_sock = socket(PF_INET, SOCK_STREAM, 0); if(listen_sock == -1) err_quit("socket() error"); struct sockaddr_in serveraddr; memset(&serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(9000); serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); retval = bind(listen_sock, (struct sockaddr*) &serveraddr, sizeof(serveraddr)); if(retval == -1) err_quit("bind() error"); retval = listen(listen_sock, 5); if(retval == -1) err_quit("listen() error"); int client_sock; struct sockaddr_in clientaddr; int addrlen; char buf[BUFSIZE]; while(1) { addrlen = sizeof(clientaddr); client_sock = accept(listen_sock, (struct sockaddr*) &clientaddr, &addrlen); if(client_sock == -1) err_quit("accept() error"); printf("\n->FileSender connect : IP = %s, Port = %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); char filename[256]; memset(filename, 0, sizeof(filename)); retval = recvn(client_sock, filename, 256); if(retval == -1) { err_quit("recv() error"); close(client_sock); continue; } printf("-> filename : %s\n", filename); int totalbytes; retval = recvn(client_sock, (char *) &totalbytes, sizeof(totalbytes)); if(retval == -1) { err_quit("recv() error"); close(client_sock); continue; } printf("-> filesize : %d\n", totalbytes); FILE *fp = fopen(filename, "wb"); if(fp == NULL) { err_quit("File I/O error"); close(client_sock); continue; } int numtotal = 0; while(1) { retval = recvn(client_sock, buf, BUFSIZE); if(retval == -1) { err_quit("recv() error"); break; } else if(retval == 0) break; else { fwrite(buf, 1, retval, fp); numtotal += retval; } } fclose(fp); if(numtotal == totalbytes) printf("-> File trans complete\n"); else printf("-> File trans failed\n"); close(client_sock); printf("FileSender complete : IP = %s, Port = %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); } close(listen_sock); return 0; }
Forums:
client.c 를 이렇게 수정해 보니 잘 되네요
너무너무 감사함니다.
저는 서버에서 동작을 제대로 못하길래 서버문제일줄알고 한참 해매고만있었는데
클라이언트 문제였군요 ㅠㅠ pogusm 님 너무너무 감사함니다. 사실 포기 지경이였는데
다시한면 힘내서 해보렵니다. ^^
댓글 달기