[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.platform.swt] Re: Listening on 2 ports same time from SWT APP.
|
sumit panchasara wrote:
Hi!
ya it is blocked. But by creating threads also it will make number of
threads for the same port. I want to connect to different port address.
"Stefan Langer" <eclipse@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:d86ol8$r9f$1@xxxxxxxxxxxxxxxxxxx
sumit panchasara wrote:
Hi,
I made one SWT app. using Eclipse. I am creating two buttons for server1,
server2 in a single application. When I press server1 it should start
listening on port 5555 and on pressing server2, should start listening on
6666.
Clients issues related commands and servers should accept both
connections(on related ports). e.g. client1 issues command for port 5555
and
client2 issues command for port 6666.
All clients are different machines.
Servers are on same machine. i.e. it is only one but listening on two
different ports.
Communication Protocol I have used is TCP/IP.
Now every thing is working fine. But for only one of these port. server
doesn't allow me to listen on second port. i.e. after pressing server1 I
can
not able to press server2. Thus Client listening on port 6666 for
example,
will not able to build connection with server.
What to do in this case?
Sumit.
How are you establishing the connection? Are you creating new Threads to
connect to server or are you doing it in the eventdispatch thread? If so
then you are blocking the whole ui.
Regards
Stefan
Spawn a Thread for every Port you are opening and keep track of the
thread. If the thread is allready started simply ignore the request of
the button. If it has not been started create a Thread and start it to
listen on the requested port.
If you are using 1.4 or higher try using nonblocking IO.
In Order to keep the user from pressing the button more than once you
might try to disable it when the thread for listening on the port has
been spawned.
Something along the lines
Button btnPort55 = new Button(parent, SWT.PUSH);
btnPort55.addActionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e)
{
spawnThread(66);
}
public void widgetSelected(SelectionEvent e)
{
spawnThread(55);
}
});
Button btnPort66 = new Button(parent, SWT.PUSH);
btnPort66.addActionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e)
{
spawnThread(66);
}
public void widgetSelected(SelectionEvent e)
{
spawnThread(55);
}
});
spawnThread(int port)
{
Thread thread = this.threads_.get(new Integer(port));
if(!thread.isAlive())
thread.start();
}
threads_ is a HashMap that holds all the threads that need to listen to
a port keyed by the portnumber. To prevent the user from pressing the
buttons while a thread is running simply disable them with
btnPort55.setEnabled(false);
That should give you a clue.
Regards
Stefan
P.S.: Don't forget that you can only update the ui in the eventdispatch
thread use execAsync or execSync to execute code for the ui in your
spawned Threads.