Results 1 to 8 of 8
  1. #1
    Ernie- is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Help sending messages using TCP with small chat application

    Hello, I'm quite new to JAVA programming. I need some help to implement a code that acts as a server for a Flash & Java applications and/or games.

    This code receives a string from an user and then sends a Hello message to that client.

    1) How can I specify a maximum number of clients and then store the clients in a array table so then I can work easily with them?

    This is my idea (from my C/C++ experience):
    [roomnumber][numberuser][IPuser]

    2) Where do I have to put a While loop so it never stops? I tried before "Socket skClient" line, but I don't know if it's right to place it here

    3) How can I send then a received message to a specific [roomnumber] and [numberuser] without closing any connection?

    Here goes my code (The one that's laughing at me):

    Java Code:
    import java.io.* ;
    import java.net.* ;
    
    class Server {
     static final int PORT=5000;
     
     public Server() {
      
      try {
       
       ServerSocket skServer = new ServerSocket( PORT );
    
       System.out.println("Listening on PORT " + PORT );
       
        Socket skClient = skServer.accept();
        System.out.println("Attending client " + numCli);
        OutputStream aux = skCliente.getOutputStream();
        DataOutputStream flux= new DataOutputStream( aux );
     
        flux.writeUTF( "Hello client " + numCli );
    
        skClient.close();
    
      } catch( Exception e ) {
       System.out.println( e.getMessage() );
      }
     }
     
     public static void main( String[] arg ) {
      new Server();
     }
    }
    Thanks in advance.

    Regards.
    Last edited by Ernie-; 03-20-2009 at 06:27 PM.

  2. #2
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Howdy, Howdy!

    To help save you some time after going through the same kind of things. You'll need to look at multi-threading your app. 1 thread for listening for new connections and setting them up. 1 thread for handling all the client to client communication and 1 thread for handling the actual client connection.

    I set up my architecture like this...

    Connection Class, listens for new connections. Once a new connection is found, I pass the info to the client handling class.

    ClientHandler class, parses any info sent from the client and determines if the info should go to the main server class to get passed to all clients or sends messages directly to the client its handling to send checks, error messages, etc.

    Main Server class, handles the client to client stuff like 1 client sending a message to all or to 1 client. I usually use the ClientHandler to determine what kind of message the client sent, who its addressed to and the contents of the message.

    After spending days dealing with issues of sending messages while receiving messages, client connections while receiving messages, etc.

    Hopefully this will help point to something that may give you some insight! :D

  3. #3
    Ernie- is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you Xyle, but I think that it's too complicate for me (to start with JAVA) I just want to make it simple. Maybe in some weeks.

    I solved yesterday the loop part, but it only accepts 1 message from 1 client and the clients are not stored (I don't know how to do it) in an array table (As it can do with C)

    Then it'll be easier to send messages between clients.

  4. #4
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    I agree, the networking architecture is a bit confusing. I've spent months on it trying to get a good one put together that I made that I can reuse over and over.

    I have read over tons of chat client/server setups and attempted to make tons more.

    The one thing that really screwed me up was the lack of multiple threads. If you only have 1 thread, the server wont respond to anything until it recieves a message, then you can parse that message, run some logic on it, then the server returns to listening for a message again.

    Best advice I can give on this is to look at other chat/server frameworks using internet search, and definitely look at Killer game programming in Java. That book, which is free online, finally gave me what I needed to complete a good messaging service, it doesnt have to apply to gaming, just the structure of the framework.

    My first attempt was a command line server and command line client. Since then I turned the server into an App with a JtextArea for messages and a JTextField for sending messages from the server. The client is sometimes a game gui in an applet or as with my Chat applet, a swing gui inside an applet.

    I finally got a chat server running on my website. Took me all day to put a working version up, but the experience was well worth it.
    Last edited by Xyle; 03-24-2009 at 05:30 AM.

  5. #5
    Mara is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    Well i just created a code that allows you to connect a certain number of clients (5, but you can specify as many you want) and then says "limit reached", and closes the server, though i still have to research how to "block" the port so that no more conections are allowed, it works. This is how it works:
    in the beginning the "toom" boolean is set as false, then, instead of while(true) its.. while(!toom).. inside the while there is an if that verifies if the number of clients is equal to the maximum client and if so, sets toom to true.
    Here's the code:
    EDIT: I've added so that the Server receives data from applications, the problem is that the data is received once the application is closed. (does anyone know how to fix that?)
    Java Code:
    import java.io.* ;
    import java.net.* ;
    
    class Server {
     static final int PORT=5000;
     int numCli = 0;
    boolean toom = false;
    int maxCli = 5; // Change this to the maximum clients
     public Server() {
    System.out.println("Starting server..");
      try {
    System.out.println("Listening on PORT " + PORT );
       ServerSocket skServer = new ServerSocket(PORT);
    
    while(!toom) {
    numCli = numCli+1;
        Socket skClient= skServer.accept();
        System.out.println("Attending client " + numCli);
    BufferedReader readerIn = new BufferedReader(new 
    
    InputStreamReader(skClient.getInputStream()));
    String data_in = readerIn.readLine();
    
    System.out.println("Received "+data_in+" from " + numCli);
    
        OutputStream aux = skClient.getOutputStream();
        DataOutputStream flux= new DataOutputStream( aux );
        flux.writeUTF( "Hello client " + numCli );
     if(numCli==maxCli) {
    System.out.println("Limit reached");
    toom = true;
    }
    
    }
      } catch( Exception e ) {
       System.out.println( e.getMessage() );
      }
     }
     
     public static void main( String[] arg ) {
      new Server();
     }
    }
    If anyone knows how to "block" the port instead of closing the server let me know.
    Also, i would like to know how to close a client's connection and make it show on the server when the client closes the application.
    Last edited by Mara; 03-26-2009 at 02:31 AM.

  6. #6
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    How about...

    Java Code:
    if(numClients<MaxClients){
        Socket skClient = skServer.accept();
        System.out.println("Attending client " + numCli);
    }

  7. #7
    Mara is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks for your response but its not working.. it keeps saying "limit reached" and then the jar is closed.. i have fixed the receveing of the messages of the other applications, they are received at real time now.. but i can only receive a maximum of 1 messages from a client. Here is my code.. Does anyone know how to fix it?

    Java Code:
    import java.io.* ;
    import java.net.* ;
    
    class Server {
     static final int PORT=5000;
     int numCli = 0;
    
    int maxCli = 5; // Change this to the maximum clients
     public Server() {
    System.out.println("Starting server..");
      try {
    System.out.println("Listening on PORT " + PORT );
       ServerSocket skServer = new ServerSocket(PORT);
    
    while(numCli<maxCli) {
    numCli = numCli+1;
    
        Socket skClient= skServer.accept();
        System.out.println("Attending client " + numCli);
    BufferedReader readerIn = new BufferedReader(new 
    
    InputStreamReader(skClient.getInputStream()));
    String data_in = readerIn.readLine();
    if(data_in != null) {
    System.out.println("Received "+data_in+" from " + numCli);
    }
        OutputStream aux = skClient.getOutputStream();
        DataOutputStream flux= new DataOutputStream( aux );
        flux.writeUTF( "Hello client " + numCli );
    }
    if(numCli==maxCli) {
    System.out.println("Limit reached.");
    }
      } catch( Exception e ) {
       System.out.println( e.getMessage() );
      }
     }
     
     public static void main( String[] arg ) {
      new Server();
     }
    }
    Last edited by Mara; 03-26-2009 at 11:45 PM.

  8. #8
    Mara is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    Oh btw.. i found this
    java.sun.com/docs/books/tutorial/networking/datagrams/broadcasting.html

    which probably helps answer all your questions

Similar Threads

  1. How to implement private chat application using jsp
    By PortalTeam in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-19-2010, 11:49 PM
  2. how to run chat application on multiple systems
    By jayeth in forum Advanced Java
    Replies: 4
    Last Post: 03-27-2009, 04:25 PM
  3. All JSP expert / Is there JSP code for calling & sending messages? please help me
    By java student in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-02-2008, 11:51 AM
  4. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 04:38 PM
  5. Help in Chat application
    By zocky in forum Networking
    Replies: 0
    Last Post: 11-08-2007, 08:19 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
  •