Results 1 to 3 of 3
  1. #1
    Reztem is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Simple Client (2) - Server program

    Hi,
    I have made a simple client server program, (using a thread in the server to allow 2 clients at once to interact with the server)

    When running all 3 applications on 1 computer (using getlocalhost) it seems to run fine , but when I use 3 different computers ( for 2 clients + 1 server), only 1 client seems to work with the server..


    Java Code:
      public static void main(String[] args) throws Exception
        {
            DatagramSocket socket = new DatagramSocket(2310,InetAddress.getLocalHost());
            InetAddress clientAddress; //socket created here
            int port;
            Main.flightdetails = new FlightInfo("flightinfo.txt"); //textfile is retrived here into class FlightInfo
            Main m = new Main(); //object for main class created
           
            // TODO code application logic
           
     
            for(int i=0;i<2;i++) //main server loop
            {
                System.out.println("Server Started");
                byte[] buffer = new byte[256];
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                socket.receive(packet); //recive packet containing client username
                String clientName = new String(buffer);
                clientAddress = packet.getAddress();
                port = packet.getPort();
                System.out.print(clientName);
                 Main.FlightServer f = m.new FlightServer(clientAddress, port,clientName);
                 Thread t = new Thread(f); //thread created here for any concurrent clients
                 t.start();
            } //end of for loop
     
            
        }/
    The rest of the code for the server is after this, (code that sends packets to the client depending on what info the client requires - by sending packets to server)

    Can anyone see any problems with the 'for' loop above? As I thought this was supposed to create a thread for each client that connects to the server..

    Thanks in advance

  2. #2
    xenoepist is offline Xenoepist
    Join Date
    Jan 2012
    Location
    Brownsville, Tx
    Posts
    5
    Rep Power
    0

    Default Re: Simple Client (2) - Server program

    Hi, I did a similar program one time, but unfortunately it was too slow for the application I wanted to do, so I'm trying to do port TCP instead of UDP :( (3 second delay on the internet, not much of a delay on local net) Anyways, if you are trying to run this on three different computers over the internet (not the local router) you need to do port forwarding on your router. Also, if you are using android phones, remember to allow the internet permission. Other than that I can't think of anything else. Also, I think it would be better if you post your client code.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Simple Client (2) - Server program

    You're not trying to use localhost with remote machines are you? You're specifying the correct ip/port combo?

Similar Threads

  1. Simple server-client
    By DC200 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 06:13 PM
  2. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 06:26 PM
  3. Simple server/client text problem
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-02-2009, 11:33 AM
  4. Replies: 3
    Last Post: 01-07-2008, 08:01 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 11:36 AM

Posting Permissions

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