Link to home
Start Free TrialLog in
Avatar of hongen
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
Avatar of Venci75
Venci75

you can - but probably they must be openned in two different threads
Avatar of hongen

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);
Avatar of hongen

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?
Avatar of hongen

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
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hongen

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.java:9: cannot resolve symbol
symbol  : class ServerSocket
location: class ProcessingSocketThread
     ServerSocket ss = new ServerSocket(port);
     ^
ProcessingSocketThread.java:9: cannot resolve symbol
symbol  : class ServerSocket
location: class ProcessingSocketThread
     ServerSocket ss = new ServerSocket(port);
                           ^
ProcessingSocketThread.java: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(10001)).start();          
     }


Error:
CopsServer.java:71: cannot resolve symbol

symbol  : method start ()

location: class ProcessingSocketThread

               ProcessingSocketThread pst1 = (new ProcessingSocketThread(10001)).start();

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...
>>ProcessingSocketThread pst1 = (new ProcessingSocketThread(10001)).start();

should be

ProcessingSocketThread pst1 = new ProcessingSocketThread(10001);
(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.
did you try the kennethxu's suggestion?
Avatar of hongen

ASKER

Thanks a lot. It works now. Thansk also goes to Kennethxu and all the replier.
Hongen