Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Larion - 25 Mar 2006 15:59 GMT
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[i]);

           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();

       }
   }
jcsnippets.atspace.com - 26 Mar 2006 19:48 GMT
> Hi, I've to build a small Java Chat, based on 2 Clients and one central
> Server.
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.