网络 (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 分钟前
如何更改远程桌面连接的默认端口?附外网访问内网计算机方法
服务器·网络·远程工作·访问公司内网
数巨小码人33 分钟前
Linux常见命令
大数据·linux·运维·服务器·elasticsearch·搜索引擎
家庭云计算专家37 分钟前
还没用过智能文档编辑器吗?带有AI插件的ONLYOFFICE介绍
服务器·人工智能·docker·容器·编辑器
北冥SP43 分钟前
OkHttp连接池
网络·okhttp
magic 2451 小时前
第五章:Linux用户管理
linux·运维·服务器
小叶子来了啊1 小时前
信息系统运行管理员:临阵磨枪版
运维·服务器·数据库
是大强1 小时前
ssl 中 key 和pem 和crt是什么关系
网络·网络协议·ssl
-天涯762 小时前
5.13/14 linux安装centos及一些操作命令随记
linux·运维·服务器
-qOVOp-2 小时前
zst-2001 上午题-历年真题 计算机网络(16个内容)
网络·计算机网络·算法
Johny_Zhao2 小时前
VMware workstation 部署微软MDT系统
网络·人工智能·信息安全·微软·云计算·系统运维·mdt