Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: communication protocol

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile communication protocol

    hello members
    i am building a client server chat application and apparently the server can receive a message from the client and send that message to all the clients connected to the server.
    that is not what i want,all i want is explained below

    When a user types something into their chat window, their message will be sent as a string through a DataOutputStream.

    When the server receives a message, through a DataInputStream, it will send this same message to a specific user(client) connected to the server, again as a string through a DataOutputStream.

    The users will use a DataInputStream to receive the message.

    here is a copy of the serverthread code

    import java.io.*;
    import java.net.*;
     
    public class ServerThread extends Thread
    {
      // The Server that spawned us
      private Server server;
     
      // The Socket connected to our client
      private Socket socket;
     
      // Constructor.
      public ServerThread( Server server, Socket socket ) {
     
        // Save the parameters
        this.server = server;
        this.socket = socket;
     
        // Start up the thread
        start();
      }
     
      // This runs in a separate thread when start() is called in the
      // constructor.
      public void run() {
     
        try {
     
          // Create a DataInputStream for communication; the client
          // is using a DataOutputStream to write to us
          DataInputStream din = new DataInputStream( socket.getInputStream() );
     
     
          while (true) {
     
            // ... read the next message ...
            String message = din.readUTF();
     
            .
            System.out.println( "Sending "+message );
     
            //  have the server send it to all clients
            server.sendToAll( message );
          }
        } catch( EOFException ie ) {
     
     
        } catch( IOException ie ) {
     
          // This does; tell the world!
          ie.printStackTrace();
        } finally {
     
          // The connection is closed for one reason or another,
          // so have the server dealing with it
          server.removeConnection( socket );
        }
      }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: communication protocol

    Hello isaac.

    Welcome to the Java Programming Forums.

    I have moved this thread to our Java Networking forum - Java Networking - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Exception (payword protocol)
    By ThodorisVon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 27th, 2010, 01:35 PM
  2. servlet applet communication
    By prashanthi_asn in forum Java Servlet
    Replies: 0
    Last Post: May 21st, 2009, 12:50 AM