网络 (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;
}
相关推荐
白帽黑客沐瑶3 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
christine-rr3 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆3 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
树码小子3 天前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
乌萨奇也要立志学C++3 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
绿箭柠檬茶3 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
real 13 天前
传输层协议UDP
网络·网络协议·udp
獭.獭.3 天前
Linux -- 信号【上】
linux·运维·服务器
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
ZERO_pan3 天前
服务器装机遇到的问题
运维·服务器