基于套接字与父子进程实现服务器端与客户端的发送与接受的实现

该办法主要是实现服务器端和客户端,实现双端的消息接受与发送

实现思路:
主要是在服务端和客户端分别创建两个进程
服务器端:一个进程发送数据,一个进程接受数据
客户端:  一个进程发送数据,一个进程接受数据

服务器端 server.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>

#define DEF_VAL 0
#define ERR_VAL -1
#define ERR_LOG(VAL)   \
    do                 \
    {                  \
        perror(VAL);   \
        exit(ERR_VAL); \
    } while (0)

int main(int argc, char const *argv[])
{
    //创建流式套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, DEF_VAL);
    if (sockfd == ERR_VAL)
    {
        ERR_LOG("socket");
    }
    printf("---------------------success!----------------------\n");
    struct sockaddr_in saddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(atoi(argv[1]));                // 把主机字节序换成网络字节序
    saddr.sin_addr.s_addr = inet_addr("192.168.255.128"); // 把点分十进制的ip地址转换成网络字节序
    // ip与套接子绑定
    int ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
    if (ret == ERR_VAL)
    {
        ERR_LOG("bind");
    }
    printf("-------------------bind success!-------------------\n");
    // 把套接字设置成监听套接字,等待队列长度为10
    ret = listen(sockfd, 10);
    if (ret == ERR_VAL)
    {
        ERR_LOG("listen");
    }
    printf("------------------listen success!-------------------\n");
    // 从等待队列取出连接请求,开始连接,产生通信套接字
    char buf[1024] = {0};
    int connfd = 0;
    memset(buf, 0, sizeof(buf));
    connfd = accept(sockfd, NULL, NULL);
    if (connfd == ERR_VAL)
    {
        ERR_LOG("conn");
    }
    printf("------------------accept success!-------------------\n");
    int pid = fork();
    if (pid < 0)
    {
        ERR_LOG("fork");
    }
    else if (pid == 0)
    {
        while (1)
        {
            ret = read(connfd, buf, sizeof(buf));
            if (ret <= 0)
            {
                printf("客户端异常退出!\n");
                close(sockfd);
                close(connfd);
                exit(-1);
            }
            else
            {
                printf("read %d bytes : %s", ret, buf);
            }
            if (!strcmp(buf, "quit\n"))
                break;
            //write(connfd, buf, ret);
            memset(buf, 0, sizeof(buf));
        }
    }
    else
    {
        while (1)
        {
            if (!strcmp(buf, "quit\n"))
                break;
            memset(buf, 0, sizeof(buf));
            fgets(buf, sizeof(buf), stdin);
            write(connfd, buf, strlen(buf));
        }
    }
    close(sockfd);
    close(connfd);
    return 0;
}

客户端 client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>

#define HOST_PORT 8888
#define HOST_IP "192.168.191.128"
#define DEF_VAL 0
#define ERR_VAL -1
#define ERR_LOG(VAL)   \
    do                 \
    {                  \
        perror(VAL);   \
        exit(ERR_VAL); \
    } while (0)
int main(int argc, char const *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, DEF_VAL);
    if (sockfd == ERR_VAL)
    {
        ERR_LOG("socket");
    }
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));

    int connfd = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
    if (connfd == ERR_VAL)
    {
        ERR_LOG("conn");
    }
    printf("----------------connection success!-----------------\n");
    char buf[128] = {0};
    int pid = fork(),ret;
    if (pid < 0)
    {
        ERR_LOG("fork");
    }
    else if (pid == 0)
    {
        while (1)
        {
            ret = read(sockfd, buf, sizeof(buf));
            if (ret <= 0)
            {
                printf("服务器端异常退出!\n");
                close(sockfd);
                exit(-1);
            }
            else {
                printf("read %d bytes : %s", ret, buf);
            }
            if (!strcmp(buf, "quit\n"))
                break;
            //write(connfd, buf, ret);
            memset(buf, 0, sizeof(buf));
        }
    }
    else
    {
        while (1)
        {
            if (!strcmp(buf, "quit\n"))
                break;
            memset(buf, 0, sizeof(buf));
            fgets(buf, sizeof(buf), stdin);
            write(sockfd, buf, strlen(buf));
        }
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追meng赤子心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值