Membuat program chatting sederhana menggunakan Java

Oke sohib q semua………Salam satu jiwa.Kali ini aku mau posting lagi nih,postingan ku kali ini ada hubungannya dengan java programing.Disini aku mau posting cara untuk membuat program chatting sederhana dengan memanfaatkan socket.Okelah gak usah lama-lama ngomong langsung aja nih aku kasih source code dari program chatting sederhananya……Tapi disini aku pake netBeans untuk buat programnya…….

source code buat server :

import java.io.*;
import java.net.*;

public class Main {

public static void main(String[] args) throws IOException {

ServerSocket server = null;
Socket client = null;

try {
server = new ServerSocket(8888);
System.out.println(“Server telah online”);
client = server.accept();
System.out.println(“Client telah masuk dan bisa online”);
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
System.out.println(“silahkan chat”);
InputStream masuk = client.getInputStream();
OutputStream keluar = client.getOutputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

String pesan;
while (true) {
pesan = in.readLine();
if (pesan.equalsIgnoreCase(“stop”))
{
out.println(“sempai jumpa”);

break;
}

System.out.println(“Client Nomong :”+pesan);
System.out.print(“Server :”);
pesan = stdin.readLine();
out.println(pesan);
}

server.close();
client.close();
in.close();
out.close();
stdin.close();

}

}

Dan ini source code buat client

import java.io.*;
import java.net.*;

public class Main {

public static void main(String[] args) throws IOException {
Socket client = null;

client = new Socket (“10.5.1.65”, 8888);
BufferedReader sin=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintStream sout=new PrintStream(client.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;

while (true){
System.out.print(“Client :”);
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print(“Server : “+s+”\n”);
if ( s.equalsIgnoreCase(“Bye”) )
break;
}
stdin.close();
sout.close();
sin.close();
client.close();
}

}

About miftafebrian

oke coy......

Posted on April 27, 2012, in Java. Bookmark the permalink. 1 Komentar.

  1. thanks ya..
    very help

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: