编程语言
首页 > 编程语言> > 网络编程(传图片大结局)

网络编程(传图片大结局)

作者:互联网

原理内容同上篇:网络编程 - 西北小蚂蚁 - 博客园 (cnblogs.com)但在上一篇的基础上去掉了延时的限制

有想法不去做妄称少年郎,上一篇提出了回传字符的想法,总感觉不去试一试不好,所以它来了。

一、服务器代码:

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


#include <sys/stat.h>
#include <fcntl.h>





#define N 512

struct tcp
{
    int len;
    char buf[N];
}QY = {0,"0"};

char a[] = "ok";

int main()
{
    //创建socket套接字
    int sfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == sfd)
    {
        perror("socket");
        return -1;
    }

    //设置存储IP PORT变量//PORT用来区分进程
    struct sockaddr_in sddr,cddr;
    sddr.sin_family        = AF_INET;
    sddr.sin_port        = htons(6666);//PORT用来区分进程一般来说大于5000
    sddr.sin_addr.s_addr = inet_addr("0.0.0.0");//自动分配IP//也可以自己设置
    int len = sizeof(cddr);


    //把IP PORT和套接字绑定
    if(-1 == bind(sfd,(void *)&sddr,sizeof(sddr)))
    {
        perror("bind");
        return -1;
    }

    //监听套接字
    if(-1 == listen(sfd,10))
    {
        perror("listen");
        return -1;
    }
    puts("listen...");

    //创建连接
    int nfd = accept(sfd,(void *)&cddr,&len);
    if(-1 == nfd)
    {
        perror("accept");
        return -1;
    }
    

    int fd_1 = open("LF_1.jpg",O_RDONLY);//打开文件

    while(1)
    {   
        read(nfd,&QY,sizeof(QY));
        if(0 == strncmp(QY.buf,a,2))
        {
            QY.len = read(fd_1, QY.buf,sizeof(QY.buf));//读取文件的内容
            if(0 == QY.len)        
            {
                write(nfd,&QY,sizeof(QY));
                printf("over\n");
                break;
            }
            write(nfd,&QY,sizeof(QY));
            printf("go\n");
            //usleep(5000);//延时5ms
        }
        

    }    

    close(fd_1);
    
}

 

二、客户端代码:

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


#include <sys/stat.h>
#include <fcntl.h>

#define N 512



struct tcp
{
    int len;
    char buf[N];
}QY = {0,"ok"};

char a[] = "ok";


int main(int argc,char *argv[])
{
    if(3 > argc)
    {
        printf("argv: %s <IP> <PORT>\n",argv[0]);
        return -1;
    }

    int cfd = socket(AF_INET,SOCK_STREAM,0);
    if(-1 == cfd)
    {
        perror("socket");
        return -1;
    }

    //设置服务器的IP PORT
    struct sockaddr_in sddr;
    sddr.sin_family        = AF_INET;
    sddr.sin_port        = htons(atoi(argv[2]));
    sddr.sin_addr.s_addr= inet_addr(argv[1]);

    //连接服务器
    if(-1 == connect(cfd,(void *)&sddr,sizeof(sddr)))
    {
        perror("connect");
        return -1;
    }

    int fd_2 = open("LF_2.jpg",O_CREAT|O_WRONLY, 0777);//打开文件
 
    

    write(cfd,&QY,sizeof(QY));
    while(1)
    {
        read(cfd,&QY,sizeof(QY));
        printf("go\n");
        if(0 == QY.len)
        {
            printf("over\n");
            break;
        }
        write(fd_2, QY.buf, QY.len);
        strcpy(QY.buf,a);
        write(cfd,&QY,sizeof(QY));
        
        
        
    }
    close(fd_2);

}

 

标签:QY,编程,len,sddr,大结局,int,sizeof,include,图片
来源: https://www.cnblogs.com/JinShanCheShen/p/15036580.html