Results 1 to 10 of 10
  1. #1
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Question how to handle PORT - - Multithreaded Client UDP

    Hi everybody,

    this is my first post, I am a beginner to Java and I really need your help.
    I am implementing a multithreaded server , that serves more clients also multithreaded with UDP Socket.They connect to each other, and they work fine when there are just one client/two different clients(which means two different files).
    But :
    When I try to connect 2 clients with the same code, they connect to the same port, which is the one I set in the piece of code afterwards.
    so this is my question: how to connect multithreaded clients to the server in different ports?
    Here is the piece of code:

    class WorkerThread extends Thread{
    DatagramSocket socket = null;
    int bufLength = 256;
    int port=9981;
    ....

    }


    Thank you in advance!

  2. #2
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Is my question too difficult, or is too dummy?
    I believe is the second, so please let me know something. I'll be very grateful at least to see if there is another way to write my doubts.
    Thanks!:)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    You'll have to show more code for anyone to be able to help.
    I haven't done much with Datagrams except for the textbook samples.
    they connect to the same port
    Why is that a problem?
    The server could look in the Datagram to see who sent it.

  4. #4
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Oh, ok more code. Ok, now I put the thread of my UDPserver. In the main I start the thread ServerThread1():

    class ServerThread1 extends Thread {
    private DatagramSocket socket = null;
    private boolean chiudiSocket = true;
    private int bufLength = 256;
    int port = 9377;

    ServerThread1() {
    socket = new DatagramSocket(port);
    System.out.println("Server listening on port: " + socket.getLocalPort());

    @Override
    public void run() {

    ** exchange messages with the client **


    }
    }
    }


    My QUESTION is the following: how can I assign to every client a different port?
    If I put the same port to everybody(server and clients they connect correctly).
    I need different port for every client because is a requirement of my project.

    Thank you for your answer!

    ps. forget of the piece of code I put in my first thread!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    I need different port for every client because is a requirement of my project.
    Have the server have one port for login. The response to that login would be to return a port number for future communications. Sort of like FTP does in some mode.

  6. #6
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks Norm for your answers! But I have no idea how FTP works...I am a dummy newbie.
    I have this server always up, and some clients that have to connect to this server each on one different port.
    If I use one port to every client it works fine.
    I have no idea how to connect every client with a different port, because this server is always up on the same port....

    Any suggestion?

    I am sorry if I put only few lines of the project, but my professor is kinda obsessed with plagiarism, which is punished with a severe report to the chancellor of my uni, and I feel haunted by this. So that's why I am kind of stingy with the code. Please be patient.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    My idea is that the server listens at port xx for logins. All clients first connect to port xx.
    The server finds an unused port: yy, starts a thread listening on that port and then sends the port number to the client. The client and server then communicate over port yy.
    The next client sends a login to server port xx, the above is repeated (server finds port zz available). The new client and server communicate over port zz. etc etc etc

  8. #8
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks Norm, I'll try how you say, it sounds good. But I need a bit of time.

    Instead I thought that it was enough to put a different port on the client's part, like this:

    client nr.1
    --sending a request to the server--
    byte[] buf = new byte[bufLength];
    InetAddress indirizzoIP = InetAddress.getByName("localhost");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, indirizzoIP, 9987);

    client nr. 2
    --sending a request to the server--
    byte[] buf = new byte[bufLength];
    InetAddress indirizzoIP = InetAddress.getByName("localhost");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, indirizzoIP, 9988);

    As it is said on sun's site on the client side:
    "Next, the main method creates a DatagramSocket:
    DatagramSocket socket = new DatagramSocket();
    The client uses a constructor that does not require a port number. This constructor just binds the DatagramSocket to any available local port. It doesn't matter what port the client is bound to because the DatagramPackets contain the addressing information. The server gets the port number from the DatagramPackets and send its response to that port. "
    I get that if I put a different port on the packet, it indicates where the client want to be bound.
    In this configuration, though, the server doesn't bind with the clients.
    Why is that?
    What am I mistaken?

    Thanks in advance and for the patience.
    Last edited by maribo; 06-14-2010 at 05:09 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    After looking at the doc and writing a small test program I'm not sure I understand what your problem is. Have you written code that doesn't work?

    My test pgm has a server using a DatagramSocket at port 1234.
    It has 3 clients with DatagramSockets with 3 different ports: 2345, 2346 2347.
    Each client was able to send a datagram to the server and get a response.

  10. #10
    maribo is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    you are right....sorry, it was the tempest in a teapot or the storm in a teacup, as you prefer...

    Thank you for your answers!

Similar Threads

  1. Multithreaded server crashes
    By skarosg3 in forum Threads and Synchronization
    Replies: 7
    Last Post: 05-26-2010, 09:24 AM
  2. Multithreaded search
    By xjx in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-13-2010, 12:02 AM
  3. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 PM
  4. Replies: 11
    Last Post: 01-26-2009, 12:22 AM
  5. A simple multithreaded server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •