Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Image shifted after transfer  (Read 1388 times)

0 Members and 1 Guest are viewing this topic.

Chili

  • Guest
[SOLVED]Image shifted after transfer
« on: June 05, 2013, 03:23:18 pm »
After sending an image through the network and reconstructing it with the create command everything gets shifted quite a few pixels to the right. So the obvious question is, what is causing this behaviour?

Server side:
http://imgur.com/aVMI2Sr.png

Client side:
http://imgur.com/CVjhuJy.png

Server side send code:
data_block.data_value_1 = size_;
data_block.data_value_2 = image.getSize().x;
data_block.data_value_3 = image.getSize().y;
send(sockets_[num_clients_], (char*)&data_block, DEFAULT_BUFLEN, 0);

int temp_count = 0, wait_for_send;
char temp_arr[1000];
unsigned char * img_arr = (unsigned char *)image.getPixelsPtr();
memcpy(temp_arr, img_arr+temp_count, 1000);
while(temp_count < size_){
        // Send image in smaller portions
        wait_for_send = send(sockets_[num_clients_], temp_arr, 1000, 0);
        if(wait_for_send > 0){
                memcpy(temp_arr, img_arr+temp_count, wait_for_send);
                temp_count += wait_for_send;
        }
}

Client side receive code(The first package is received in another function and passes the data to this function):
size_p = new unsigned char[(unsigned int)p->data_value_1];

int temp_count = 0, wait_for_recive, image_size = p->data_value_1;
char temp_arr[1000];
while(temp_count < image_size){
        // Receive the image in smaller portions
        wait_for_recive = recv(connect_socket_, temp_arr, 1000, 0);
        if(wait_for_recive > 0){
                memcpy(size_p+temp_count, temp_arr, wait_for_recive);
                temp_count += wait_for_recive;
        }
}

// Create the new image
gImages.create("I_Image", p->data_value_2, p->data_value_3, size_p);

Edit : The issue went away entirely when I rewrote the send code to a do while loop instead and removed the if statement, which means the if loop was screwed up.

do{
        memcpy(temp_arr, img_arr+temp_count, 1000);
        // Send image in smaller portions
        wait_for_send = send(sockets_[num_clients_], temp_arr, 1000, 0);

        temp_count += wait_for_send;
}
while(temp_count < size_);
« Last Edit: June 06, 2013, 04:10:31 pm by Chili »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Image shifted after transfer
« Reply #1 on: June 06, 2013, 04:56:28 am »
Are you using TCP or UDP sockets?  UDP just sends and doesn't care about order or even if the packets got there which could/would/should caused jumbled data.  TCP is a little slower but makes sure everything arrives in order(unless multiple sends/receives are happening on the same socket)

Chili

  • Guest
Re: Image shifted after transfer
« Reply #2 on: June 06, 2013, 06:27:21 am »
Are you using TCP or UDP sockets?  UDP just sends and doesn't care about order or even if the packets got there which could/would/should caused jumbled data.  TCP is a little slower but makes sure everything arrives in order(unless multiple sends/receives are happening on the same socket)
TCP but considering that I had to split the image up into multiply sends/receive could that be it? Although then the image would most likely be a bit different each time the program is ran which is not the case. and only tests I've done so far is with a single client connected to the server.
« Last Edit: June 06, 2013, 06:32:41 am by Chili »

 

anything