hongen
asked on
how to open two sockets with different listening port
can I open two sockets with different listening port
numbers in a single server? For each socket, the way
to deal with the inputstream and outputstream is
different.Thanks in advance. Hongen
numbers in a single server? For each socket, the way
to deal with the inputstream and outputstream is
different.Thanks in advance. Hongen
you can - but probably they must be openned in two different threads
ASKER
I am new to Java. I tried but couldn't figure out how to do it. Could you please post some example with some code?
ServerSocket server = new ServerSocket(1234);
ServerSocket server2 = new ServerSocket(1235);
ServerSocket server2 = new ServerSocket(1235);
ASKER
I am new to Java. I tried but couldn't figure out how to do it. Could you please post some example with some code?
ASKER
I am new to Java. I tried but couldn't figure out how to do it. Could you please post some example with some code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your code. I tried but there are two issues:
1. I compiled the program and got error
source code:
class ProcessingSocketThread implements Runnable {
int port;
public ProcessingSocketThread(int port) {
this.port = port;
}
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket s = ss.accept();
// process the socket
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Errors:
ProcessingSocketThread.jav a:9: cannot resolve symbol
symbol : class ServerSocket
location: class ProcessingSocketThread
ServerSocket ss = new ServerSocket(port);
^
ProcessingSocketThread.jav a:9: cannot resolve symbol
symbol : class ServerSocket
location: class ProcessingSocketThread
ServerSocket ss = new ServerSocket(port);
^
ProcessingSocketThread.jav a:11: cannot resolve symbol
symbol : class Socket
location: class ProcessingSocketThread
Socket s = ss.accept();
^
3 errors
I added "import java.net.*;" at the very beginning. It compiled OK.
2. But the main class gave error:
source code:
try{
while (listening)
{
ProcessingSocketThread pst1 = (new ProcessingSocketThread(100 01)).start ();
}
Error:
CopsServer.java:71: cannot resolve symbol
symbol : method start ()
location: class ProcessingSocketThread
ProcessingSocketThread pst1 = (new ProcessingSocketThread(100 01)).start ();
Any thoughts? Thanks a lot. Hongen
1. I compiled the program and got error
source code:
class ProcessingSocketThread implements Runnable {
int port;
public ProcessingSocketThread(int
this.port = port;
}
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket s = ss.accept();
// process the socket
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Errors:
ProcessingSocketThread.jav
symbol : class ServerSocket
location: class ProcessingSocketThread
ServerSocket ss = new ServerSocket(port);
^
ProcessingSocketThread.jav
symbol : class ServerSocket
location: class ProcessingSocketThread
ServerSocket ss = new ServerSocket(port);
^
ProcessingSocketThread.jav
symbol : class Socket
location: class ProcessingSocketThread
Socket s = ss.accept();
^
3 errors
I added "import java.net.*;" at the very beginning. It compiled OK.
2. But the main class gave error:
source code:
try{
while (listening)
{
ProcessingSocketThread pst1 = (new ProcessingSocketThread(100
}
Error:
CopsServer.java:71: cannot resolve symbol
symbol : method start ()
location: class ProcessingSocketThread
ProcessingSocketThread pst1 = (new ProcessingSocketThread(100
Any thoughts? Thanks a lot. Hongen
You have to derive the socket thread class from java.lang.Thread class - otherwise there is no start() method to call ;-)
So do this:
class ProcessingSocketThread extends Thread implements Runnable
{
...
}
I think this should be work
greetings jchecker...
So do this:
class ProcessingSocketThread extends Thread implements Runnable
{
...
}
I think this should be work
greetings jchecker...
>>ProcessingSocketThread pst1 = (new ProcessingSocketThread(100 01)).start ();
should be
ProcessingSocketThread pst1 = new ProcessingSocketThread(100 01);
(new Thread( pst1 )).start();
should be
ProcessingSocketThread pst1 = new ProcessingSocketThread(100
(new Thread( pst1 )).start();
Would you import the ServerSocket and Socket both class?
If you didn't do that, to add this statement "import java.net.*;" to in your code.
If you didn't do that, to add this statement "import java.net.*;" to in your code.
did you try the kennethxu's suggestion?
ASKER
Thanks a lot. It works now. Thansk also goes to Kennethxu and all the replier.
Hongen
Hongen