C语言版本socket server

#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
#include <sys/types.h>   
#include <sys/socket.h>  
#include <netinet/in.h>  
  
  
void doprocessing (int sock)  
{  
        int n,i_recvBytes;  
    
        char buffer[256];  
       //bzero(buffer,256);  
  
     while(1){  
       
         printf("waiting for request...\n");  
         //Reset data.  
         memset(buffer,0,256);  
   
         i_recvBytes = read(sock,buffer,255);  
         if(i_recvBytes == 0){  
             printf("Maybe the client has closed\n");  
             break;  
         }  
         if(i_recvBytes == -1){  
             fprintf(stderr,"read error!\n");  
             break;  
         }  
         if(strcmp(buffer,"quit")==0){  
             printf("Quit command!\n");  
             break;                           //Break the while loop.  
         }  
         printf("read from client : %s\n",buffer);  
         if(write(sock,buffer,strlen(buffer)) == -1){  
             break;  
         }  
     }  
  
}  
  
  
int main( int argc, char *argv[] )  
{  
    int sockfd, newsockfd, portno, clilen;  
    char buffer[256];  
    struct sockaddr_in serv_addr, cli_addr;  
    int pid;
    int  n;  
  
    /* First call to socket() function */  
    sockfd = socket(AF_INET, SOCK_STREAM, 0);  
    if (sockfd < 0)   
    {  
        perror("ERROR opening socket");  
        exit(1);  
    }  
    /* Initialize socket structure */  
    bzero((char *) &serv_addr, sizeof(serv_addr));  
    portno = 5001;  
    serv_addr.sin_family = AF_INET;  
    serv_addr.sin_addr.s_addr = INADDR_ANY;  
    serv_addr.sin_port = htons(portno);  
   
    /* Now bind the host address using bind() call.*/  
    if (bind(sockfd, (struct sockaddr *) &serv_addr,  
                          sizeof(serv_addr)) < 0)  
    {  
         perror("ERROR on binding");  
         exit(1);  
    }  
    /* Now start listening for the clients, here  
     * process will go in sleep mode and will wait  
     * for the incoming connection 
     */  
    listen(sockfd,5);  
    clilen = sizeof(cli_addr);  
    while (1)   
    {  
        newsockfd = accept(sockfd,   
                (struct sockaddr *) &cli_addr, &clilen);  
        if (newsockfd < 0)  
        {  
            perror("ERROR on accept");  
            exit(1);  
        }  
        /* Create child process */  
        pid = fork();  
        if (pid < 0)  
        {  
            perror("ERROR on fork");  
        exit(1);  
        }  
        if (pid == 0)    
        {  
            /* This is the client process */  
            close(sockfd);  
            doprocessing(newsockfd);  
            exit(0);  
        }  
        else  
        {  
            close(newsockfd);//shutdown(newsockfd);或许会更好  
        }  
    } /* end of while */  
}  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值