• 基于半关闭的文件传输程序


    这里给出了windows版本的代码,首先要导入ws2_32.lib,我的路径在C:WindowsSystem32,在codeblocks-setting-compiler-linker settings添加。

    服务端(首先创建好1.txt)

    #include <stdio.h>
    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
    #include <winsock2.h>
    #include <iostream>
    
    using namespace std;
    const int N = 1024;
    
    int main(int argc, char **argv)
    {
        WSADATA wsadata;
        SOCKET ser_sock, cli_sock;
        if(WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
            puts("ser_0 error");
    
        FILE *fp = fopen("1.txt", "rb");
        if(!fp) puts("file error");
    
        ser_sock = socket(PF_INET, SOCK_STREAM, 0);
        if(ser_sock == INVALID_SOCKET) puts("ser_1 error");
    
        SOCKADDR_IN ser_addr, cli_addr;
        memset(&ser_addr, 0, sizeof(ser_addr));
        ser_addr.sin_family = AF_INET;
        ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        ser_addr.sin_port = htons(atoi(argv[1]));
        if(bind(ser_sock, (SOCKADDR *)&ser_addr, sizeof(ser_addr)) != 0) puts("ser_2 error");
    
        if(listen(ser_sock, 5) != 0) puts("ser_3 error");
    
        int len = sizeof(cli_addr);
        cli_sock = accept(ser_sock, (SOCKADDR *)&cli_addr, &len);
        if(cli_sock == INVALID_SOCKET) puts("ser_4 error");
    
        char s[100];
        len = fread(s, 1, 50, fp);//从文件流中读
        send(cli_sock, s, len, 0);
        shutdown(cli_sock, SD_SEND);
        len = recv(cli_sock, s, 50, 0);
        s[len] = 0;
        puts(s);
    
        closesocket(cli_sock);
        closesocket(ser_sock);
        fclose(fp);
        WSACleanup();
        return 0;
    }

    客户端

    #include <stdio.h>
    #include <cstdlib>  //atoi
    #include <cstring>
    #include <unistd.h> //write,read,close
    #include <winsock2.h>
    #include <iostream>
    
    using namespace std;
    const int N = 1024;
    
    int main(int argc, char **argv)
    {
        WSADATA wsadata;
        SOCKET sock;
        if(WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
            puts("cli_0 error");
    
        FILE *fp = fopen("2.txt", "wb");
        if(!fp) puts("file error");
    
        sock = socket(PF_INET, SOCK_STREAM, 0);
        if(sock == INVALID_SOCKET) puts("cli_1 error");
    
        SOCKADDR_IN ser_addr;
        memset(&ser_addr, 0, sizeof(ser_addr));
        ser_addr.sin_family = AF_INET;
        ser_addr.sin_addr.s_addr = inet_addr(argv[1]);
        ser_addr.sin_port = htons(atoi(argv[2]));
        if(connect(sock, (SOCKADDR *)&ser_addr, sizeof(ser_addr)) != 0) puts("cli_2 error");
    
        char s[100];
        int len = recv(sock, s, 50, 0);
        fwrite(s, 1, len, fp); //写入文件流
        send(sock, "Thank you!", 10, 0);
        cout<<"文件读取成功"<<endl;
    
        closesocket(sock);
        fclose(fp);
        WSACleanup();
        return 0;
    }

    运行截图:

  • 相关阅读:
    Delphi Stream(流)介绍
    springboot 启动慢分析
    内网穿透NPS
    Docker非root用户使用
    springBoot项目中的log导入,ELK
    基于Addrparser根据经纬度分析所在地区位置
    java ArrayList条件排序
    未完成事项记录
    Python数据挖掘——银行分控模型的建立
    unity ugui的拖拽与放置
  • 原文地址:https://www.cnblogs.com/lesroad/p/9530067.html
Copyright © 2020-2023  润新知