Teddy
Teddy

Reputation: 21

Java TCP server socket accepting multiple clients on different ports

I am trying to build a socket that is capable of having multiple TCP connections, at different ports with multiple clients simultaneously.

The situation is my socket has to receive commands from a server and redirect the commands to wireless sensors (one command to one sensor, not broadcasting), and be able to collect the corresponding data from the sensors and then send them back to the server. Connections with the server would be using TCP, but connections with the sensors could be TCP or UDP. My main concern are the TCP connections. I am looking into java multithreaded or thread pooled socket tutorials. But the examples I’ve seen were only using a single port to handle all the connections. I’m also trying to look into other possible solutions: utilizing tomcat server, java servlet/JSP, socket channel etc…

I’m not an expert in networking or socket programming so I really hope someone with experience could point me in the right direction. Thank you for any help you can provide in this situation.

Not sure if I fully understand but it seems like it is unnecessary for me to obtain multiple ports for my situation. Seems like I would need to focus on multi threaded sockets and Java NIO topics?

And again thank you for the advice and help.

Upvotes: 2

Views: 5974

Answers (4)

Chinwendu Ochonma
Chinwendu Ochonma

Reputation: 11

My understanding of this request is that you would need to deploy several instances of your server socket application listening on their respective ports and capable of servicing multiple client connections.

By way of mathematical induction, if you have written your server app properly it should work anywhere it is deployed. Below is sample of what your server socket application run should look like

public void run() {
try { while(true) { try { Thread client_thread= new Thread(new ClientReqProcessor(serverSocket.accept())); client_thread.start(); }
catch(Exception ex) {
logger.error("connection error: "+ex.getMessage()); }
} }

}

Upvotes: 0

USSEraser
USSEraser

Reputation: 325

Why do you want to use multiple ports? You can have multiple connections on one listening port. The connection itself runs always on different ports.

while (running)
{
    try
    {
        @SuppressWarnings("resource")
        Socket socket = serverSocket.accept();
        new ServerHandler(socket); // ServerHandler is your class to handle one connection.
    }
    catch (IOException e)
    {
        ...
    }
}

When you really need different ports, you can create a lot of Threads, each with a ServerSocket. Ports are one of the most limited resources on your computer. The running Threads are much cheaper.

for(int i = 1000; i < 1100; i++)
{
    final int port = i;
    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                ServerSocket serverSocket = new ServerSocket(port);
                Socket socket = serverSocket.accept();
                new ServerHandler(socket); // ServerHandler is your class to handle one connection.
            }
            catch(IOException e)
            {
                ...
            }
        }
    }).start();
}

Upvotes: -2

Tarek
Tarek

Reputation: 697

This is rather a big project to be answered in full here. But here are some general guides:

1) If you want to create a socket on one port you need to create one thread to run it. That is called a server socket. Therefore, from the main thread u need to call one thread for every socket on every port.

2) Each server socket keeps listening on a certain port and waits for clients to connect. when a client actually connects, the server socket should open another thread for that connection alone and return back to listening.

while(myServerSocket.accept()) { Open connection thread }

My advice would be to learn online about how to open threads from classes and then follow the guide above.

Upvotes: 4

Alexandre Santos
Alexandre Santos

Reputation: 8338

Unless you are going over 10k connections then most web servers would be able to handle the traffic.

But maybe you should get more details on the difference between a connection, a socket, and a port. Take a look at this: What is the difference between a port and a socket?

On your question: one port can handle many connections. You don't need different ports for different connections.

Upvotes: 0

Related Questions