Thursday, September 20, 2012

Aplikasi Chatting Dengan Java Bagian III

Setelah membangun beberapa class or file pada pertemuan yang lalu (pertemuan I dan pertemuan II), maka kali ini code terakhir yang akan dibuat pada program chat ini.

Berikut listing programnya :

// program client : Simpan dalam ClientKu2.java
// kompile menjadi ClientKu2.class
import java.net.*;
import java.io.*;
public class ClientKu2 {   
    public static void main(String[] args) {
int port = 2002;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1;
// read arguments
if(args.length == 2) {
    server = args[0];
    try {
port = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
System.out.println("server port = 1500 (Standarnya)");
port = 2002;
    }
}
// Koneksi ke server
try {
    socket = new Socket(server, port);
    System.out.println("Terkoneksi ke server " +
   socket.getInetAddress() +
   ":" + socket.getPort());
}
catch (UnknownHostException e) {
    System.out.println(e);
    System.exit(ERROR);
}
catch (IOException e) {
    System.out.println(e);
    System.exit(ERROR);
}
try {
    input = new BufferedReader(new InputStreamReader(System.in));
    output = new PrintWriter(socket.getOutputStream(),true);
}
}
}

------------------------------------------------thanks for all....this end of code....please give comment

No comments: