Agen Terpercaya   Advertise
 
 
 
 
Pasang iklan, hanya lewat Contact Us.
Waspada penipuan iklan. Klik di sini untuk melihat daftar penipu.

SHARE MEMBUAT CHAT SEDERHANA CONNECT IP CLIENT/SERVER VIA JAVA SOCKET [COMPLETE/PLUS VIDEO DEMO]

RayMantic

Moderator
Staff member
Moderator
Daftar
4 Oct 1988
Post
×
Like diterima
9.035
Bimabet
[HIDE][/HIDE][HIDE]
STEP BY STEP
BUAT KOMPONEN
1.BUKA NETBEAN CREATE PROJECT SEPERTI BIASA SUHU.

2.BUAT FILE .JAVA STRUKTUR KOMPONEN SEPERTI INI.

3.SETELAH TERBENTUK SEPERTI GAMBAR DIATAS TINGGAL EDIT KOMPONEN
- RENAME ITEM 1 = cboCONNECTION
-DIDALAMNYA BERISI ITEM = SERVER dan CLIENT


-RENAME BUTTON1 = btnConnect = ON/OFF

-RENAME TEXTFIELD1 = txtUser = Username

-RENAME TEXTFILD2 = txtMessege = your messege here...

-RENAME BUTTON2 = btnsend = Send



SOURCE


Code:
public class chatme extends javax.swing.JFrame (
      socket client; //kelas socket untuk clien
      serversocket server; //kelas socket untuk server
      bufferedreader server_reader,client_reader; //buffer untuk membaca pesan
      bufferedwriter server_writer,client_reader; //buffer untuk mengirim pesan
)



Code:
private void client_connection () {
    try {
    string ip_address =JOption.showInputDialog("Masukan Alamat ip:");
    client = new socket(ip_address, 2000);
    cboconnection.setEnable(false);
    server_reader = new BufferedReader(ner inputStreamReader(client.getinputstream()));
    server_writer = new Bufferedwriter(ner inputStreamwriter(client.getoutputstream()));
    btnConnect.setText("Putuskan");
} catch (UnknownHostException e){
  System.err.println("Akses ke server gagal !");
  system.exit(-1);
} catch (IOException e){
  Logger.getLogger(ChatMe.class.getName()).log(Level.SEVERE, null, e);
}
}



Code:
private void read_connection (){
    try{
    try{
   try{
    server = new serversocket (2000);
    this.settitle("Mohon Tunggu Sebentar...");
}catch (IOException e) {
    system.err.println("gagal membuat server!");
    system.exit(-1);
}
client = server.accept();
this.settitle ("terhubung ke"+client.getinetaddress());
}catch (IOException e) {
system.err.println("akses ke server di tolak!");
system.exit(-1);
}
server_reader = new bifferedReader(new inputstreamreader(client.getinputstream()));
server_writer = new bifferedwriter(new outputstreamwriter(client.getoutputstream()));
}catch (IOException e) {
system.err.println("Tidak dapat membaca pesan !");
system .exit(-1);
}



Code:
private void disconnected_by_client(){
    try{
    client.close();
    server_reader.cloase();
    server_writer.cloase();
    cboConnection.setEnabled(true);
    btnConnect.setText("Hubungkan");
}catch (IOException e) {
    Logger.getLogger(Chatme.class.getName()).log(Level.SEVERE, null, e);
}
}
private void stopped_by_client(){
    try{
    server_reader.close();
    server_writer.close();
    btnConnect.setText("ON");
   this.setTitle("Terputus !");
} cath (IOException e){
    logger.getLogger(ChatMe.class.getName()).log(Level.SERVER, null, e)
}
}



Code:
private void cboConnectionItemStateChanged(java.awt.event.itemevent evt){
   if (cboConnection.getselectedItem().equals("Server")){
    btnConnect.setText("ON");
    txtUser.setText("server");
} else {
    btnConnext.setText("Hubungkan !");
    txtUser.setText("Client");
}
}



Code:
private void btnconnectactionperformed(java.awt.event,actionevent evt){
    if(btnconnect.gettext().equals("Menghubungkan !")){
    btnconnect.settext("Putuskan !");
    client_connection();
    thread thread = new thread (runnable) this);
    thread.start();
}
else if (cboconnection.getselecteditem().equals("Server")){
    btnConnect.setText("OFF");
    read_connection();
    thread thread = new thread (this);
    thread.start();
}

Code:
private void btnsendactionperformed(java.awt.event.actionevent evt){
    try{
    server_writer.writer(textUser.gettext() + ": " + txtmessege.gettext());
    server_writer.newline();
    server_writer.flush();
}catch (IOException e){
    logger.getlogger(Chatme.class.getname()).log(Level.SEVERE, null, e);
}
listmessege.add("me: "+txtmessege.gettext());
txtmessege.settext("");
}



HASIL JADI SERTA DEMO




VIDEO DEMO


[/HIDE]



S
ELESAI SEMOGA BERMANFAAT
:)
 
Terakhir diubah:
Jav Toys
Gaple Online Indonesia
Pasang iklan hanya lewat CONTACT US
Back
Top
We are now part of LS Media Ltd