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

L

Larion

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

}
}
 
J

jcsnippets.atspace.com

Larion said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top