Somehow the received data size will change suddenly, after will lead to errors, and is not on a regular basis! Sometimes can complete transfer!
Code:
Linux server (recv)
# include & lt; Unistd. H>
#include
#include
#include
#include
#include
#include
# define SERVER_PORT 6000
# define LENGTH_OF_LISTEN_QUEUE 20
# define BUFFER_SIZE 10
Struct Data//packet
{
Int length;
Char receivemessage [2000].//content information
Int fin;
};
Int main ()//(int arg c, char * argv [])
{
Struct sockaddr_in server_addr;
Int server_socket;
Int opt=1;
Bzero (& amp; Server_addr, sizeof (server_addr));
Server_addr. Sin_family=AF_INET;
Server_addr. Sin_addr. S_addr=htons (INADDR_ANY);
Server_addr. Sin_port=htons (SERVER_PORT);
//create a Socket
Server_socket=socket (AF_INET SOCK_STREAM, 0).
If (server_socket & lt; 0)
{
Printf (" Create Socket Failed! \n");
exit(1);
}
//to bind a socket
The setsockopt (server_socket, SOL_SOCKET, SO_REUSEADDR, & amp; Opt, sizeof (opt));
If (bind (server_socket, (struct sockaddr *) & amp; Server_addr, sizeof (server_addr)))
{
Printf (" Server Bind Port: % d Failed! \ n ", SERVER_PORT);
exit(1);
}
//listening Socket
If (listen (server_socket, 5))
{
Printf (" Server Listen Failed! \n");
exit(1);
}
Struct sockaddr_in client_addr;
Int client_socket;
Socklen_t length;
//connect the client Socket
Length=sizeof (client_addr);
Client_socket=accept (server_socket, (struct sockaddr *) & amp; Client_addr, & amp; Length);
If (client_socket & lt; 0)
{
Printf (" Server Accept Failed! \n");
}
The FILE * fp.
if (! (fp=fopen (" ABC. BMP, wb + "")))
{
Printf (" open ABC. BMP error ");
exit(0);
}
The Data Data;
Memset (& amp; The data, 0, sizeof (data));
//from the client receiving data
while(! Data. A fin)
{
Memset (data. The receivemessage, 0, sizeof (data. Receivemessage));
Length=recv (client_socket, (char *) & amp; Data, sizeof (data), 0);
Printf (" length fin size % d % d % d \ n ", length, (int) data. Fin, (int) data. Length);
If (length & lt; 0)
{
Printf (" Server Recieve Data Failed! \n");
break;
}
Fwrite (data. Receivemessage, data length, 1, fp);
}
Close (client_socket);
Close (server_socket);
return 0;
}
Windows client: (send)
# include & lt; stdio.h>
#include
# pragma comment (lib, "ws2_32. Lib")
# define Port 6000
# define IP_ADDRESS "10.14.105.88
"
Typedef struct Data
{
Int length;
Char sendMessage [2000].
Int fin;
};
Int main (int arg c, char * argv [])
{
The Data Data;
WSADATA s;
The SOCKET ClientSocket;
Struct sockaddr_in ClientAddr;
int ret=0;
Char SendBuffer [MAX_PATH];
If (WSAStartup (MAKEWORD (2, 2), & amp; S)!=0)
{
Printf (" Init Windows Socket Failed! Error: % d \ n ", GetLastError ());
Getchar ();
return -1;
}
ClientSocket=socket (AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
If (ClientSocket==INVALID_SOCKET)
{
Printf (" Create Socket Failed! Error: % d \ n ", GetLastError ());
Getchar ();
return -1;
}
ClientAddr. Sin_family=AF_INET;
ClientAddr. Sin_addr. S_addr=inet_addr (IP_ADDRESS);
ClientAddr. Sin_port=htons (Port);
Memset (ClientAddr sin_zero, 0 x00, 8).
The FILE * fp.
Errno_t er=fopen_s (& amp; Fp, "./40000. BMP ", "rb +");
Fseek (fp, 0, SEEK_END);
Int the end=ftell (fp);
The fseek (fp, 0, 0);
//connection Socket
Ret=connect (ClientSocket, (struct sockaddr *) & amp; ClientAddr, sizeof (ClientAddr));
If (ret==SOCKET_ERROR)
{
Printf (" Socket Connect Failed! Error: % d \ n ", GetLastError ());
Getchar ();
return -1;
}
The else
{
Printf (" Socket Connect Succeed!" );
}
While (the end & gt; 0)
{
Printf (" end % d \ n ", end);
Memset (data. The sendMessage, 0, sizeof (data. SendMessage));
Fread (data. SendMessage, 1024, 1, fp);
If (end & gt;
=1024){
Data. The fin=0;
Data. The length=1024;
}
The else
{
Data. The fin=1;
Data. The length=end;
}
Ret=send (ClientSocket, (char *) & amp; Data, sizeof (data), 0);
If (ret==SOCKET_ERROR)
{
Printf (" end % d \ n ", end);
Printf (" send error: % d \ n ", WSAGetLastError ());
//printf (" the send () failed! \n");
break;
}
The else
{
End -=1024;
}
}
//close the socket
Closesocket (ClientSocket);
WSACleanup ();
Getchar (); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull