Build a simple Textual Chat with Java - 2 Clients and 1 Server: Help Me !!

Discussion in 'Java' started by Larion, Mar 25, 2006.

  1. Larion

    Larion Guest

    Hi, I've to build a small Java Chat, based on 2 Clients and one central
    Server.
    No graphic elements are needed, this application runs in a system
    shell.
    I also have some examples (a client send to a server a string and this
    resend this string with UpperCase to the client) so I think I can use
    che client code, but I must recompile che Server code. Can anyone help
    me?

    "code:SERVER"

    import java.io.*;
    import java.net.*;

    class TCPServer {

    public static void main(String argv[]) throws Exception
    {
    String clientSentence;
    String capitalizedSentence;

    ServerSocket welcomeSocket = new ServerSocket(6860);

    while(true) {

    Socket connectionSocket = welcomeSocket.accept();

    System.out.println("Connected to: " +
    connectionSocket.getInetAddress());

    BufferedReader inFromClient =
    new BufferedReader(new

    InputStreamReader(connectionSocket.getInputStream()));

    DataOutputStream outToClient =
    new
    DataOutputStream(connectionSocket.getOutputStream());

    clientSentence = inFromClient.readLine();

    System.out.println("FROM CLIENT: " + clientSentence);

    capitalizedSentence = clientSentence.toUpperCase() +
    '\n';

    System.out.println("TO CLIENT: " + capitalizedSentence);

    outToClient.writeBytes(capitalizedSentence);
    }
    }
    }

    - - - - - - -

    " code:CLIENT "

    import java.io.*;
    import java.net.*;
    class TCPClient {

    public static void main(String argv[]) throws Exception
    {
    String sentence;
    String modifiedSentence;

    BufferedReader inFromUser =
    new BufferedReader(new InputStreamReader(System.in));

    for (int i=0; i < argv.length; i++)
    System.out.println("argv[" + i + "]" + argv);

    Socket clientSocket = new Socket(argv[0], 6860);

    DataOutputStream outToServer =
    new DataOutputStream(clientSocket.getOutputStream());

    BufferedReader inFromServer =
    new BufferedReader(new
    InputStreamReader(clientSocket.getInputStream()));

    sentence = inFromUser.readLine();

    outToServer.writeBytes(sentence + '\n');

    modifiedSentence = inFromServer.readLine();

    System.out.println("FROM SERVER: " + modifiedSentence);

    clientSocket.close();

    }
    }
    Larion, Mar 25, 2006
    #1
    1. Advertising

  2. "Larion" <> wrote in message
    news:...
    > Hi, I've to build a small Java Chat, based on 2 Clients and one central
    > Server.
    > No graphic elements are needed, this application runs in a system
    > shell.
    > I also have some examples (a client send to a server a string and this
    > resend this string with UpperCase to the client) so I think I can use
    > che client code, but I must recompile che Server code. Can anyone help
    > me?

    <snipped code>

    Help you with what? What exactly is your question?

    Best regards,

    JC
    --
    http://jcsnippets.atspace.com/
    a collection of source code, tips and tricks
    jcsnippets.atspace.com, Mar 26, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joanduan
    Replies:
    0
    Views:
    323
    joanduan
    Apr 10, 2004
  2. joanduan
    Replies:
    1
    Views:
    417
    Roman Vottner
    Apr 11, 2004
  3. Replies:
    0
    Views:
    2,190
  4. Francesco
    Replies:
    1
    Views:
    527
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Dec 27, 2006
  5. ostra pikula

    Simple textual calendar

    ostra pikula, Nov 3, 2008, in forum: Python
    Replies:
    8
    Views:
    294
    Jorgen Grahn
    Nov 4, 2008
Loading...

Share This Page