网络编程UNIX

时间:2017-04-11 16:49:44

标签: c++ unix networking

我在'拍卖'编程项目。但是服务器文件不是从客户端读取数据而是在我在客户端写东西后立即终止信号SIGCHLD。任何人都可以建议我,我应该如何读取数据,以防止此错误? 提前致谢。 下面是两个文件的代码片段。

server_auction.cpp

void sig_child(int signo) {
pid_t pid;
int stat;
pid = wait(&stat);
cout<<"child "<<pid<<" terminated with status "<<stat<<endl;

}

void err_sys(const char* x)    { 
perror(x);      // defined in stdio.h
exit(1);        // indicates unsuccessful program termination

}

int main(int argc, char** argv) {
//char*product_name[];
int listenfd, connfd, childpid;
int option;     // option =1 if sell otherwise 0 to buy 
int productNo = 0;
ssize_t n;
char buffer[maxBuffer];
socklen_t clilen;
Product obj[maxProduct];
struct sockaddr_in servaddr, cliaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr));
listen(listenfd, 5);
cout<<"Starting Auction...."<<endl;
signal(SIGCHLD, sig_child);
for(;;) {
    clilen = sizeof(cliaddr);
    if((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen))<0) {
        err_sys("Accept blocks...exiting");
        break;
    }       
    cout<<"running"<<endl;  
    if((childpid = fork())==0)  {
        cout<<"running2"<<endl; 
        close(listenfd);    
        cout<<"running3"<<endl;         
        read(connfd, buffer, maxBuffer);            //option
        cout<<"buffer:"<<buffer;
        if((option = atoi(buffer))==0) {        // sell
            read(connfd, buffer, maxBuffer);            //cust_name         
            strcpy(obj[productNo].cust_name, buffer);
            read(connfd, buffer, maxBuffer);            //product_id
            strcpy(obj[productNo].product_id, buffer);                  
            read(connfd, buffer, maxBuffer);            //product_details
            strcpy(obj[productNo].product_details, buffer);
            read(connfd, buffer, maxBuffer);            //current_bid               
            obj[productNo].initialBidPrice = atof(buffer);  //initialBidPrice
            obj[productNo].current_bid = atof(buffer);      
            read(connfd, buffer, maxBuffer);            //endTime
            struct tm tm;
            strptime(buffer, "%B/%e/%Y-%H:%M:%S", &tm);   // Aug/1/2017-23:02:51
            time_t t = mktime(&tm); 
            obj[productNo].endTime = t;
            time_t startTime;
            time(&startTime);
            obj[productNo].startTime = startTime;           //startTime         
            exit(0);                
            //close(connfd);    
        }
    }
    productNo++;    
    cout<<"Client Joined: "<<childpid<<endl;
}
close(listenfd);
cout<<"Server terminated";
return 0;

}

client_auction.cpp

int main(int argc, char**argv) {
char* category[] = {"Vehicle", "Electronics", "Electricals", "Precious"};
char* category0[] = {"Car", "Bike", "Bicycle", "Tricycle", "Taxi", "Auto"};
char* category1[] = {"Laptop", "Desktop", "Cellphone"};
char* category2[] = {"Refrigerator", "Air Conditioner"};
char* category3[] = {"Watch", "Wood", "Iron"};
//char* productID =(char*)malloc(10);
struct sockaddr_in servaddr, cliaddr;
char buffer[maxBuffer];
int sockfd, choice, option1, option2, initialBidPrice;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
cout<<"Connection established"<<endl;
//while((fgets(buffer,4096,stdin))!=NULL)  {
cout<<"Want to sell(0) or buy(1) ";
cin>>choice;
snprintf(buffer, sizeof(buffer),"%d",choice);
write(sockfd, buffer, maxBuffer);               // option
if(choice == 0) {
    strcpy(buffer,"");              
    cout<<"Enter your name"<<endl;  
    gets(buffer);   
    write(sockfd, buffer, maxBuffer);               // cust_name
    strcpy(buffer,"");              
    cout<<"Choose product category"<<endl;
    cout<<"1. Vehicle\n2. Electronics\n 3. Electricals\n4. Precious"<<endl;
    cin>>option1;
    switch(option1) {
        case 1: cout<<"Choose product item in Vehicle Category"<<endl;
            cout<<"1. Car\n2. Bike\n 3. Bicycle\n4. Tricycle\n5. Taxi\n6. Auto"<<endl;              
            cin>>option2;
            snprintf(buffer, sizeof(buffer),"%d_%d",option1-1, option2-1);
            write(sockfd, buffer, maxBuffer);               //product_id
            strcpy(buffer,"");              
            cout<<"Give your "<<category0[option2-1]<<" details"<<endl;
            gets(buffer);
            write(sockfd, buffer, maxBuffer);
            break;
        case 2: cout<<"Choose product item in Electronics Category"<<endl;
            cout<<"1. Laptop\n2. Desktop\n 3. Cellphone"<<endl;             
            cin>>option2;
            snprintf(buffer, sizeof(buffer),"%d_%d",option1-1, option2-1);
            write(sockfd, buffer, maxBuffer);               //product_id
            strcpy(buffer,"");                              
            cout<<"Give your "<<category1[option2-1]<<" details"<<endl;
            gets(buffer);
            write(sockfd, buffer, maxBuffer);
            break;
        case 3: cout<<"Choose product item in Electricals Category"<<endl;
            cout<<"1. Refrigerator\n2. Air Conditioner"<<endl;              
            cin>>option2;
            snprintf(buffer, sizeof(buffer),"%d_%d",option1-1, option2-1);
            write(sockfd, buffer, maxBuffer);               //product_id
            strcpy(buffer,"");                              
            cout<<"Give your "<<category2[option2-1]<<" details"<<endl;
            gets(buffer);
            write(sockfd, buffer, maxBuffer);               // product_details
            break;
        case 4: cout<<"Choose product item in Precious Category"<<endl;
            cout<<"1. Watch\n2. Wood\n3. Iron"<<endl;               
            cin>>option2;
            snprintf(buffer, sizeof(buffer),"%d_%d",option1-1, option2-1);
            write(sockfd, buffer, maxBuffer);               //product_id
            strcpy(buffer,"");                              
            cout<<"Give your "<<category3[option2-1]<<" details"<<endl;
            gets(buffer);
            write(sockfd, buffer, maxBuffer);
            break;
    }
    cout<<"Enter price to start Bid"<<endl; 
    cin>>initialBidPrice;
    strcpy(buffer,"");              
    snprintf(buffer, sizeof(buffer),"%d",initialBidPrice);
    write(sockfd, buffer, maxBuffer);           // current_bid
    strcpy(buffer,"");              
    cout<<"Enter closing time and date in format e.g., Aug/1/2017-23:02:51"<<endl;
    gets(buffer);
    write(sockfd, buffer, maxBuffer);
}   
exit(1);close(sockfd);
return 0;

}

0 个答案:

没有答案