sharing wxSocket in same application 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
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

sharing wxSocket in same application

Post by evstevemd »

Hi,
I think my first post was not clear and I have re written to make it clearer.
I have an application that have to interact with XDebug. Application is supposed to be server and it have to receive response from XDebug
as well as take commands from wxTextCtl and send them to XDebug.
The two communicate via TCP and the same socket until they terminate the transaction.
I have run out of Ideas as I'm still new to the world of sockets (I have even read the book). The sample shows only how to get data and writeback instantly but to me is different case. I hope I have made myself clear now

Thanks for your help!
Attachments
test socket.cpp
(5.31 KiB) Downloaded 119 times
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

Any idea?
I need to have single socket where I will be sending commands to XDebug and receive data.
I don't know how to share the socket among the methods of the class and write/read it.
My problem is how do I know to set shared socket (I tried having class member wxSocketBase m_socket but somehow I could not write anything successfully nor receive data) and knowing that I can write to and read from socket.
Please help me as I'm still new regarding sockets.

I have no problem on how to write/read data, just how to receive data using one method and send command using another method of the same class.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: sharing wxSocket in same application

Post by JimFairway »

Hi,


If you want to re-use the socket, then you have to have some way of making the receiver know the type of data it's receiving.
A simple way of doing that is to prepend some special byte sequence before the messages.
For example, in the code you posted in method void MainFrame::OnClientReadData if the length variable is not a number, then you could use that to signal that it's not output from xDebug.
e.g. for example
DEBUGGER: [NUMBER] [NULL] XML(data) [NULL] // syntax for xDebug data
wxTextCtrl: ["TEXT"][NULL][NUMBER][NULL] wxTextCtrlData [NULL] // syntax for output from wxTextCtrl

So if the string that comes in is not a number in length, but is the word "TEXT", then the receiver knows how to handle the rest of the data.

Alternatively, you could use another port for wxTextCtrl data.

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

hi Jim,
I'm not sure if I have made myself clear so I will try again. I have a shared socket and at least two methods. one method user that socket to read data and the other for sending data via the same socket. during the time of connection I want to stop any connection until current connection terminates.
as I'm writing I have not yet figured out how to send data from another method that handles button clicks.
thanks for helping!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: sharing wxSocket in same application

Post by JimFairway »

Hi,

Reading and writing are totally independent. I.e. you can write whenever you want, even if you're in the middle of reading something.
Are you trying to synchronize a command / response style?
E.g.

The user enters "Get this data from server", which is sent to the server and then when the response comes back, the answer is displayed?
If so, there's not much that you need to do... just call m_pSocket->Write when the users wants and then display the answer.
Unless your code is parsing the answer as a response to the command...?

Jim
OS: Vista SP1, wxWidgets 2.8.7.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

hi Jim,
you are right! I need to sync commands and response.
something like I send command and xdebug responds. I then parse response and determine next command. I may send next command long after receiving response depending on button clicks
again thanks for help
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: sharing wxSocket in same application

Post by JimFairway »

Hi,

The issue is less one of sharing the socket for send and receive, it's already set up for that.
It's more an issue of retaining state, and connecting responses to queries.
Is there anything in the response that you can use to connect to a specific command?
For example, if the response had some tag in it that connected it to the initial response, you could simply keep the command around long enough to until the response came in and you could associate them that way.
Does it matter to connect the answer to the response? Or is it simply for logging purposes?

Jim
OS: Vista SP1, wxWidgets 2.8.7.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

Hi Jim,
JimFairway wrote:Hi,
The issue is less one of sharing the socket for send and receive, it's already set up for that.
It's more an issue of retaining state, and connecting responses to queries.
Thanks you for clarifying that.
JimFairway wrote:Is there anything in the response that you can use to connect to a specific command?
Yes. For example when XD connect it brings alot of informations some of which are useful in successive commands.
Some response are whether the command executed successful and so you can continue or you should try again.
JimFairway wrote: For example, if the response had some tag in it that connected it to the initial response, you could simply keep the command around long enough to until the response came in and you could associate them that way.
do you suggest I use something like wxSleep and keep polling if there is a response?
JimFairway wrote: Does it matter to connect the answer to the response? Or is it simply for logging purposes?

Jim
Yes it matters in some much if not all of commands.
Thanks for your help, I really appreciate!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: sharing wxSocket in same application

Post by JimFairway »

Hi,

You could keep the history of the messages sent to the server in a list in your program . When a response is received from the server, determine if the response is from the first message. If it is, process the response, if not, check the second and so on.
I doubt the server would provide responses out of order, so you could delete all skipped messages, and the message which matched from the list.

If you don't want to allow the user to make changes while there are messages outstanding, then block the UI, and then you only need to store 1 message.
Unblock the UI when a response is received from the server, of course, it should only be for commands where a response is expected.

If you go down that path, you have to watch when the socket closes unexpectedly and unblock the UI.

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

JimFairway wrote:Hi,

You could keep the history of the messages sent to the server in a list in your program . When a response is received from the server, determine if the response is from the first message. If it is, process the response, if not, check the second and so on.
I doubt the server would provide responses out of order, so you could delete all skipped messages, and the message which matched from the list.
This, I think should be my last resort so I want to try your latter advice.
JimFairway wrote:If you don't want to allow the user to make changes while there are messages outstanding, then block the UI, and then you only need to store 1 message.
Unblock the UI when a response is received from the server, of course, it should only be for commands where a response is expected.

If you go down that path, you have to watch when the socket closes unexpectedly and unblock the UI.

Hope that helps,

Jim
This looks like what I need, I just send command and when first response comes I block GUI until I want to send another. The side effects are clear that UI get blocked which is undesired in my case. Will it work this way If I only block the socket and not whole UI?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: sharing wxSocket in same application

Post by JimFairway »

Hi,
Will it work this way If I only block the socket and not whole UI?
Not sure what you mean by this.
Are you asking if socket should be blocked while you're waiting for a response?
If you want to allow the UI to continue to operate, you can turn the socket into half-duplex, 1 send, followed by 1 receive by creating a transmit queue.
If you're waiting for a response to a message, then don't send a message, instead add it to the transmit queue.
When the response is received, pop the oldest message from the queue (if the queue is not empty) and send it.

Hope that's what you're looking for.

Jim
OS: Vista SP1, wxWidgets 2.8.7.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: sharing wxSocket in same application

Post by evstevemd »

JimFairway wrote:Hi,
Will it work this way If I only block the socket and not whole UI?
Not sure what you mean by this.
Are you asking if socket should be blocked while you're waiting for a response?
You have described what I was trying to do well, a half duplex system!
JimFairway wrote: If you want to allow the UI to continue to operate, you can turn the socket into half-duplex, 1 send, followed by 1 receive by creating a transmit queue.
If you're waiting for a response to a message, then don't send a message, instead add it to the transmit queue.
When the response is received, pop the oldest message from the queue (if the queue is not empty) and send it.

Hope that's what you're looking for.

Jim
Yes this is it. let me try implementing it and I will be back.
Thank you!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply