how to open two sockets with different listening port

hongen
hongen used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you can - but probably they must be openned in two different threads

Author

Commented:
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?

Commented:
    ServerSocket server = new ServerSocket(1234);
     ServerSocket server2 = new ServerSocket(1235);
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
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?

Author

Commented:
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?
Commented:
class ProcessingThread implements Runnable {
  int port;
  public ProcessingThread(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();
    }
  }
}


and here - how to start the threads:
(new Thread(10000)).start();
(new Thread(10001)).start();
(new Thread(10002)).start();

Author

Commented:
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

Commented:
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.

Commented:
did you try the kennethxu's suggestion?

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial