Overview
Features
Download
Documentation
Community
Add-Ons & Services

SocketInputStreams... how does it work ?

Please post support and help requests here.

SocketInputStreams... how does it work ?

Postby Fu » 17 May 2007, 11:03

Hi,
I work with Poco and wxWidgets. I want to display text received from server so I'm writing the run() method of MyTCPClient class (see this topic)

But I don't understand how to do it. In java, I would have done that :
^public void run(){
try {
while ((fromServer = in.readLine()) != null) {
this.gui.display.asyncExec(new Runnable(){ // this is to be able to write text in gui
public void run() {
gui.discutionArea.insert(MyTCPClient.fromServer);
}
});
}
} catch (IOException e) {
System.err.println("niop");
}
}^
where "in" is a BufferedReader.
It doesn't seem to be the same way with Poco...
I've done it (run() function) :
^SocketStream str(sock);
str << "message from client to server" << std::flush;
StreamCopier::copyToString(str, received);
sock.shutdownSend();
if (mainGui) mainGui->setWxDiscutionText(wxString("Connection closed"));^
where "received" is a std::string which is a member of MyTCPClient class.
So, if I understand, all strings sent by the server are copied in my "received" string.
So, now, I have to make another thread to display "received" string each time it changes ?
Isn't there a better way to display incoming strings ?
Fu
 
Posts: 4
Joined: 08 May 2007, 18:52

Re: SocketInputStreams... how does it work ?

Postby alex » 18 May 2007, 00:39

> So, now, I have to make another thread to display "received" string each time it changes ?
> Isn't there a better way to display incoming strings ?

You are making another thread in your Java code each time you receive new data. If you want the asynchronous function call in Poco, you can use ActiveMethod.

A word of warning on your code: unless you call shutdownSend() before copying the stream, you will not see anything in received string. Additionally, unless you set the receive timeout for SocketStream, your program will hang forever waiting for more data from server. But even if you do set the timeout, unless you call shutdownSend() before copying, the stream contents will still not be copied to string.

You may want to look into DialogSocket and see if it suits your task better:

Code: Select all

DialogSocket ds;
ds.connect(SocketAddress("host", port));
ds.sendMessage("message from client to server");
std::string str;
ds.receiveMessage(str);

alex
 
Posts: 1104
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: SocketInputStreams... how does it work ?

Postby Fu » 18 May 2007, 10:53

I got confused : because of a simple error in my server program, my client never received any thing from server (:redface:)
Now, every thing works fine and I understand how to manage input streams. Moreover, you're right : it's beter for me to use DialogSocket class.

One more time, your help is precious. Thanks a lot.

Fu
Fu
 
Posts: 4
Joined: 08 May 2007, 18:52


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests