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