creating a server that listens at two different ports
I have an app in java that is nothing bu开发者_开发技巧t a remote server.The remote server I wanna design to accept two kinds of client:
1.Some that connect at 127.0.0.1 at the port 6000
2.The second type of client that connects at 127.0.0.1 at the port 6500
How do I do that???
I've tried the following:
public class Start {
Socket socket=null;
private String serverIpAddress="127.0.0.1";
static Thread cThread;
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadPooledServer server = new ThreadPooledServer(6000);
new Thread(server).start();
cThread = new Thread(new ClientThread());
cThread.start();
}
Where ThreadPooledServer
and ClientThread
are two different classes.
When I wanna start the second thread ....and that means this line:
cThread.start();
....java tells me that "No enclosing instance of Start type is accessible!!!"
According to me it means that I cannot start two different threads in the same main.....Well,if I can't do it this way...what is the proper way to do it?....to start two threads
1.That listen on the local machine to port 6000
2.Second that listen on the local machine at port 6500???
UPDATE: I wanna create a server that can listen on multiple ports and when I get a new connection, I want to be able to tell which port that connection used!!!!!!!!!!!!
If you want to listen on multiple ports in an efficiant way (=SingleThreaded) it may be a good idea to have a look at java.nio.SocketChannel and Selector, but I assume this is overkill for a low load scenario.
Regarding the "No enclosing instance of type Start is accessible.":
Either create the treads in the constructor and create a new object of your class or declare the inner class static (if it does not need access to outer classes members)
精彩评论