TCP丢失连接

时间:2015-04-23 19:26:37

标签: c++ sockets network-programming

我创建了一个客户端和服务器,我能够从客户端成功发送文件并在服务器上接收它。当我成功发送文件然后如果我尝试发送另一个文件我的连接失败时会出现问题。我不明白为什么会这样。任何见解都会很棒,我对套接字编程没有多少经验。

client.cpp

int sock, n;
unsigned int length;
struct sockaddr_in server, from;
struct hostent *hp;
string send_file;
cout << "Enter filename: ";
cin >> send_file;

FILE *file_read;
unsigned long fsize;

// get file size
file_read = fopen(send_file.c_str(), "rb");
if (file_read == NULL){
    cout << "file doesn't exist."<< endl;
    break;
 }
 else{
     fseek(file_read, 0, SEEK_END);
     fsize = ftell(file_read);
     rewind(file_read);
     cmd_msg.size= fsize;

 }

// receive first UDP message
n = recvfrom(sock,&cmd_rec,sizeof(cmd_rec),0,(struct sockaddr *)&server, &length); //1B
if (n < 0){
    exit(1);
 }

int tcp_port=cmd_rec.port;


int sk;
struct sockaddr_in serv_addr;
struct hostent *serverTCP;

//create TCP socket
sk = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
    exit(1);
 }

serverTCP = gethostbyname(server_host);
if (serverTCP==0){
    exit(1);
 }

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)serverTCP->h_addr, (char *)&serv_addr.sin_addr.s_addr,serverTCP->h_length);
serv_addr.sin_port = tcp_port;
if (connect(sk,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
    cout << "failed to create connection. "<<endl; // this is the error received when trying to send second file
    exit(1);
 }

while (true){
    //buffer size of 1448
    unsigned char buff[1448]={0};
    int bytes_read = fread(buff,1,1448,file_read);

    // send data with successful read
    if(bytes_read > 0){
        write(sk, buff, bytes_read);
    }

    //exit when end of file is reached
    if (bytes_read < 1448){
        if (feof(file_read)){
        }
        break;
    }
 }
fclose(file_read);

server.cpp

int sk, sk2;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;

// create TCP socket
sk = socket(AF_INET, SOCK_STREAM, 0);
if (sk < 0){
    exit(1);
 }

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = 0;                    // let system choose port
if (bind(sk, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
    exit(1);
 }

socklen_t len = sizeof(serv_addr);
if (getsockname(sk, (struct sockaddr *)&serv_addr, &len) == -1){
    exit(1);
 }

int listening = serv_addr.sin_port ;
cmd_rec.port=listening;

//send UDP message
n = sendto(sock,&cmd_rec,sizeof(cmd_rec),0,(struct sockaddr *)&from,fromlen); //1B
if (n < 0){
    exit(1);
 }

cout << "- listen @: " << listening << endl;
listen(sk,5);
clilen = sizeof(cli_addr);
sk2 = accept(sk, (struct sockaddr *) &cli_addr, &clilen);
if (sk2 < 0){
    cout << "- failed to accept connection. " << endl;
 }
 else{
     cout << "- connected with client" << endl;
 }

FILE *file_write;
string backup= "backup/";
string location= backup+cmd_msg.filename;

file_write = fopen(location.c_str(), "w");
int bytes_rec=0;
int bytes_sum=0;
char rec_buffer[1448];
//loop to write data chunks to server
while((bytes_rec = read(sk2, rec_buffer, 1448)) > 0){
    bytes_sum=bytes_rec+bytes_sum;
    cout << "- total bytes received: " << bytes_sum <<endl;
    fwrite(rec_buffer, 1,bytes_rec,file_write);

    //once end of file is reached break the loop
    if(bytes_sum ==cmd_msg.size){
        break;
    }
 }

fclose(file_write);

0 个答案:

没有答案