Go Back   Java Programming Forums > Java Standard Edition Programming Help > Java Networking


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-02-2010, 10:34 AM
Junior Member
 

Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
isaac is on a distinguished road
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

Java 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 );
    }
  }
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 05-02-2010, 12:20 PM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,581
Thanks: 103
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Stressed
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
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception (payword protocol) ThodorisVon What's Wrong With My Code? 6 27-01-2010 05:35 PM
servlet applet communication prashanthi_asn Java Servlet 0 21-05-2009 05:50 AM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 08:31 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.