Mariesa Rahmawati’s Weblog

Menuju hari esok yang lebih baik

Client-Server

Client

package Chat;
import java.net.*;
import java.io.*;
public class ClientKu1 {
public static void main(String[] args) {
try {
Socket s = new Socket(“127.0.0.1”,2002);
System.out.println(“Komunikasi terhubung”);
System.out.println(“Client pada port = “+
s.getLocalAddress().getHostAddress() + ” : “+ s.getLocalPort());
System.out.println(“Server pada port = “+
s.getInetAddress().getHostAddress() + ” : “+ s.getPort());
BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream()));
System.out.println(“Menunggu pesan…”);
String greeting = “”;
while ((greeting=br.readLine())!=null) { System.out.println(greeting); }
System.out.println(“Selesai”);
br.close();
s.close();
}
catch (Exception e) { e.printStackTrace(); }
}
}

Server

package Chat;
import java.net.*;
import java.io.*;
public class ServerKu1 {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(2002);
System.out.println(“Menunggu Klien…”);
Socket s = ss.accept();
System.out.println(“Klien telah masuk”);
System.out.println(“Server pada port = “+
s.getLocalAddress().getHostAddress() + ” : ” + s.getLocalPort());
System.out.println(“Client pada port = “+
s.getInetAddress().getHostAddress() + ” : ” + s.getPort());
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String greeting = “”;
while(!(greeting=br.readLine()).equals(“”)) { pw.println(greeting); }
pw.close(); s.close(); ss.close();
} catch (Exception e) { e.printStackTrace(); }

June 25, 2008 - Posted by | Pemrograman

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: