I want to send a file from a Linux server to a Windows client through sockets, the problem is that I receive more bytes than I send.
Server code----------------------------------------------
if (resultEnviarLongitud = send(ClientSocket,GotFileSize.c_str(),1024,0)<0){
cout<<endl<<"Error mandando la longitud! "<<endl;
}
rewind(fs);
while ((len = fread(Buffer,1,1024, fs)) > 0)
{
if((resultEnviar = send(ClientSocket,Buffer,1024,0)) < 0){
printf("ERROR: Failed to send file %s.\n", nombreArchivoADescargar.c_str());
break;
}
sumEnviada+=len;
}
send(ClientSocket,"Hi",sizeof(Buffer),0);
cout<<"Bytes enviados: "<<sumEnviada<<endl;
strcpy(data, "");
cout<<endl<<"ARCHIVO MANDADO EXITOSAMENTE!"<<endl;
rutaArchivoADescargar.clear();
Client code-----------------------------------------
if (resultRecibirLongitud = recv(sock, Buffer, sizeof(Buffer), 0) > 0)
{
LongitudArchivo = atoi(Buffer);
cout<<endl<<"Longitud Archivo a Recibir: " <<LongitudArchivo<<endl;
}
FILE *fp=fopen("imagen.jpg","wb");
if (fp==NULL){
cout<<"Error al crear archivo."<<endl;
}else{
bzero(Buffer2, 1024);
int fr_block_sz = 0;
int contador=0;
//shutdown(sock, SD_SEND); I HAVE TO USE IT?
while((fr_block_sz = recv(sock, Buffer2, 1024, 0)) >= 0)
{
if (fr_block_sz == 0) break;
if ( strcmp (Buffer,"Hi") == 0) break;
int write_sz = fwrite(Buffer2, 1, 1024, fp);
if(write_sz < fr_block_sz)
{
printf("File write failed on server.\n");
}
bzero(Buffer2, 1024);
contador+=fr_block_sz;
if (contador >= LongitudArchivo)break;
bzero(Buffer2, 1024);
}
cout<<endl<<"Numero de bytes recibidos: "<<contador<<endl<<endl;
if(fr_block_sz < 0)
{
printf("Error receiving file from client to server.\n");
}
printf("Ok received from client!\n");
fclose(fp);
}
Thanks,
while ((len = fread(Buffer,1,1024, fs)) > 0)
{
if((resultEnviar = send(ClientSocket,Buffer,1024,0)) < 0)
One of your issues is that you always send 1024 bytes of the buffer even if you fread fewer bytes. (Note that 1348656 rounded up to the nearest multiple of 1024 is 1349632.)
So, on the write side you want something like:
while ((len = fread(Buffer,1,1024, fs)) > 0)
{
if((resultEnviar = send(ClientSocket,Buffer,len,0)) < 0)
and on the read side you want something like:
while((fr_block_sz = recv(sock, Buffer2, 1024, 0)) >= 0)
{
// ...
int write_sz = fwrite(Buffer2, 1, fr_block_sz, fp);
Your initial send is also problematic as you always send 1024 bytes with no check that this is the actual length of what is returned by c_str.
Related
I have established a connection between a client and a server in c using poll() on a single fd. I want for the client to receive a message when the server has something to send and vice versa.
As far as I know, poll() listens for events on file desctriptors. I am not clear though as to how these events (or revents) are triggered, to determine when it is time to send or receive on the fd.
I have tried using read and write (or send and recv for that matter) in a loop for the client side but they block, so I switched to poll() for the client side too.
On the client side, I am never getting to the }else if (fds[0].revents & POLLOUT) part, meaning that the socket is never available to write to.
//Create - bind - listen to a socket named listeningSocket
struct pollfd fds[1];
fds[0].fd = listeningSocket;
fds[0].events = POLLIN | POLLPRI;
if (poll(fds, 1, 3000)) {
(client_sock = accept(listeningSocket, &client, (socklen_t *) &c));
spdlog::info("Connection accepted");
std::thread thread(&ConnectionHandler::Handle, std::ref(requestHandler), client_sock);
thread.detach();
}
The client block
while (true) {
if ((rv = poll(fds, 1, 100) > 0)) {
if (fds[0].revents & POLLIN ){
recv(sockfd, buff, 200, 0);
printf("From Server : %s", buff);
bzero(buff, sizeof(buff));
}
}else if (fds[0].revents & POLLOUT){
puts(buff);
strcpy(buff, "HELLO WORLD");
write(sockfd, buff, sizeof(buff));
break;
}
}
On connect, the server sends a welcoming message with write() to the client. The client recv()s this but then never gets its turn to right back to the server.
Am I missing something? Shouldn't the socket be ready to write to when there are no lingering data to be received on it?
Lets reformat your while loop to make it easier to read:
while (true)
{
if ((rv = poll(fds, 1, 100) > 0))
{
if (fds[0].revents & POLLIN)
{
recv(sockfd, buff, 200, 0);
printf("From Server : %s", buff);
bzero(buff, sizeof(buff));
}
}
else if (fds[0].revents & POLLOUT)
{
puts(buff);
strcpy(buff, "HELLO WORLD");
write(sockfd, buff, sizeof(buff));
break;
}
}
It should now be apparent why your POLLOUT block is never executed - that if statement is only reached when poll() returns <= 0, which is not what you want.
You need this kind of logic instead:
while (true)
{
if ((rv = poll(fds, 1, 100) > 0))
{
if (fds[0].revents & POLLIN )
{
recv(sockfd, buff, 200, 0);
printf("From Server : %s", buff);
bzero(buff, sizeof(buff));
}
else if (fds[0].revents & POLLOUT)
{
puts(buff);
strcpy(buff, "HELLO WORLD");
write(sockfd, buff, sizeof(buff));
break;
}
}
}
Which, when reformatted back to your original coding style, would look like this:
while (true) {
if ((rv = poll(fds, 1, 100) > 0)) {
if (fds[0].revents & POLLIN ){
recv(sockfd, buff, 200, 0);
printf("From Server : %s", buff);
bzero(buff, sizeof(buff));
} else if (fds[0].revents & POLLOUT){
puts(buff);
strcpy(buff, "HELLO WORLD");
write(sockfd, buff, sizeof(buff));
break;
}
}
}
See the difference good formatting makes?
That being said, know that a socket enters a writable state as soon as it connects. So your code is likely to send the client's greeting before waiting for the server's greeting. If you need to wait for the server's greeting before replying, you have to actually read the greeting first, eg:
while (true)
{
if ((rv = poll(fds, 1, 100) > 0))
{
if (fds[0].revents & POLLIN)
{
rv = recv(sockfd, buff, 200, 0);
if (rv <= 0) break;
printf("From Server : %.*s", rv, buff);
if (/* the complete greeting has been read */) // MAY take multiple reads!
{
strcpy(buff, "HELLO WORLD");
write(sockfd, buff, strlen(buff));
break;
}
}
}
}
Note that it is not necessary to handle POLLOUT when using blocking sockets. But if you are using non-blocking sockets instead, you are responsible for checking if write() fails with an EWOULDBLOCK error, and if so then buffer the data you tried to send that failed, as well as any subsequent data, until POLLOUT is reported, then you can send the buffered data (continuing to handle EWOULDBLOCK and POLLOUT) until the buffer is empty. Only then can you send new data over the socket without handling POLLOUT again until a new EWOULDBLOCK error is reported. For example:
int sendData(int fd, void *data, int len)
{
char *pdata = (char *) buff;
if (/* fd's buffer is empty */)
{
while (len > 0)
{
int rv = send(fd, pdata, len, 0);
if (rv < 0)
{
if (errno != EWOULDBLOCK)
return rv;
break;
}
pdata += rv;
len -= rv;
}
}
if (len > 0)
{
// add pdata up to len bytes to fd's buffer...
}
return 0;
}
...
while (true)
{
if ((rv = poll(fds, 1, 100) > 0))
{
if (fds[0].revents & POLLIN)
{
rv = recv(sockfd, buff, 200, 0);
if (rv <= 0) break;
printf("From Server : %.*s", rv, buff);
if (/* the complete greeting has been read */) // MAY take multiple reads!
{
strcpy(buff, "HELLO WORLD");
sendData(sockfd, buff, strlen(buff));
}
}
if (fds[0].revents & POLLOUT)
{
char *pdata = ...; // fd's buffer data
int len = ...; // fd's buffer length
int sent = 0;
while (len > 0)
{
rv = send(fd, pdata, len, 0);
if (rv < 0)
break;
pdata += rv;
len -= rv;
sent += rv;
}
if (sent > 0)
{
// remove sent bytes from fd's buffer...
}
}
}
}
Then you can use sendData() any time you need to send any data to fd, instead of calling write()/send() directly.
Remove the } before the else if.
Then put it after the else if-block.
I am using fread function to read file, which I am sending via TCP. I found out, that fread doesn't read the whole file, if the file is binary. I tried everything what i found on the internet, but nothing helped. My code is:
#define BUFSIZE 1024
char buf[BUFSIZE];
FILE *file = fopen(soubor,"rb"); //I do a check which i won't write here
size_t bytes_loaded = 0;
while (!feof(file))
{
bytes_loaded = fread(buf,1,BUFSIZE,file);
if(bytes_loaded != BUFSIZE)
{
if(!feof(file))
{
for(int i = 0; i < 100;i++)
{
fseek(file,-strlen(buf),SEEK_CUR);
bytes_loaded = fread(buf,1,BUFSIZE,file);
if(bytes_loaded == BUFSIZE)
{
break;
}
else if(i == 99)
{
fprintf(stderr,"C could't read the file\n");
fclose(file);
close(client_socket);
return 1;
}
}
}
}
bytestx = send(client_socket, buf, BUFSIZE, 0);
if (bytestx < 0)
perror("ERROR in sendto");
bzero(buf, BUFSIZE);
bytes_loaded = 0;
}
Am I doing something wrong? For example that fread check...
Your whole fread() error handling is wrong, get rid of it (using strlen() on a binary buffer is wrong anyway).
In fact, you shouldn't be using feof() to control your loop. Simply call fread() in a loop until it returns < 1 on EOF or error (use feof() and ferror() to differentiate). And when it returns > 0, you need to pass that value to send instead of passing BUFSIZE.
Try something more like this:
#define BUFSIZE 1024
char buf[BUFSIZE], *pbuf;
FILE *file = fopen(soubor, "rb");
...
size_t bytes_loaded;
do
{
bytes_loaded = fread(buf, 1, BUFSIZE, file);
if (bytes_loaded < 1)
{
if ((!feof(file)) && ferror(file))
fprintf(stderr, "Couldn't read the file\n");
break;
}
pbuf = buf;
do
{
bytestx = send(client_socket, pbuf, bytes_loaded, 0);
if (bytestx < 0)
{
perror("ERROR in send");
break;
}
pbuf += bytestx;
bytes_loaded -= bytestx;
}
while (bytes_loaded > 0);
}
while (bytes_loaded == 0);
fclose(file);
...
If you are just shifting bytes from the file to the socket then you can just keep looping on the return value from std::fread which tells you how many bytes you read and then send exactly that many bytes to your send() command.
Something like this (untested) code:
if(FILE* fp = std::fopen(soubor, "rb"))
{
char buf[1024];
std::size_t bytesrx;
while((bytesrx = std::fread(0, 1, sizeof(buf), fp)) > 0)
{
int bytestx;
if((bytestx = send(client_socket, buf, bytesrx, 0) < 0))
{
// socket error
std::cout << "socket error: " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
if(bytesrx < 0)
{
// file error
std::cout << "file error: " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
else
{
// error opening file
}
When i send the file to the client it gets corrupted, and with a size in bytes higher.
I have a version of this server running on Windows and works perfectly,but I'm not having the same result on Linux.
The file size on disk may be the error in time to send the size in bytes to the client that runs on another platform?
fread function is being used correctly?
an expert can analyze and help find the error?
LINUX SERVER SIDE
// FUNCTION TO UPLOAD A FILE TO CLIENT WINDOWS
int Socket_Setup::FILE_UPLOAD(int iD, std::string DIR_UPLOAD)
{
char Block[1024];
long FileSize;
int BytesRead;
fp = fopen(DIR_UPLOAD.c_str(), "rb");
if (!fp)
{
errno_message.append((char*)strerror(errno));
FUNCTION_LOG(errno_message);
return 1;
}
fseek(fp, 0, SEEK_END);
FileSize = ftell(fp);
rewind(fp);
long Size_Send = htonl(FileSize);
Total = FileSize;
// Sending the file size to the Windows Client
iResult = send(client[iD].socket, (const char*)&Size_Send, sizeof(long), 0);
if (iResult <= 0)
{
errno_message.append((char*)strerror(errno));
FUNCTION_LOG(errno_message);
return 1;
}
// LOOP TO SEND FILE
while (FileSize > 0)
{
BytesRead = fread(Block, 1, sizeof(Block), fp);
if (BytesRead <= 0)
{
errno_message.append((char*)strerror(errno));
FUNCTION_LOG(errno_message);
fclose(fp);
return 1;
}
if (send(client[iD].socket, Block, BytesRead, 0) != BytesRead)
{
errno_message.append((char*)strerror(errno));
FUNCTION_LOG(errno_message);
fclose(fp);
return 1;
}
FileSize -= BytesRead;
}
fclose(fp);
return 0;
}
WINDOWS CLIENT SIDE:
int readBytes(SOCKET s, void *buffer, int buflen)
{
int total = 0;
char *pbuf = (char*)buffer;
while (buflen > 0)
{
int iResult = recv(s, pbuf, buflen, 0);
if (iResult < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
continue;
return SOCKET_ERROR;
}
else if (iResult == 0)
return 0;
else
{
pbuf += iResult;
buflen -= iResult;
total += iResult;
}
}
return total;
}
// FUNCTION TO DOWNLOAD FILE FROM SERVER
int Remote_Manip::FILE_DOWNLOAD(std::string directory, Socket_Setup &socket_setup)
{
unsigned long FileSize;
char mfcc[65535];
File = fopen(directory.c_str(), "wb");
if (File == NULL)
{
closesocket(socket_setup.ConnectSocket);
WSACleanup();
return 1;
}
// Receiving file size from server
int iResult = readBytes(socket_setup.ConnectSocket, &FileSize, sizeof(FileSize));
if (iResult <= 0)
{
fclose(File);
closesocket(socket_setup.ConnectSocket);
WSACleanup();
return 1;
}
FileSize = ntohl(FileSize);
// LOOP TO RECEIVING FILE
while (FileSize > 0)
{
int Received = recv(socket_setup.ConnectSocket, mfcc, sizeof(mfcc),0);
if (Received <= 0)
{
fclose(File);
closesocket(socket_setup.ConnectSocket);
WSACleanup();
return 1;
}
if (fwrite(mfcc, 1, Received, File) != Received)
{
fclose(File);
closesocket(socket_setup.ConnectSocket);
WSACleanup();
return 1;
}
FileSize -= Received;
}
fflush(File);
fclose(File);
return 0;
}
I will rephrase the whole question here so that it is answerable.
I am able to copy binary file perfectly in the same machine not using sockets but just making a simple copy function. Trying to implement this code for copying onto a TCP/IP connection but can't get it to work.
FILE *filehandle = fopen("imagefile.jpg", "rb");
FILE *dest =fopen("imagecopy.jpg", "wb"); // copied image file
fseek(filehandle, 0, SEEK_END);
unsigned long filesize = ftell(filehandle);
char *buffer = (char*)malloc(sizeof(char)*filesize);
rewind(filehandle);
int bytesread = fread(buffer, sizeof(char), filesize, filehandle);
for( int i=0; i<filesize; i++ )
{
fputc(buffer[i], filehandle); // copies all the contents to dest
}
The code above works perfectly for copying an image file in the computer but when implemented to copy on server, it is difficult to go about it.
I am trying to send an image file from a server to a client both which have been made manually in C. The length of the file to be sent by the server is only known to the server when it's sending the file so the buffer is dynamically generated in the server, something like this:
SERVER
fseek(filehandle, 0, SEEK_END);
long filesize = ftell(filehandle); // file could be 11000bytes
char *buffer = (char*)malloc(sizeof(char)*filesize); // char buffer with 11000 bytes to store the data from the file.
// then I call the send() function
rewind(filehandle); // go back to beginning
send(clientsocket, buffer, filesize, 0); // this is being sent perfectly, no errors because in the actual code, I am checking for errors
CLIENT
// here is where I don't understand how to dynamically allocate the 11000 bytes to store the data in a client buffer
// the filesize is not necessarily going to be 11000 so need to dynamically allocate
// I did the following:
#define BUFSIZE 10
FILE *filehandle = fopen("imagefile.jpg", "wb"); // image file created by client
char *buffer = (char*)malloc(sizeof(char)*BUFSIZE);
int bytesread = recv(buffer, 1, strlen(buffer), 0);
if( bytesread > 0 )
{
printf("Bytes read: %d\n", bytesread); // bytes read is 5
printf("Buffer: %s\n", buffer); // but buffer shows all the binary text like it normally would
// when I try to store buffer in a file, it doesn't put full buffer because only 5 characters are written
for( int i=0; i<bytesread; i++ )
{
fputc(buffer[i], filehandle); // this doesn't create full image
}
}
How can I dynamically allocate the 11000 bytes sent by the server?
You need to loop both the sending and receiving. Neither send() nor recv() are guaranteed to send/read as many bytes as you requested.
You also should send the file size before the file data so the receiver knows how many bytes to expect and when to stop reading.
Try something more like this:
SERVER
bool senddata(SOCKET sock, void *buf, int buflen)
{
unsigned char *pbuf = (unsigned char *) buf;
while (buflen > 0)
{
int num = send(sock, pbuf, buflen, 0);
if (num == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
// optional: use select() to check for timeout to fail the send
continue;
}
return false;
}
pbuf += num;
buflen -= num;
}
return true;
}
bool sendlong(SOCKET sock, long value)
{
value = htonl(value);
return senddata(sock, &value, sizeof(value));
}
bool sendfile(SOCKET sock, FILE *f)
{
fseek(f, 0, SEEK_END);
long filesize = ftell(f);
rewind(f);
if (filesize == EOF)
return false;
if (!sendlong(sock, filesize))
return false;
if (filesize > 0)
{
char buffer[1024];
do
{
size_t num = min(filesize, sizeof(buffer));
num = fread(buffer, 1, num, f);
if (num < 1)
return false;
if (!senddata(sock, buffer, num, 0))
return false;
filesize -= num;
}
while (filesize > 0);
}
return true;
}
FILE *filehandle = fopen("imagefile.jpg", "rb");
if (filehandle != NULL)
{
sendfile(clientsocket, filehandle);
fclose(filehandle);
}
CLIENT
bool readdata(SOCKET sock, void *buf, int buflen)
{
unsigned char *pbuf = (unsigned char *) buf;
while (buflen > 0)
{
int num = recv(sock, pbuf, buflen, 0);
if (num == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
// optional: use select() to check for timeout to fail the read
continue;
}
return false;
}
else if (num == 0)
return false;
pbuf += num;
buflen -= num;
}
return true;
}
bool readlong(SOCKET sock, long *value)
{
if (!readdata(sock, value, sizeof(value)))
return false;
*value = ntohl(*value);
return true;
}
bool readfile(SOCKET sock, FILE *f)
{
long filesize;
if (!readlong(sock, &filesize))
return false;
if (filesize > 0)
{
char buffer[1024];
do
{
int num = min(filesize, sizeof(buffer));
if (!readdata(sock, buffer, num))
return false;
int offset = 0;
do
{
size_t written = fwrite(&buffer[offset], 1, num-offset, f);
if (written < 1)
return false;
offset += written;
}
while (offset < num);
filesize -= num;
}
while (filesize > 0);
}
return true;
}
FILE *filehandle = fopen("imagefile.jpg", "wb");
if (filehandle != NULL)
{
bool ok = readfile(clientsocket, filehandle);
fclose(filehandle);
if (ok)
{
// use file as needed...
}
else
remove("imagefile.jpg");
}
We could avoid the header that contains the image size, but we just read to the end of the sent data. About the buffer size, we could use a fixed number such as 10 * 1024, when we received some data from the server, we just save it into a file according to the actual received data length.
// please open a file ...
FILE * fp;
// ...
const int LENGTH = 10 * 1024;
int len = 0;
char * buffer = (char *)malloc(LENGTH);
while ((len = recv(socket, buffer, LENGTH, 0)) > 0) {
fwrite(buffer, 1, len, fp);
}
free(buffer);
// close the file
#T.C: I guess we cannot allocate a buffer according to the size sent from the server in case the image is too large to save inside the client's memory. Not mention the server is fake, and intended to make any attack.
im having a segmentation fault error testing a client-server file transfer. I have this code on the downloader side (this is where i think the problem is):
else if (info_socket[fds[i].fd] == RECIBIENDO) {
close_conn = FALSE;
buffer = (char*) malloc(1500);
rc = recv(fds[i].fd, buffer, 1500, 0);
if (rc < 0) {
if (errno != EWOULDBLOCK) {
perror(" recv() failed");
close_conn = TRUE;
map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
descargas.erase(it);
}
} else if (rc == 0) {
close_conn = TRUE;
map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
descargas.erase(it);
} else {
arch = fopen((directorio + descargas[fds[i].fd]->nombre_archivo).c_str(), "ab");
printf("%s -- %d",(directorio + descargas[fds[i].fd]->nombre_archivo).c_str(),fds[i].fd);
totalEscrito = fwrite(buffer, 1, rc, arch);
descargas[fds[i].fd]->bytes_descargados = descargas[fds[i].fd]->bytes_descargados + totalEscrito;
fclose(arch);
file_descript = open((directorio + descargas[fds[i].fd]->nombre_archivo).c_str(), O_RDONLY);
file_size = get_size_by_fd(file_descript);
file_buffer = (char*) mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
MD5((unsigned char*) file_buffer, file_size, result);
mdString = (char*) malloc(33);
for (int r = 0; r < 16; r++)
sprintf(&mdString[r * 2], "%02x", (unsigned int) result[r]);
if (strcmp(mdString, (descargas[fds[i].fd]->md5).c_str()) == 0) {
close_conn = TRUE;
map<int, info_trans*>::iterator it = descargas.find(fds[i].fd);
descargas.erase(it);
}
free(mdString);
free(file_buffer);
close(file_descript);
}
The Server is on a similar loop sending the data. The strange part is that if i run this on my laptop it works, but at the school pc's virtual machine throws the Segmentation fault.
If there is a good way to debug the memory using VMware Player, please tell me too.
Thanks you, and sorry for my english level.
free(file_buffer); is wrong. I guess you meant munmap(file_buffer, file_size);.