C++ - File is not Fully arrived

i want to send a file from client to server through socket . yes it sends a file but the received file in the server is not full or complete like the original one.

So the test file originally has "this is a test" in it, and the received file has "this"
yes it's only 4 letters
i tried to change the original one becomes "MyMomGoesToTheMarket" and received file has "MyMo" . still 4 letters which is not what i expect.


Here is the client :

#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;



SOCKET clientsock;
WSADATA winsock;
sockaddr_in serverAddr , addr;
int Addrlen = sizeof(serverAddr);
FILE *File;
unsigned long Size;


void startClient() {


WSAStartup(MAKEWORD(2,2), &winsock);

if(LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2 ){

WSACleanup();

}


clientsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(6091);

connect(clientsock,(sockaddr*)&addr,sizeof(addr));

printf("socket connected... \n");


}


void sending() {

//preparing the file
ifstream myfile;
myfile.open("B:\RelativeLayout.txt",ios::in | ios::binary | ios::ate);

if(myfile.is_open()) {

printf("File open OK ! \n ");


}else {

printf("File not open ! \n ", WSAGetLastError());

}

//preparing the file size

long Size ;
myfile.seekg(0,fstream::end);
Size = myfile.tellg();
myfile.close();

printf("File Size : %d bytes.\n ",Size);

char cisi[10];
sprintf(cisi, "%i",Size);
send(clientsock,cisi,10,0); // file size sent

//sending the file

char *rbuffer;

myfile.open("B:\RelativeLayout.txt",ios::in | ios::binary | ios::ate);

if(myfile.is_open()) {

myfile.seekg(0, ios::beg);

rbuffer = new char[Size];
myfile.read(rbuffer, Size);

//send(clientsock, rbuffer, Size, 0);

int j = send(clientsock, rbuffer, Size, NULL); //send to server
if (j == -1){
cout << "Error sending file to server :(" << endl;
}else {
cout << " sending file to server succeed" << endl;
}



myfile.close();

}


}





int _tmain(int argc, _TCHAR* argv[])
{

startClient();
sending();

system("PAUSE");
return 0;
}


and here is the server code :


#include "stdafx.h"
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <fstream>

using namespace std;

SOCKET servsocket, ClientAcc;
WSAData winsock;
sockaddr_in addr,incomingAddress;
int addrlen = sizeof(sockaddr_in);
int addresslen = sizeof(incomingAddress);
char *Filesize = new char[1024];
long Size;




void start() {

//socket initialization
WSAStartup(MAKEWORD(2,2), &winsock);

//socket check

if(LOBYTE(winsock.wVersion) !=2 || HIBYTE(winsock.wVersion) != 2 ) {

WSACleanup();
}


servsocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_port = htons(6091);
bind(servsocket, (sockaddr*)&addr, sizeof(addr));

listen(servsocket, 5);

ClientAcc = accept(servsocket, (sockaddr*)&incomingAddress, &addresslen);

char *ClientIP = inet_ntoa(incomingAddress.sin_addr);
int ClientPort = ntohs(incomingAddress.sin_port);
printf("Client Connected ... \n");
printf("IP : %s:%d\n", ClientIP, ClientPort);


}


void receiving() {



//receive the file size

recv(ClientAcc,Filesize,1024,0);
Size = atoi((const char*)Filesize);
printf("File size : %d\n",Size);

//receive the file

char *rbuffer;
rbuffer = new char[Size];
int k = recv(ClientAcc, rbuffer, sizeof(rbuffer), NULL);
if (k < 0){
cout << "Error uploading file" << endl;
}else {


fstream file;
file.open("B:\FileReceived.txt", ios::out|ios::binary| ios::ate);
file.write(rbuffer, sizeof(rbuffer));
file.close();
cout << "File received!" << endl;



}






}


int _tmain(int argc, _TCHAR* argv[])
{

start();
receiving();
system("PAUSE");
return 0;
}


anyone know how to solve this problem and the solution ?
i hope you can fix it . thank you
Please use code tags to make your code easier to read and respond to.

This is actually a little above my skill but I'm going to guess at something I'm not sure about and you can test to see if it is right or wrong.

I don't understand is how the server is supposed to know what the file/buffer size should be ? Perhaps that is the code to write the file after it's received.

I'm guessing it's a problem with your buffers. What I'm not sure about is the space in the name "new char" below. Should there be a space ?

1
2
char *rbuffer;
rbuffer = new char[Size];


My advice is to put in some print/cout statements and display on the screen all the things you are assuming are working but maybe haven't tested.

Directions for code tags > http://www.cplusplus.com/articles/jEywvCM9/
thanks for the reply ..
yes i change char *rbuffer to char rbuffer[256] ;
it works to send the file.

but if the bytes of the file that i send from client to server is less than 256 , the received file contains some junks to fit in 256 bytes.

any ideas ?

once again , thank you ^^
Last edited on
Topic archived. No new replies allowed.