c++windowssocketssendrecv

Send Picture by socket (send func) in c++ , but do not recive complete(Windows)!


I am sending data from client to server,But picture do not receive complete.

Client Code:

FILE *fr = fopen(tmppicsend, "rb");
char* buffer;
buffer = (char*) malloc(sizeof(char)*size);
fread(buffer, size, 1, fr);
send_len_pic = send( m_socket_pic, buffer, size, 0 );
recv( m_socket_pic, rec_end_check, 32, 0 );
fclose(fr);
free(buffer);

Server Code:

FILE *fw = fopen(fname, "wb");
char* buffer;
buffer = (char*) malloc(sizeof(char)*size);
int rec_len = recv( current_client, buffer, size, 0 );
buffer[size]='\0';
fwrite(buffer, size, 1, fw);
size -= size;
free(buffer);
fclose(fw);

Config Socket:

WSADATA wsaData_pic;
SOCKET m_socket_pic;
SOCKET m_backup_pic;
sockaddr_in con_pic;

 // Initialize Winsock.
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData_pic );
if ( iResult != NO_ERROR ){
    //printf("Error at WSAStartup()\n");
}
// Create a socket.
m_socket_pic = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket_pic == INVALID_SOCKET ) {
    //printf( "Error at socket(): %ld\n", WSAGetLastError() );
    //WSACleanup();
}

m_backup_pic = m_socket_pic;

// Connect to a server.
con_pic.sin_family = AF_INET;
con_pic.sin_addr.s_addr = inet_addr( ip );
con_pic.sin_port = htons( 2200 );

if ( connect( m_backup_pic, (SOCKADDR*) &con_pic, sizeof(con_pic) ) == SOCKET_ERROR) {
    //printf( "Failed to connect.\n" );
    //WSACleanup();
}else
{   
    m_socket_pic=m_backup_pic;
}


}

When I'm comparing this pictures, i see error in sequence number packet(packets do not receive regularly ). Picture(Pic from Client and Server) were the same size. I want to send JPEG picture.

Please Help me, tanks.


Solution

  • I got many attempts to answer.

    The best answer for this question is:

    Sync client with server for send stream.

    because the recv() function i slower than send() function, clients send amount of data that the server is able to receive.


    Clients Code:

    char* bufferCMP;
    bufferCMP = (char*)malloc(sizeof(char) * size);
    p_file = fopen(tmppicsend,"rb");
    fread(bufferCMP, 1, size , p_file);
    fclose(p_file);
    
    
    int chunkcount = size / DEFAULT_BUFLEN;
    int lastchunksize = size - (chunkcount * DEFAULT_BUFLEN);
    int fileoffset=0;
    
    //Sending Actual Chunks
    while (chunkcount > 0)
    {
        iResult = send( m_socket_pic, bufferCMP + (fileoffset * DEFAULT_BUFLEN) , DEFAULT_BUFLEN , 0 );
        fileoffset++;
        chunkcount--;
    
        if (iResult != DEFAULT_BUFLEN)
        {
            //printf("Sending Buffer size <> Default buffer length  ::: %d\n",WSAGetLastError());
        }
        else
        {
            //printf("Sending Buffer size = %d \n", iResult);
        }
    }
    
    //Sending last Chunk
    iResult = send( m_socket_pic, bufferCMP + (fileoffset * DEFAULT_BUFLEN) , lastchunksize , 0 );
    

    Server Code:

    int FileCounter=0;
    bool flg=true;
    char * fileComplete;
    char * filesizeBuffer;
    
    FILE *temp;
    
    int iResult;
    
    int receiveBuffer = 0;
    int desiredRecBuffer = size ;
    //int desiredRecBuffer = DEFAULT_BUFLEN ; 
    fileComplete = (char*) malloc (sizeof(char)* size );
    while (desiredRecBuffer > 0)
    {
        iResult = recv( current_client, fileComplete + receiveBuffer , desiredRecBuffer , 0 );
        //iResult = recv( ClientSocket, fileComplete + receiveBuffer , fileSize , 0 );
    
        if (iResult < 1)
        {
            //printf("Reveive Buffer Error  %d \n", WSAGetLastError());
        }
        else
        {
            receiveBuffer += iResult;
            desiredRecBuffer = size - receiveBuffer ;
            //printf("Reveived Data size :  %d \n", iResult);
        }
    }
    
    FILE *File = fopen(fname, "wb");
    fwrite(fileComplete,1, size , File);
    //flg = true;
    free(fileComplete);
    fclose(File);