cannot read correct binaries with tcp sockets - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

cannot read correct binaries with tcp sockets

Client sends buffer size along with contents(image file) to the server.(works fine). Server gets the size and contents and loads the image. the client program is follows and it is tested:
[code]
QString fileName = "/home/saman/Desktop/1.png";
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) return;
QByteArray blob = file.readAll();
file.close();

int size = blob.length();
clientSocket->write((char*)&size, 4);
clientSocket->write(blob.data(), size);
[/code]

The server program cannot receive the full image file. It reads only 50KB out of 140KB and then read returns 0. I also implemented the code in core C, but, I getting the same result. Here are my implementations:
[code]
int size;
clientConnection->read((char*)&size, 4);
qDebug() << size;

char* image;

qDebug() << size;

image = new char[size];

int size2 = size;
int offset = 0;

while(size2 > 0)
{
int r = clientConnection->read(image+offset, size2);
if(r == 0)
break;
size2 -= r;
offset += r;
}
[/code]

As mentioned,also tried C:

[code]
struct sockaddr_in clntaddr, servaddr; /* my address information */
struct stat statbuf;
ssize_t bytes_read;
long filesize, chunksize, offset;
int listenfd, connfd, fd,recv_bytes, fileparts;
socklen_t clntlen;
int total_parts, optval;
char *buff, filename[128];
pid_t childpid;


if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("Server-socket() sockfd error lol!");
exit(1);}
else
printf("Server-socket() sockfd is OK...
");

// set SO_REUSEADDR on a socket to true (1):
optval = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET; /* host byte order */
servaddr.sin_port = htons(SERV_PORT); /* short, network byte order */
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* automatically fill with my IP */
// memset(&(servaddr.sin_zero), '', 8); /* zero the rest of the struct */

if(bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) == -1)
{
perror("Server-bind() error lol!");
exit(1);
}
else
printf("Server-bind() is OK...
");

if(listen(listenfd, LISTENQ) == -1)
{
perror("Server-listen() error
");
exit(1);
}
else
printf("Server-listen() is OK
");

for(;;)
{
clntlen = sizeof(clntaddr);
if((connfd = accept(listenfd, (SA *) &clntaddr, &clntlen)) < 0)
{
if(errno == EINTR)
continue;
else
{
printf("server-accept() error
");
exit(1);
}
}
else
printf("server-accept() is OK..
");


/* if((childpid = fork()) == 0)
{ */ // child process

close(listenfd); // close listening socket
printf("connection established..
"); // process the request

if( (recv_bytes = recv(connfd, (void *) &chunksize, sizeof(int), 0))<= 0 )
{
if(!recv_bytes)
printf("socket is blocking and the connection to the remote node failed..
");
else
printf("Server-recv(Request) error..
");
exit(1);
}

fileparts = ntohl( chunksize );

if((fd = open("/home/saman/2.png", O_CREAT | O_WRONLY)) < 0)
{
perror("couldn't open the Server-file");
exit(1);
}

buff = (char *)malloc(sizeof(char) * (chunksize));

if((bytes_read = recv(connfd, (void *)buff, chunksize, 0)) <= 0)
{
if(bytes_read < 0)
{
perror("Client receiving chunks error..
");
exit(1);
}
}

if(write(fd, (void *) buff, bytes_read) <= 0)
{
perror("Client writing data error..
");
exit(1);
}

close(fd)

if(close(listenfd) != 0)
printf("Server-listenfd closing failed!
");
else
printf("Server-listenfd successfully closed!
");


qDebug() << chunksize;
qDebug() << buff;
}
[/code]

I am running client/server program in Linux. The strange part is that server program written by another clerk on windows could load the whole image. Although I have no access to his code, I am wondering where I am doing wrong so that this happens!
Sign In or Register to comment.