网络 (tcp)

客户端

cs 复制代码
/*************************************************************************
	> File Name: client.c
	> Author: yas
	> Mail: [email protected]
	> 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;
}

服务器端

cs 复制代码
/*************************************************************************
	> File Name: server.c
	> Author: yas
	> Mail: [email protected]
	> 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;
}
相关推荐
啵啵学习2 小时前
Linux 里 su 和 sudo 命令这两个有什么不一样?
linux·运维·服务器·单片机·ubuntu·centos·嵌入式
网硕互联的小客服3 小时前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
Yungoal3 小时前
php & apache构建 Web 服务器
服务器·php·apache
浩浩测试一下4 小时前
Authpf(OpenBSD)认证防火墙到ssh连接到SSH端口转发技术栈 与渗透网络安全的关联 (RED Team Technique )
网络·网络协议·tcp/ip·安全·网络安全·php
冰橙子id4 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
咕噜企业签名分发-淼淼4 小时前
应用app的服务器如何增加高并发
运维·服务器
leagsoft_10034 小时前
联软NSPM自动化策略管理 助力上交所加速国产化替代提升运维效率
运维·网络·自动化
孤寂大仙v5 小时前
【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑
tcp/ip·计算机网络·php
漫步者TZ6 小时前
【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
java·网络协议·tcp/ip·netty
leagsoft_10036 小时前
筑牢企业网管域安全防线,守护数字核心——联软网管域安全建设解决方案
网络·安全·网络安全