TCP服务器客户端进程通信

服务器代码

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/

#include 
#include 
#include 
#include 
#include 

void upper(char *msg)
{
    while(*msg)
    {
        *msg =toupper(*msg);
        *msg++;
    }
}
int main()
{
    pid_t pid; 
    int listenfd,connfd;
    int n;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t client;
    char msg[1000];

    listenfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(8888);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    listen(listenfd,1024);

    for(;;)
    {
        client = sizeof(cliaddr);

        connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&client);

        if(pid =fork() == 0)
        {
            close(listenfd);
            for(;;)
            {
                n = recvfrom(connfd,msg,1000,0,(struct sockaddr *)&cliaddr,&client);
                if(n == 0)
                {
                    printf("client is closed\n");
                    exit(-1);
                }

                printf("--------------------------------------------\n");
                printf("%s",msg);
                printf("--------------------------------------------\n");

                upper(msg);

                sendto(connfd,msg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

//              printf("--------------------------------------------\n");
//              printf("%s",msg);
//              printf("--------------------------------------------\n");

            }
            close(connfd);
        }
    }

    close(listenfd);

    return 0;
}

客户端代码

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/

#include 
#include 
#include 
#include 
#include 

int main(int argc,char **argv)
{
    int sockfd;
    int n;
    struct sockaddr_in servaddr,cliaddr;
    char sendbuf[1000];
    char recvbuf[1000];

    if(argc != 2)
    {
        printf("Usage:client \n");
        exit(-1);
    }

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
    servaddr.sin_port = htons(8888);

    connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    while(fgets(sendbuf,1000,stdin) != NULL)
    {

        sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&servaddr,sizeof(servaddr));

        n = recvfrom(sockfd,recvbuf,1000,0,NULL,NULL);

        recvbuf[n] = '\0';

        fputs(recvbuf,stdout);

    }

    close(sockfd);

    return 0;
}

你可能感兴趣的:(TCP服务器客户端进程通信)