Need Help Sending multiple Images using wxSocketOutputStream Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
rineez
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Nov 08, 2008 7:00 pm
Location: Thiruvananthapuram
Contact:

Need Help Sending multiple Images using wxSocketOutputStream

Post by rineez » Sun Nov 09, 2008 4:15 pm

I'm trying to send a number of JPEG image files over the network using wxSocketOutputStream and wxSocketInputStream.

This is what I'm trying to do in my program.
At the server I open jpeg files one by one, using wxImage and write it to a wxSocketOutputStream object(constructed with a connected client socket) using SaveFile function of wxImage.

At the client I'm Creating a wxSocketInputStream object and then I construct a wxImage object from it and display

I have already got a Socket connection established using wxSocketClient and wxSocketServer.

My Problem now is that I'm recieving only the first one of the sent images at the client program.
Actually I got no clear idea how these wxSocketOutputStream and wxSocketInputStream classes work.
Can any one please tell me what I'm doing wrong here?
or if there is any simple alternative ways for transfering the images?
And I would like to know where I can get details about how to work with these classes. wxWidgets docs Class reference didn't have much about them. :( :?:
I'm pasting here The part of code I wrote to send and recieve images.

//Code of Server Program that send JPEG images

Code: Select all

while(DbCon->nextRecord()) //loops until there are more rows in table which stores file names 
{ wxSocketOutputStream *SymbolStream=new wxSocketOutputStream(*ServiceSockObj);//ServiceSockObj is pointer to wxSocketBase object for the client connection 
wxString Symbol=wxString(DbCon->getVarchar(1,512)); //read file name from database(using my own class that implements ODBC API) 
wxImage *SymbolImg= new wxImage(Symbol,wxBITMAP_TYPE_JPEG); //open file 
SymbolImg->SaveFile(*SymbolStream,wxBITMAP_TYPE_JPEG); 
} 
//End of Server Code Sample
*******************************
//Code of Client Program that send JPEG images

Code: Select all

for(int i=1;i<=n;i++) 
{ 
SlNo.Printf("%d)",i); 
wxSocketInputStream *SymbolStream=new wxSocketInputStream(*ClientSockObj); //ClientSockObj is the pointer to socket connected to server 
wxImage *SymbolImg=new wxImage(*SymbolStream,wxBITMAP_TYPE_JPEG); 
wxBitmapButton *Symbol_WxBitmapButton = new wxBitmapButton(VotingClient_WxPanel, 4100+n, wxBitmap(SymbolImg->Scale(100,111)), wxPoint(btnX,btnY), wxSize(100,111), wxBU_AUTODRAW, wxDefaultValidator, SlNo); 
Symbol_WxBitmapButton->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma"))); 
btnY+=Yinc; 
} 
//End of Client code sample [/color][/b]

Please check if u can see anything bad in my code??
I'm totally confused. :?:
the code compiles well and first image is getting recieved and displayed at the client correctly.
someone please help me out of this. :(
Last edited by rineez on Mon Nov 10, 2008 3:52 am, edited 1 time in total.
↑♥rineez♥↓

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Sun Nov 09, 2008 11:17 pm

Hi,

First, I suggest you use the code tags when you post code to make it more readable. Check the FAQ.

I would look at the sockets sample that should have come with your library 'wherever you installed wxWidgets'/samples/sockets.

Or check the sockets FAQ / Tutorial :
http://forums.wxwidgets.org/viewtopic.php?t=2736

Quickly perusing your code, it appears that you're not telling the other end where the file boundaries are, one file after another is sent through the socket.

I think the samples can demonstrate how to tell the other end what it should expect is coming.

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

rineez
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Nov 08, 2008 7:00 pm
Location: Thiruvananthapuram
Contact:

Post by rineez » Mon Nov 10, 2008 3:41 am

JimFairway wrote:Hi,

First, I suggest you use the code tags when you post code to make it more readable. Check the FAQ.

I would look at the sockets sample that should have come with your library 'wherever you installed wxWidgets'/samples/sockets.

Or check the sockets FAQ / Tutorial :
http://forums.wxwidgets.org/viewtopic.php?t=2736

Quickly perusing your code, it appears that you're not telling the other end where the file boundaries are, one file after another is sent through the socket.

I think the samples can demonstrate how to tell the other end what it should expect is coming.

Hope that helps,

Jim
Sorry I didn't know about the code tags. I'm new to the forum. I'll edit it again.
Well I've checked the sockets samples but it doesn't have anything about the wxSocketOutputStream.
And I thought the end of each call to

Code: Select all

wxImage::SaveFile
do something to mark the end of file, doesn't it?
I also tried using LoadFile at the client instead of direct constructor.
Do I need to call something to reinitialize the socket buffer or something?
↑♥rineez♥↓

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Mon Nov 10, 2008 12:12 pm

Hi,

I don't believe that SaveFile does anything special to mark end of file.

A good model is File Transfer Protocol (ftp), it uses a separate port for the control path and the data path.
The control path tells the other side what to expect.

See
http://www.faqs.org/rfcs/rfc959.html

Another method would be to close the socket after sending every file to tell the other side that something new is coming next time.

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

rineez
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Nov 08, 2008 7:00 pm
Location: Thiruvananthapuram
Contact:

Post by rineez » Mon Nov 10, 2008 12:38 pm

JimFairway wrote:Hi,

I don't believe that SaveFile does anything special to mark end of file.

A good model is File Transfer Protocol (ftp), it uses a separate port for the control path and the data path.
The control path tells the other side what to expect.

See
http://www.faqs.org/rfcs/rfc959.html

Another method would be to close the socket after sending every file to tell the other side that something new is coming next time.

Hope that helps,

Jim
Well guss that was helpful but I have more doubts..

Did you mean Close socket by calling wxSocketBase::Close() function?

How can I reopen such a closed connection?
Do I have to call Connect from client again?

And hey I've tried closing the OutputStream at the bottom of loop.

And I'm afraid it wont be worth reimplementing the whole thing in FTP style now as I'm almost at the end of the project. I fear will become double headache for me.:(
↑♥rineez♥↓

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Mon Nov 10, 2008 2:12 pm

Hi,

Yet another approach is to not use the JPEG LoadFile / SaveFile, but rather design it so that it can send any file.

Approach would be:

Code: Select all

   open connection
   for each file
      get size of file
      send size of file (bytecount)
      send file with that bytecount
   end for
   close connection
The receiver would first get the byte count of the file, create a file, and read that many bytes from the socket and store them to the file.

Then the next data received would be the size of the next file.

However, I would put in some kind of error checking to make sure the 2 sides were not out of sync.
For example, I would have some special OP code and checksum around the bytecount.
You could even send the filename using this approach.

I would most likely use wxFile for manipulating the files, getting the file size, etc.
See http://docs.wxwidgets.org/2.6/wx_wxfile.html

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

rineez
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Nov 08, 2008 7:00 pm
Location: Thiruvananthapuram
Contact:

Post by rineez » Mon Nov 10, 2008 2:19 pm

JimFairway wrote:Hi,

Yet another approach is to not use the JPEG LoadFile / SaveFile, but rather design it so that it can send any file.

Approach would be:

Code: Select all

   open connection
   for each file
      get size of file
      send size of file (bytecount)
      send file with that bytecount
   end for
   close connection
The receiver would first get the byte count of the file, create a file, and read that many bytes from the socket and store them to the file.

Then the next data received would be the size of the next file.

However, I would put in some kind of error checking to make sure the 2 sides were not out of sync.
For example, I would have some special OP code and checksum around the bytecount.
You could even send the filename using this approach.

I would most likely use wxFile for manipulating the files, getting the file size, etc.
See http://docs.wxwidgets.org/2.6/wx_wxfile.html

Hope that helps,

Jim
Thanks for the Repay. But right now I dont actually wish to send any files other than images.
Well, I'm doing this for students so I do wish to keep this as simple as possible.
And can someone tell me how to reopen a socket connection that is closed by

Code: Select all

wxSocketBase::Close()
↑♥rineez♥↓

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Mon Nov 10, 2008 2:25 pm

Hi

Use wxSocketClient (derived from wxSocketBase), then use wxSocketClient::Connect.

I'll suggest again that you look at the sample client that should have come with your package.

Jim
OS: Vista SP1, wxWidgets 2.8.7.

rineez
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Nov 08, 2008 7:00 pm
Location: Thiruvananthapuram
Contact:

Post by rineez » Mon Nov 10, 2008 5:51 pm

JimFairway wrote:Hi

Use wxSocketClient (derived from wxSocketBase), then use wxSocketClient::Connect.

I'll suggest again that you look at the sample client that should have come with your package.

Jim
Ok so that means I'll have to reconnect.

And yeah I'm already sending text data in my program using wxSocketBase functions Read, Write, ReadMsg, and WriteMsg.

Now I doubt the idea of sending images also through network is a knife at the neck of simplicity in this project.
↑♥rineez♥↓

Post Reply