IO网络编程Day4

广播

#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //2、将套接字设置成允许广播
    int broadcast = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast))==-1)
    {
        perror("setsockopt error");
        return -1;
    }
    
    //3、绑定(非必须)
    //4、填充接收端地址信息结构体
    struct sockaddr_in cin;
    cin.sin_family = AF_INET;
    cin.sin_port = htons(6789);
    cin.sin_addr.s_addr = inet_addr("192.168.125.255");

    //5、发送消息
    char sbuf[128] = "";
    while(1)
    {
        printf("请输入>>>");
        fgets(sbuf, sizeof(sbuf), stdin);
        sbuf[strlen(sbuf)-1] = 0;

        //发送到广播地址中
        sendto(sfd, sbuf, sizeof(sbuf), 0, (struct sockaddr*)&cin, sizeof(cin));
        printf("发送成功\n");
    }
    

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

    return 0;
}
#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int rfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(rfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //2、填充地址信息结构体
    struct sockaddr_in rin;
    rin.sin_family = AF_INET;
    rin.sin_port = htons(6789);       //端口号
    rin.sin_addr.s_addr = inet_addr("192.168.125.255"); //广播地址

    //3、绑定
    if(bind(rfd, (struct sockaddr*)&rin, sizeof(rin)) == -1)
    {
        perror("bind error");
        return -1;
    }

    //4、接收消息
    char rbuf[128] = "";
    while(1)
    {
        //清空数组
        bzero(rbuf , sizeof(rbuf));

        //接收消息
        recvfrom(rfd, rbuf, sizeof(rbuf), 0, 0, 0);

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

    //5、关闭套接字
    close(rfd);

    return 0;
}

组播

#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //2、绑定  非必须
    
    //3、填充地址信息结构体
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(9999);
    sin.sin_addr.s_addr = inet_addr("224.1.2.3");

    //4、发送消息
    char sbuf[128] = "";
    while(1)
    {
        printf("请输入>>>");
        fgets(sbuf, sizeof(sbuf), stdin);
        sbuf[strlen(sbuf)-1] = 0;

        //将消息发送出去
        sendto(sfd, sbuf, sizeof(sbuf), 0, (struct sockaddr*)&sin, sizeof(sin));
        printf("发送成功\n");
    }

    //5、关闭套接字
    close(sfd);
    return 0;
}
#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int rfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(rfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //2、设置网络属性
    struct ip_mreqn im;
    im.imr_multiaddr.s_addr = inet_addr("224.1.2.3");    //组播地址
    im.imr_address.s_addr = inet_addr("192.168.122.118");  //本机ip
    im.imr_ifindex = 2;             //网络索引

    if(setsockopt(rfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &im, sizeof(im)) == -1)
    {
        perror("setsockopt error");
        return -1;
    }
    printf("加入多播组成功\n");

    //3、填充地址信息结构体
    struct sockaddr_in rin;
    rin.sin_family = AF_INET;
    rin.sin_port = htons(9999);
    rin.sin_addr.s_addr = inet_addr("224.1.2.3");

    //4、绑定
    if(bind(rfd, (struct sockaddr*)&rin, sizeof(rin)) == -1)
    {
        perror("bind error");
        return -1;
    }

    //5、接收消息
    char rbuf[128] = "";
    while(1)
    {
        bzero(rbuf, sizeof(rbuf));
        recvfrom(rfd, rbuf, sizeof(rbuf), 0, 0, 0);
        printf("收到消息为:%s\n", rbuf);
    }

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

    return 0;
}

流式域套接字

#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    //int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }


    //由于域套接字的绑定函数,只能绑定一个不存在的套接字文件
    //所以,在绑定之前需要判断当前文件是否存在
    if(access("./unix", F_OK) == 0)
    {
        //表示文件存在,删除该文件
        if(unlink("./unix")==-1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;        //通信域
    //sun.sun_path = ".unix";            //字符串赋值不能使用赋值运算符
    strcpy(sun.sun_path, "./unix");      //绑定套接字文件

    //3、绑定地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    //4、监听
    if(listen(sfd, 128) ==-1)
    {
        perror("listen error");
        return -1;
    }

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

    int newfd = accept(sfd, (struct sockaddr*)&cun, &socklen);    //表示不接收客户端地址信息
    if(newfd == -1)
    {
        perror("accept error");
        return -1;
    }

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

        int res = recv(newfd, buf, sizeof(buf), 0);       //读取消息
        if(res == 0)
        {
            printf("客户端已经下线\n");
            break;
        }
        printf("[%s]: %s\n", cun.sun_path ,buf);

    }

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


    return 0;
}
#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    //int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }


    //由于域套接字的绑定函数,只能绑定一个不存在的套接字文件
    //所以,在绑定之前需要判断当前文件是否存在
    if(access("./unix", F_OK) == 0)
    {
        //表示文件存在,删除该文件
        if(unlink("./unix")==-1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;        //通信域
    //sun.sun_path = ".unix";            //字符串赋值不能使用赋值运算符
    strcpy(sun.sun_path, "./unix");      //绑定套接字文件

    //3、绑定地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    //4、监听
    if(listen(sfd, 128) ==-1)
    {
        perror("listen error");
        return -1;
    }

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

    int newfd = accept(sfd, (struct sockaddr*)&cun, &socklen);    //表示不接收客户端地址信息
    if(newfd == -1)
    {
        perror("accept error");
        return -1;
    }

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

        int res = recv(newfd, buf, sizeof(buf), 0);       //读取消息
        if(res == 0)
        {
            printf("客户端已经下线\n");
            break;
        }
        printf("[%s]: %s\n", cun.sun_path ,buf);

    }

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


    return 0;
}

报式域套接字

#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    //int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }


    //由于域套接字的绑定函数,只能绑定一个不存在的套接字文件
    //所以,在绑定之前需要判断当前文件是否存在
    if(access("./unix", F_OK) == 0)
    {
        //表示文件存在,删除该文件
        if(unlink("./unix")==-1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;        //通信域
    //sun.sun_path = ".unix";            //字符串赋值不能使用赋值运算符
    strcpy(sun.sun_path, "./unix");      //绑定套接字文件

    //3、绑定地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    //4、监听
    if(listen(sfd, 128) ==-1)
    {
        perror("listen error");
        return -1;
    }

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

    int newfd = accept(sfd, (struct sockaddr*)&cun, &socklen);    //表示不接收客户端地址信息
    if(newfd == -1)
    {
        perror("accept error");
        return -1;
    }

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

        int res = recv(newfd, buf, sizeof(buf), 0);       //读取消息
        if(res == 0)
        {
            printf("客户端已经下线\n");
            break;
        }
        printf("[%s]: %s\n", cun.sun_path ,buf);

    }

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


    return 0;
}
#include

int main(int argc, const char *argv[])
{
    //1、创建套接字
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    //int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }


    //由于域套接字的绑定函数,只能绑定一个不存在的套接字文件
    //所以,在绑定之前需要判断当前文件是否存在
    if(access("./unix", F_OK) == 0)
    {
        //表示文件存在,删除该文件
        if(unlink("./unix")==-1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;        //通信域
    //sun.sun_path = ".unix";            //字符串赋值不能使用赋值运算符
    strcpy(sun.sun_path, "./unix");      //绑定套接字文件

    //3、绑定地址信息结构体
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

    //4、监听
    if(listen(sfd, 128) ==-1)
    {
        perror("listen error");
        return -1;
    }

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

    int newfd = accept(sfd, (struct sockaddr*)&cun, &socklen);    //表示不接收客户端地址信息
    if(newfd == -1)
    {
        perror("accept error");
        return -1;
    }

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

        int res = recv(newfd, buf, sizeof(buf), 0);       //读取消息
        if(res == 0)
        {
            printf("客户端已经下线\n");
            break;
        }
        printf("[%s]: %s\n", cun.sun_path ,buf);

    }

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


    return 0;
}

TFTP文件传输

#include 
#define SER_PORT 69
#define SER_IP "192.168.125.64"
int upload(char* name)
{
	//以只读的形式打开文件
	int fd=-1;
	if((fd=open(name,O_RDONLY))==-1)
	{
		perror("open error");
		return -1;
	}
	//创建用于通信的套接字文件描述符
	int cfd = socket(AF_INET,SOCK_DGRAM,0);
	if(cfd==-1)
	{
		perror("socket error");
		return -1;
	}
	//填充服务器的地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port= htons(SER_PORT);
	sin.sin_addr.s_addr = inet_addr(SER_IP);
	socklen_t socklen = sizeof(sin);
	//向服务器发送上传请求
	short str1=0;
	short str2=0;
	char buf[516]="";
	//组装请求数据
	short *p1 = (short *)buf;
	*p1 =htons(2);
	char *p2 = buf+2;
	strcpy(p2,name);
	char *p3=p2+strlen(p2)+1;
	strcpy(p3,"octet");
	int len =4+strlen(p2)+strlen(p3);
	//向服务器发送请求
	sendto(cfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin));
	while(1)
	{
 
		bzero(buf,sizeof(buf));
		//接收ACK包
		int res1=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&socklen);
		//发送数据包
		short *p1 = (short *)buf;
		*p1 =htons(3);
		short *p2 = (short *)(buf+2);
		str1++;
		*p2=htons(str1);
		char *p3 =buf+4;
		int res=read(fd,p3,sizeof(buf)-4);
		//printf("%d\n",res);
		if(res<512)
		{
			sendto(cfd,buf,res+4,0,(struct sockaddr*)&sin,sizeof(sin));
			break;
		}
		sendto(cfd,buf,516,0,(struct sockaddr*)&sin,sizeof(sin));
		//printf("%d\n",res);
	}
	//关闭套接字
	close(cfd);
	//关闭文件指针
	close(fd);
	return 0;
 
 
}
int download(char* name)
{
	//以写的形式打开文件
	int fd=-1;
	if((fd=open(name,O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
	{
		perror("open error");
		return -1;
	}
	//创建用于通信的套接字文件描述符
	int cfd = socket(AF_INET,SOCK_DGRAM,0);
	if(cfd==-1)
	{
		perror("socket error");
		return -1;
	}
	//填充服务器的地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port= htons(SER_PORT);
	sin.sin_addr.s_addr = inet_addr(SER_IP);
	socklen_t socklen = sizeof(sin);
	//向服务器发送下载请求
	short str=0;
	char buf[516]="";
	//组装请求数据
	short *p1 = (short *)buf;
	*p1 =htons(1);
	char *p2 = buf+2;
	strcpy(p2,name);
	char *p3=p2+strlen(p2)+1;
	strcpy(p3,"octet");
	int len =4+strlen(p2)+strlen(p3);
	//向服务器发送请求
	sendto(cfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin));
	while(1)
	{
		bzero(buf,sizeof(buf));
		//向后移动指针,读取数据
		lseek(cfd,4,SEEK_SET);
		int res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&socklen);
		char *sbuf=buf+4;
		write(fd,sbuf,res-4);
	//	printf("%d\n",res);
		//判断是否拷贝完成
		if(res<516)
		{
			break;
		}
		bzero(buf,sizeof(buf));
		short *p1 = (short *)buf;
		*p1 =htons(4);
		short *p2 = (short *)(buf+2);
		str++;
		*p2=htons(str);
	//	printf("%d\n",str);
		len =4;
		//发送ACK,继续拷贝
		sendto(cfd,buf,len,0,(struct sockaddr*)&sin,sizeof(sin));
	}
	//关闭套接字
	close(cfd);
	//关闭文件指针
	close(fd);
	return 0;
 
}
 
int main(int argc, const char *argv[])
{
	//创建菜单目录
	while(1)
	{
		printf("********选择对文件功能*********\t\n");
		printf("*************1.上传*********\t\n");
		printf("*************2.下载****************\t\n");
		printf("*************3.退出****************\t\n");
		int news;
		printf("请输入你要执行的功能\n");
		scanf("%d",&news);
		getchar();
		char name[128]="";
		char *file =name;
		switch(news)
		{
		case 1:
			printf("请输入要上传的文件名\n");
			fgets(name,sizeof(name),stdin);
			name[strlen(name)-1]=0;
			upload(file);
			break;
		case 2:
			printf("请输入要下载的文件名\n");
			fgets(name,sizeof(name),stdin);
			name[strlen(name)-1]=0;
			download(file);
			break;
		case 3:
			goto END;
		default:
			printf("输入内容不在选项内,请重新输入\n");
		}
		printf("请按任意键清屏!!!\n");
		getchar();
		system("clear");
	}
END:
	return 0;
}

你可能感兴趣的:(网络,linux)