网络 (tcp)

客户端

cs 复制代码
/*************************************************************************
	> 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;
}

服务器端

cs 复制代码
/*************************************************************************
	> 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;
}
相关推荐
hour_go4 分钟前
TCP/IP协议相关知识点
网络·笔记·网络协议·tcp/ip
Arva .1 小时前
WebSocket实现网站点赞通知
网络·websocket·网络协议
雪兽软件1 小时前
SaaS 安全是什么以及如何管理风险
网络·安全
纸带1 小时前
USB -- SET_ADDRESS or --SET_ADDRESS or --SET_CONFIGURATION or --SET_INTERFACE
网络
white-persist1 小时前
CSRF 漏洞全解析:从原理到实战
网络·python·安全·web安全·网络安全·系统安全·csrf
嫄码1 小时前
TCP/IP 四层模型
网络·网络协议·tcp/ip
游戏开发爱好者81 小时前
FTP 抓包分析实战,命令、被动主动模式要点、FTPS 与 SFTP 区别及真机取证流程
运维·服务器·网络·ios·小程序·uni-app·iphone
liebe1*12 小时前
第三章 常用协议
网络
key062 小时前
大模型在网络安全领域的应用与评测
网络·人工智能·web安全
望获linux2 小时前
【实时Linux实战系列】实时 Linux 的自动化基准测试框架
java·大数据·linux·运维·网络·elasticsearch·搜索引擎