CHATTING
public void start() {
keepGoing = true;
try
{
// the socket used by the server
ServerSocket serverSocket = new ServerSocket(port);
// infinite loop to wait for connections
while(keepGoing)
{
// format message saying we are waiting
display("Server waiting for Clients on port " + port + ".");
Socket socket = serverSocket.accept(); // accept connection
// if I was asked to stop
if(!keepGoing)
break;
ClientThread t = new ClientThread(socket); // make a thread of it
al.add(t); // save it in the ArrayList
t.start();
}
// I was asked to stop
try {
serverSocket.close();
for(int i = 0; i < al.size(); ++i) {
ClientThread tc = al.get(i);
try {
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
}
catch(IOException ioE) {
// not much I can do
}
}
}
catch(Exception e) {
display("Exception closing the server and clients: " + e);
}
}
2. Source program dibawah akan menampilkan message atau pesan pada terminal atau console
if(sg == null)
System.out.print(messageLf);
else
sg.appendRoom(messageLf);
3. Source program dibawah akan menyecan atau mencari Client/user yang terhubung atau connected
for(int i = 0; i < al.size(); ++i) {
ClientThread ct = al.get(i);
writeMsg((i+1) + ") " + ct.username + " since " + ct.date);
}
4. Source program dibawah adalah untuk chat atau message
import java.io.*;
public class ChatMessage implements Serializable {
protected static final long serialVersionUID = 1112122200L;
static final int WHOISIN = 0, MESSAGE = 1, LOGOUT = 2;
private int type;
private String message;
// constructor
ChatMessage(int type, String message) {
this.type = type;
this.message = message;
}
// getters
int getType() {
return type;
}
String getMessage() {
return message;
}
}
5. Source program dibawah untuk mengirim pesan ke server
void sendMessage(ChatMessage msg) {
try {
sOutput.writeObject(msg);
}
catch(IOException e) {
display("Exception writing to server: " + e);
}
}
6. Source program dibawah akan menempilkan default dari portNumber
ServerAddress dan userName
public static void main(String[] args) {
int portNumber = 1500;
String serverAddress = "localhost";
String userName = "Anonymous";
7. Source program dibawah adalah menunggu pesan dari user/client
Scanner scan = new Scanner(System.in);
// loop forever for message from the user
while(true) {
System.out.print("> ");
// read message from user
String msg = scan.nextLine();
// logout if message is LOGOUT
if(msg.equalsIgnoreCase("LOGOUT")) {
client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
// break to do the disconnect
break;
}
// message WhoIsIn
else if(msg.equalsIgnoreCase("WHOISIN")) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, ""));
}
else { // default to ordinary message
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, msg));
}
}
8. gambar dibawah menampilkan client yang baru saja connect/tersambung ke server
jika client masuk dan tersambung:
9. gambar dibawah menampilkan client1
10. gambar dibawah menampilkan client2
11. gambar dibawah menampilkan chat client1
12. gambar dibawah menampilkan chat client2
13. gambar dibawah menampilkan chat client1 dan client2 pada server
Tidak ada komentar:
Posting Komentar