2024-3-4-网络编程作业

1>流式域套接字

服务器代码:

#include <myhead.h>
int main(int argc, const char *argv[])
{
	int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sfd == -1)
	{
		perror("socket error");
		return -1;
	}
	if (access("./mysocket", F_OK) == 0)
	{
		if (unlink("./mysocket") == -1)
		{
			perror("unlink error");
			return -1;
		}
	}
	struct sockaddr_un sun;
	sun.sun_family = AF_UNIX;
	strcpy(sun.sun_path, "./mysocket");

	//2.2绑定
	if (bind(sfd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");

	//3、监听
	if (listen(sfd, 128) == -1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success\n");

	//4、阻塞等待客户端链接请求
	定义容器接收客户端地址信息
	struct sockaddr_un cun;
	socklen_t socklen = sizeof(cun);

	int newfd = -1;
	if ((newfd = accept(sfd, (struct sockaddr *)&cun, &socklen)) == -1)
	{
		perror("accept error");
		return -1;
	}
	printf("有新客户发来连接请求\n");

	//5、数据收发

	char rbuf[128] = "";
	while (1)
	{
		//清空
		bzero(rbuf, sizeof(rbuf));

		//收取数据
		recv(newfd, rbuf, sizeof(rbuf), 0);

		printf("[%s] :%s\n", cun.sun_path, rbuf);

		//连接一个笑脸回复
		strcat(rbuf, "*_*");

		//发送回去
		send(newfd, rbuf, strlen(rbuf), 0);
		printf("发送成功\n");
	}

	//6、关闭套接字
	close(sfd);
	close(newfd);

	return 0;
}

客户端代码:

#include <myhead.h>

int main(int argc, const char *argv[])
{
    //1、创建用于通信的套接字文件描述符
    int cfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (cfd == -1)
    {
        perror("socket error");
        return -1;
    }
    printf("cfd = %d\n", cfd); 

    //判断套接字文件是否存在,如果存在则需要删除
    if (access("./mysocket1", F_OK) == 0)
    {
        //说明存在,将该文件删除
        if (unlink("./mysocket1") == -1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、绑定(非必须)
    //2.1 填充地址信息结构体
    struct sockaddr_un cun;
    cun.sun_family = AF_UNIX;
    strcpy(cun.sun_path, "./mysocket1");

    //2.2 绑定
    if (bind(cfd, (struct sockaddr *)&cun, sizeof(cun)) == -1)
    {
        perror("bind error");
        return -1;
    }

    printf("bind success\n");

    //3、连接服务器
    //3.1填充要连接的服务器地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;           //地址族
    strcpy(sun.sun_path, "./mysocket"); //套接字文件

    //3.2 连接服务器
    if (connect(cfd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
    {
        perror("connect error");
        return -1;
    }
    printf("connect success\n");

    //4、数据收发
    char wbuf[128] = "";
    while (1)
    {
        //清空数据
        bzero(wbuf, sizeof(wbuf));

        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin); //从终端输入
        wbuf[strlen(wbuf) - 1] = 0;

        //将数据发送给服务器
        send(cfd, wbuf, strlen(wbuf), 0);
        printf("发送成功\n");

        //判断发送的数据
        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }

        //接收服务器发来的消息
        //清空数据
        bzero(wbuf, sizeof(wbuf));
        recv(cfd, wbuf, sizeof(wbuf), 0);

        printf("收到消息为:%s\n", wbuf);
    }
    //5
    close(cfd);

    return 0;
}

2>报式套接字

服务器代码:

#include<myhead.h>

int main(int argc, const char *argv[])
{
    //1、创建用于通信的套接字
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }
    printf("sfd = %d\n", sfd);           //3

    //判断套接字文件是否存在,如果存在则需要删除
    if(access("./linux1", F_OK) == 0)
    {
        //说明存在,将该文件删除
        if(unlink("./linux1") == -1)
        {
            perror("unlink error");
            return -1;
        }
    }


    //2、绑定IP地址和端口号
    //2.1填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;          //地址族
strcpy(sun.sun_path, "./linux1");

    //2,2绑定
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    //3、收发数据
    char rbuf[128] = "";

    //定义容器接收对端地址信息结构体
    struct sockaddr_un cun;
    socklen_t socklen = sizeof(cun);

    while(1)
    {
        //清空数组
        bzero(rbuf, sizeof(rbuf));

        //接收数据
        recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cun, &socklen);

        printf("收到消息为:%s\n", rbuf);

        //将消息加个笑脸回过去
        strcat(rbuf, "*_*");
        if(sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, socklen) == -1)
        {
            perror("sendto error");
            return -1;
        }
        
    }

    //4、关闭套接字
    close(sfd);

    return 0;
}

客户端代码:

#include<myhead.h>



int main(int argc, const char *argv[])
{
    //1、创建用于通信的套接字
    int cfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(cfd == -1)
    {
        perror("socket error");
        return -1;
    }
    printf("cfd = %d\n", cfd);           


    //判断套接字文件是否存在,如果存在则需要删除
    if(access("./linux2", F_OK) == 0)
    {
        //说明存在,将该文件删除
        if(unlink("./linux2") == -1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、绑定IP地址和端口号(如果要接收服务器消息必须绑定)
    struct sockaddr_un cun;
    cun.sun_family = AF_UNIX;
    strcpy(cun.sun_path, "./linux2");

    if(bind(cfd, (struct sockaddr*)&cun, sizeof(cun))==-1)
    {
        perror("bind error");
        return -1;
    }


    //3、收发数据
    char wbuf[128] = "";

    //定义容器,记录服务器的地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path,"./linux1");

    while(1)
    {
        //清空数组
        bzero(wbuf, sizeof(wbuf));

        //从终端获取数据
        printf("请输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf)-1] = 0;

        //将数据发送给服务器
        sendto(cfd, wbuf, sizeof(wbuf), 0, (struct sockaddr*)&sun, sizeof(sun));
        
        printf("发送成功\n");


        //接收服务器回复的消息
        bzero(wbuf, sizeof(wbuf));
        recvfrom(cfd, wbuf, sizeof(wbuf), 0, NULL, NULL);
        printf("收到消息为:%s\n", wbuf);
    }
    //4
    close(cfd);

    return 0;
}

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值