TCP服务器客户端进程通信

服务器代码

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

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>

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 <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

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 <IP ADDRESS>\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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值