网络 (tcp)

 

客户端

/*************************************************************************
	> File Name: client.c
	> Author: yas
	> Mail: rage_yas@hotmail.com
	> Created Time: Thu 22 Aug 2024 04:04:26 PM CST
 ************************************************************************/

#include<stdio.h>
#include <stdio.h>
#include <sys/types.h>	    
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>  
#include <sys/socket.h>
#include <stdlib.h>
int c_fd;
int f_fd;
void handler(int signal)
{
    close(f_fd);
    kill(getpid(),'9');
    wait(NULL);
}
int main(void)
{
    int fd = socket(AF_INET, SOCK_STREAM,0);
    if(fd == -1)
    {
        perror("socket fail");
        return 1;
    }


    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(50000);
    seraddr.sin_addr.s_addr = inet_addr("192.168.149.128");

    if((connect(fd,(const struct sockaddr*)&seraddr,sizeof(seraddr))) < 0)
    {
        perror("connect fail");
        return 1;
    }


    char buf[1024];

    pid_t pid = fork();
    if(pid == -1)
    {
        perror("fork fail");
        return 1;
    }

    if(pid > 0)
    {
        f_fd  = fd;
        while(1)
        {
            signal(SIGCHLD,handler);
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf) - 1] = '\0';
            write(fd,buf,strlen(buf) + 1);

            if(strncmp(buf,"q",1) == 0)
            {
                kill(pid,'9');
                break;
            }

        }
    }
    else if(pid == 0)
    {
        c_fd = fd;
        while(1)
        {
            read(fd,buf,sizeof(buf));
            printf("buf = %s\n", buf);

            if(strncmp(buf,"q",1) == 0)
            {
                close(c_fd);
                break;
            }
        }
    }

    return 0;
}

服务器端 

/*************************************************************************
	> File Name: server.c
	> Author: yas
	> Mail: rage_yas@hotmail.com
	> Created Time: Thu 22 Aug 2024 05:22:36 PM CST
 ************************************************************************/

#include<stdio.h>
#include <stdio.h>
#include <sys/types.h>	    
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int f_fd;
int c_fd;

void handler(int signal)
{
    wait(NULL);
    close(f_fd);
    kill(getpid(),'9');
}
int main(void)
{
    int listenfd = socket(AF_INET, SOCK_STREAM,0);
    if(listenfd == -1)
    {
        perror("socket fail");
        return 1;
    }


    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(50000);
    seraddr.sin_addr.s_addr = inet_addr("192.168.149.128");

    if((bind(listenfd,(const struct sockaddr*)&seraddr,sizeof(seraddr))) < 0)
    {
        perror("connect fail");
        return 1;
    }

    if(listen(listenfd,5) < 0)
    {
        perror("listen fail");
        return 1;
    }

    int connfd = accept(listenfd,NULL,NULL);
    if(connfd == -1)
    {
        perror("connfd fail");
        return 1;
    }

    printf("connfd = %d\n", connfd);

    char buf[1024];


    
    pid_t pid = fork();
    if (pid == -1)
    {
        perror("fork fail");
        return 1;
    }


    if(pid > 0)
    {
        f_fd = listenfd;
        while(1)
        {
            signal(SIGCHLD,handler);
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf) - 1] = '\0';
            write(connfd,buf,strlen(buf) + 1);

            if(strncmp(buf,"q",1) == 0)
            {
                kill(pid,'9');
                break;
            }
        }
    }
    else if(pid == 0)
    {
        c_fd = listenfd;
        while(1)
        {
            read(connfd,buf,sizeof(buf));
            printf("buf = %s\n", buf);
            
            if(strncmp(buf, "q",1) == 0)
            {
                close(c_fd);
                break;
            }
        }
    }

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值