소켓 프로그래밍에서 프로그램 짜다 잘안되서 질문 드립니다.

lee3390의 이미지

프로그램을 짜는도중 잘 안되서 질문을 드립니다.

소켓프로그램을 짜고있는데 클라이언트에서 서버로 쿼리문을 보내면

서버는 클라이언트에서 그 쿼리문을 받아서 sql서버와 통신후 그 쿼리결과를

다시 클라이언트에 보내주어서 클라이언트는 그 쿼리결과를 파일에 저장하는

것입니다.

그런데 쿼리를 주고난 후 받으면 다 받고나서 다운이 되어버립니다.

5개를 연속으로 쿼리를 보내야하는데 1개 받고나서 다운이 되어버리니 다음 쿼

리를 보낼수가 없네요 이거가지고 몇시간동안 고민하다 질문 올립니다.

아래 소스코드 있습니다.

server program code

#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h> 
#include <string.h>
#include <stdio.h> 
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <mysql.h>


#define PORT 7777 // 서버의 포트번호
#define BUFSIZE 512 // 한번에 전송 또는 수신 할 데이터의 양
#define MAX 5 // 최대 접속 대기 수

#define mys_host_name "localhost"
#define mys_user_name "root"
#define mys_password "estee"
#define mys_db_name "trade"

void sig_cld(int signo);

int main(int argc, char *argv[])
{
        struct sockaddr_in saddr, caddr; // 소켓 어드레스 구조체 서버, 클라이언트의 IP, >포트정보를 갖는다.
        int sockfd, newsockfd; // 리스닝 소켓, 컨넥티드 소켓
        char buf[BUFSIZE]; // 1024만큼의 buf를 생성
        int count, cli_len; // count : 실질적인 데이터의 수
//        char complete[10]="complete";
        
        int pid,yes;
        int i;
        int c;
        
        MYSQL *db;
        MYSQL_RES *res_set;
        MYSQL_ROW row;        
        
        db = mysql_init (NULL);
        if(db==NULL) {
            fprintf(stderr,"out of memory\n");
            exit(1);
        }
        
        if (mysql_real_connect ( db,mys_host_name,mys_user_name,mys_password,
                                mys_db_name,0,NULL,0) == NULL) {
            fprintf(stderr,"mysql connection failed : \nError %u (%s)\n",
                            mysql_errno(db),mysql_error(db));
            exit(1);
        }
        
        signal(SIGCHLD,sig_cld);
        
        bzero(buf,sizeof(buf));
        memset(&saddr, 0, sizeof(saddr)); // 메모리 초기화
        saddr.sin_family = AF_INET; // 인터넷 소켓
        saddr.sin_addr.s_addr = htonl(INADDR_ANY);

        saddr.sin_port = htons(PORT); // 포트정보 수록

        sockfd = socket(AF_INET, SOCK_STREAM, 0); // 소켓 생성, 인터넷 소켓, TCP, 0, sockfd = 소켓 번호
        
        setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int));
        
        if( bind(sockfd, (struct sockaddr*) &saddr, sizeof(saddr)) == -1)
        {
                printf("bind Error \n");
                exit(1);
        }
        listen(sockfd, MAX);
        while(1)
        {
                cli_len = sizeof(caddr);
                newsockfd = accept(sockfd,(struct sockaddr*) &caddr, &cli_len);

                if((pid=fork()) < 0) printf("fork error\n");
                if(pid==0) {
                    while((count = read(newsockfd, buf, BUFSIZE)) > 0)
                    {
                        c=1;
                        mysql_query(db,buf);
                        res_set=mysql_store_result(db);
                        while((row=mysql_fetch_row(res_set))!=NULL)
                        {
                            bzero(buf,sizeof(buf));
                            for(i=0;i<mysql_num_fields(res_set);i++) {
                                if(i>0) strcat(buf,",");
                                if(row[i] == NULL) strcat(buf,"NULL");
                                else strcat(buf,row[i]);
                            }
                            strcat(buf,"\n");
                            write(newsockfd,buf,strlen(buf));
                            printf("%d    %s\n",c,buf);
                            c++;
                        }
                        bzero(buf,sizeof(buf));
                        
                        printf("Send complete\n");
                    }
                    close(newsockfd);
                    exit(0);
                }
        }close(sockfd);
        mysql_close(db);
}

void sig_cld(int signo)
{
     int stat;
     pid_t pid;
     while( (pid=waitpid(-1,&stat,WNOHANG)) > 0 );
}

client program code 입니다.

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<signal.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>

#define PORT 7777
#define BUFSIZE 512

int main()
{
    struct sockaddr_in addr;// addr : 접속하고자 하는 서버의 IP와 Port
    int sockfd; // 소켓 번호
    char buf[BUFSIZE];
    int count=0,yes;
    int fd,aaa;
    char file[10][10] = {"data1.dat","data2.dat","data3.dat","data4.dat","data5.dat",
                "data6.dat","data7.dat","data8.dat","data9.dat","data10.dat"};
    
    fd=open("receive.dat",O_CREAT|O_TRUNC|O_WRONLY,00644);
    
    memset(&addr, 0, sizeof(addr)); // 일단 0으로 다 채운다.
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("128.134.54.178");
    addr.sin_port = htons(PORT);
    
    sockfd = socket(AF_INET, SOCK_STREAM,0);
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int));
    
    connect(sockfd,(struct sockaddr*) &addr, sizeof(addr));
    
    while(fgets(buf,BUFSIZE, stdin) > 0 )
    {
        if(strncmp(buf, "quit",4) == 0) break;
        write(sockfd,buf,strlen(buf));
        bzero(buf,sizeof(buf));
        fd=open(file[count],O_CREAT|O_TRUNC|O_WRONLY,00644);
        while((aaa=read(sockfd, buf, BUFSIZE)) > 0) {
//            if ( !strcmp(buf,"complete") ) break;
//            printf("%d\n",aaa);
            write(fd,buf,aaa);
            printf("%s\n",buf);
            bzero(buf,sizeof(buf));
        }
        close(fd);
        printf("Receive complete\n");
        count++;
    };
    close(sockfd);
}
wbrho의 이미지

자세히는 살펴보지 않았지만, 기본적으로 에러처리를 전혀 하지 않는 것이 문제가 될 듯 합니다.
예를 들어, socket(), connect() 함수 등등...

예외처리를 하고 다시 실행하면 문제가 뭔지 발견할 수 있을 듯 합니다.^^

아름다운 세상을 꿈꾸는 아름다운...잔인할 정도로 아름다운 녀석...그대이름은 빈(彬)!!!

june의 이미지

setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)); 음.. 저도 지금 소켓 공부하는데요.. 이 코드는 TIME_WAIT를 바로 죽이시는 옵션을 사용하신거죠? 이것 때문이 아닐까요??

커피는 블랙이나 설탕만..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.