Asuka_S 2017-08-04 04:02 采纳率: 100%
浏览 2102
已采纳

C++ 实现TCP文件传输时出现问题

我最近用C++简单的实现了一下TCP传输文件的实例
前期测试单向传输时都没有什么问题,但是目前测试双向传输时发现存在程序假死的问题,查错了几天但也没有发现什么问题。

实现的具体过程是两部分:1.服务器端先从客户端收一个文件并且保存在本地2.服务器端再发送一个文件给客户端并且客户端将这个文件保存在本地。

运行过程中,在1->2的过程中间,程序会卡死。客户端这边显示已经发送完毕,服务器端显示有接收到数据,但是会卡在最后一个数据流接收完成的位置,不会继续往下执行语句,也无法显示文件接收完毕的提示。

我用VS尝试调试了一下,发现客户端这边是正常的,但是服务器那边在接收到最后一个文件流以后程序会直接跑飞,不知道运行到哪一条语句了。
我也尝试把1,2两个部分单独拿出来,让服务器端和客户端单独进行收发,就没有出现什么问题。

还想请问各位大牛这是出现什么问题了

代码本来是在linux上出现问题的,为了方便调试我就大致修改了一下搬到windows上,但是问题还是一样出现。
代码如下:
服务器:
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<iostream>  
#include<sys/types.h>   
#include<winsock2.h>  
#include <errno.h>
#pragma comment(lib, "ws2_32.lib")
#define PORT 22222
#pragma warning(disable:4996)
#define maxline 1024    
using namespace std;
int main(int argc, char **argv)
{
    SOCKET ser_sockfd, cli_sockfd;
    sockaddr_in ser_addr;
    sockaddr_in cli_addr;
    FILE *fp;
    FILE *result;
    char buf[maxline];
    int recv_len, write_len, read_len, send_len;
    WSADATA wsaData;
    int iRet = 0;
    iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iRet != 0)
    {
        cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;;
        return -1;
    }
    if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
    {
        WSACleanup();
        cout << "WSADATA version is not correct!" << endl;
        return -1;
    }
    ser_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (ser_sockfd == INVALID_SOCKET)
    {
        printf("socket error:%s\n", strerror(errno));
        return -1;
    }

    ser_addr.sin_family = AF_INET;
    ser_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    ser_addr.sin_port = htons(PORT);
    iRet = bind(ser_sockfd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    if (iRet == SOCKET_ERROR)
    {
        printf("bind error:%s\n", strerror(errno));
        return -1;
    }


    while (1)
    {
        if ((fp = fopen("1.wav", "wb")) == NULL)
        {
            printf("open file failed\n");
            exit(0);
        }

        iRet = listen(ser_sockfd, 5);
        if (iRet == SOCKET_ERROR)
        {
            printf("listen error\n");
            return -1;
        }

        printf("listen the port:\n");

        int addlen = sizeof(sockaddr);
        cli_sockfd = accept(ser_sockfd, (struct sockaddr *)&cli_addr, &addlen);
        if (cli_sockfd == INVALID_SOCKET)
        {
            printf("accept error\n");
        }

        memset(buf,'\0',maxline);
        printf("waiting for client...\n");
        while ((recv_len = recv(cli_sockfd, buf, maxline, 0))>0)
        {
            if (recv_len==-1)
            {
                printf("recv error\n");
                break;
            }
            printf("#");
            write_len = fwrite(buf, sizeof(char), recv_len, fp);
            if (write_len < recv_len)
            {
                printf("Write file failed\n");
                break;
            }
            memset(buf,'\0', maxline);
        }
        printf("\nfinsih recieve\n");
        printf("decode finish\n");
        if ((result = fopen("result.txt", "rb")) == NULL)
        {
            printf("Open result file failed\n");
            exit(0);
        }
        memset(buf,'\0', maxline);
        while ((read_len = fread(buf, sizeof(char), maxline, result))>0)
        {
            printf("#");
            send_len = send(cli_sockfd, buf, read_len, 0);
            if (send_len==-1)
            {
                printf("send failed\n");
                exit(0);
            }
            memset(buf,'\0', maxline);
        }
        fclose(result);

        fclose(fp);
        printf("\nresult send finfish\n");
        closesocket(cli_sockfd);
    }

    closesocket(ser_sockfd);

    return 0;
}

客户端:

#include<stdio.h> 
#include<stdlib.h>  
#include<cstring>  
#include<iostream>  
#include<Ws2tcpip.h>
#include<sys/types.h>  
#include<winsock2.h>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")  
#define PORT 22222
#define maxline 1024 
using namespace std;
int main(int argc, char **argv)
{
    //加载套接字库  
    WSADATA wsaData;
    int iRet = 0;
    iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iRet != 0)
    {
        cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;
        return -1;
    }
    if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
    {
        WSACleanup();
        cout << "WSADATA version is not correct!" << endl;
        return -1;
    }
    SOCKET sockfd;
    sockaddr_in addr_ser;
    int send_len, read_len, recv_len, write_len;
    char buf[maxline];
    FILE *fp;
    FILE *result;
    memset(buf, '\0', maxline);
    if ((fp=fopen( "1.wav ","rb")) == NULL)
    {
        printf("Open file failed\n");
        exit(0);
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == INVALID_SOCKET)
    {
        printf("socket error\n");
        return -1;
    }

    addr_ser.sin_family = AF_INET;
    inet_pton(AF_INET,"192.168.20.113", (void *)&addr_ser.sin_addr.s_addr);
    addr_ser.sin_port = htons(PORT);
    iRet = connect(sockfd, (sockaddr *)&addr_ser, sizeof(addr_ser));
    if (iRet != 0)
    {
        printf("connect error\n");
        return -1;
    }

    printf("connect with server...\n");


    memset(buf,'\0', maxline);
    while ((read_len = fread(buf, sizeof(char), maxline, fp))>0)
    {
        send_len = send(sockfd, buf, read_len, 0);
        if (send_len==-1)
        {
            printf("send failed\n");
            exit(0);
        }
        memset(buf,'\0', maxline);
    }
    printf("send finish\n");
    memset(buf,'\0', maxline);
    printf("waiting for server...\n");

    if ((result = fopen("result.txt", "wb")) == NULL)
    {
        printf("Open file failed\n");
        exit(0);
    }
    while ((recv_len = recv(sockfd, buf, maxline, 0))>0)
    {
        if (recv_len==-1)
        {
            printf("recv error\n");
            break;
        }
        printf("#");
        write_len = fwrite(buf, sizeof(char), recv_len, result);
        if (write_len < recv_len)
        {
            printf("Write file failed\n");
            break;
        }
        memset(buf,'\0', maxline);
    }
    printf("\nfinsih recieve\n");
    fclose(result);
    closesocket(sockfd);
    fclose(fp);
    return 0;
}


  • 写回答

3条回答 默认 最新

  • shen_wei 2017-08-04 07:24
    关注
        ser_sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (ser_sockfd == INVALID_SOCKET)
        {
            printf("socket error:%s\n", strerror(errno));
            return -1;
        }
        int nNetTimeout=100;//1秒,   // 在这里设置超时就OK了。。。
        setsockopt(ser_sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&nNetTimeout, sizeof(int));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error