Questions about transferring files through socket [confusion]

Source: Internet
Author: User
For more information about socket file transfer, see Linux programming and kernel. Questions about transferring files through socket [confusion]

These days have been plagued by this problem.

Write a program for transmitting BMP images through socket

Client transfers client.bmp to the server

However, images received on the server cannot be opened.

Use UltraEdit to open the client.bmp image sent by the client and compare it with the server.bmp image received by the server,

The first 1448 bytes of the server.bmp file are the same as the first 1448 bytes of the client.bmp file.

But all the bytes after the 1,448th bytes of the server.bmp file are 0x00.

So server.bmp shows almost black images.

My question is: "The transfer value is not 0. Why does it all change to 0 after receiving a part?

Please give me some advice. I have read more about Baidu and I am still puzzled.


I attached my code
CODE: ---------- server -----------
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define SERVPORT 3333
# Define BACKLOG 10
# Define MAX_CONNECTED_NO 10
# Define MAXDATASIZE 100*100*3 + 54

Int main ()
{
Struct sockaddr_in server_sockaddr, client_sockaddr;
Int sin_size, recvbytes;
Int sockfd, client_fd;
Char buf [MAXDATASIZE];
Int nwrite, I;

FILE * fbmp;
Char BMP data [100*100*3 + 54];

If (fbmp = fopen ("server.bmp", "w +") = NULL)
{
Printf ("Open server bmp error! \ N ");
Exit (-1 );
}


If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}

Printf ("socket success !, Sockfd = % d \ n ", sockfd );

Server_sockaddr.sin_family = AF_INET;
Server_sockaddr.sin_port = htons (SERVPORT );
Server_sockaddr.sin_addr.s_addr = INADDR_ANY;

Bzero (& (server_sockaddr.sin_zero), 8 );

If (bind (sockfd, (struct sockaddr *) & server_sockaddr, sizeof (struct sockaddr) =-1 ){
Perror ("bind ");
Exit (1 );
}

Printf ("bind success! \ N ");

If (listen (sockfd, BACKLOG) =-1 ){
Perror ("listen ");
Exit (1 );
}

Printf ("listening---\ n ");

If (client_fd = accept (sockfd, (struct sockaddr *) & client_sockaddr, & sin_size) =-1 ){
Perror ("accept ");
Exit (1 );
}

If (recvbytes = recv (client_fd, BMP data, MAXDATASIZE, 0) =-1 ){
Perror ("recv ");
Exit (1 );
}


For (I = 0; I <MAXDATASIZE; I ++)
{
Printf ("% x-", BMP data [I]);
}

If (nwrite = fwrite (BMP data, MAXDATASIZE, 1, fbmp) <0)
{
Printf ("Write BMP error! \ N ");
Exit (-1 );
}

Fclose (fbmp );
Close (sockfd );

}


--------------- Client ---------------------

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define SERVPORT 3333

# Define MAXDATASIZE 100*100*3 + 54

Main (int argc, char * argv [])
{
Struct hostent * host;
Struct sockaddr_in serv_addr;
Int sockfd, sendbytes;
Char buf [MAXDATASIZE];
Int BMP size = 0,
I,
Headersize = 0,
Pixelsize = 0;

FILE * fbmp;
Char BMP data [100*100*3 + 54];

If (fbmp = fopen ("client.bmp", "r") = NULL)
{
Printf ("Open client bmp error! \ N ");
Exit (-1 );
}


Fseek (fbmp, 0, SEEK_SET );
Fread (BMP data, MAXDATASIZE, 1, fbmp );

If (argc <2 ){
Fprintf (stderr, "Please enter the server's hostname! \ N ");
Exit (1 );
}

If (host = gethostbyname (argv [1]) = NULL ){
Perror ("gethostbyname ");
Exit (1 );
}

If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}

Printf ("socket success !, Socket = % d \ n ", sockfd );

Serv_addr.sin_family = AF_INET;
Serv_addr.sin_port = htons (SERVPORT );
Serv_addr.sin_addr = * (struct in_addr *) host-> h_addr );

Bzero (& (serv_addr.sin_zero), 8 );

If (connect (sockfd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr) =-1 ){
Perror ("connect ");
Exit (1 );
}

Printf ("connect success! \ N ");


If (sendbytes = send (sockfd, BMP data, BMP size, 0) =-1 ){
Perror ("send ");
Exit (1 );
}


For (I = 0; I <MAXDATASIZE; I ++)
{
Printf ("% x-", BMP data [I]);
}

Printf ("send over! \ N ");

Fclose (fbmp );
Close (sockfd );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.