Aplikasi Client Server menggunakan java dengan metode socket:
Source Code untuk Server adalah sbb:
Source Code untuk Server adalah sbb:
package client_server;
/**
*
* @author aditia
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client_Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
//inisial objek server soket denga meset port 51111
ServerSocket ss = new ServerSocket(5111);
Socket s;
String balasan;
String balasan2;
//inisialisasi objek input dan ouput stream
ObjectInputStream in = null;
ObjectOutputStream out = null;
//fungsi bawaan dari class server sket yang mereturn objek soket
//tanda clent server terhubung
s = ss.accept();
//alokasi memori objek
out = new ObjectOutputStream(s.getOutputStream());
//fungsi bawaan objek output stream untuk membersihkan isi memory
out.flush();
//alokasi memory
in = new ObjectInputStream(s.getInputStream());
balasan = “”;
balasan2 = “”;
while (true) {
balasan = “”;
balasan2 = “”;
balasan=ReadMassage(in);
//program akan berhenti sampai input exit dari klien
if(balasan.equals(“exit”)) break;
else {
if(balasan.equals(“ini client”)) {
SendMassage(“ini server, masukan username”, out);
balasan2 = ReadMassage(in);
if(balasan2.equals(“admin”)) SendMassage(“Selamat datang admin”, out);
else
{
if (balasan2.equals(“user”)) SendMassage(“Selamat datang user”, out);
else SendMassage(“username tidak diketahui”, out);
}
} else {
SendMassage(“perintah tidak diketahui”, out);
}
}
}
//Menutup semua stream
in.close();
out.close();
s.close();
ss.close();
} catch (IOException ex) {
Logger.getLogger(Client_Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
//fungsi membaca input dari client
public static String ReadMassage(ObjectInputStream in)
{
String reader = “”;
try {
reader=(String) in.readObject();
} catch (IOException ex) {
Logger.getLogger(Client_Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client_Server.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(reader);
//mereturn nilai string hasil bacaan
return reader;
}
//Fungsi untuk mengirim objek /data ke klient
public static void SendMassage(String word, ObjectOutputStream out)
{
try {
out.writeObject(word);
//bersihkan memori
out.flush();
} catch (IOException ex) {
Logger.getLogger(Client_Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Source code untuk client:
package client;
import java.util.Scanner;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author aditia
*/
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Socket s = new Socket(“localhost”,5111);
ObjectInputStream in = null;
ObjectOutputStream out = null;
Scanner scanf = new Scanner(System.in);
String input=”";
String balasan;
String balasan2;
out=new ObjectOutputStream(s.getOutputStream());
out.flush();
in=new ObjectInputStream(s.getInputStream());
balasan=”";
balasan2=”";
while(true)
{
balasan=”";
balasan2=”";
input = scanf.nextLine();
SendMessage(input,out);
if(input.equals(“exit”))break;
else
{
balasan=ReadMessage(in);
if(balasan.equals(“ini server, masukkan username”))
{
input=scanf.nextLine();
SendMessage(input,out);
balasan2=ReadMessage(in);
}
}
}
in.close();
out.close();
s.close();
}
public static void SendMessage(String word, ObjectOutputStream out)
{
try{
out.writeObject(word);
out.flush();
}catch(IOException ex){
Logger.getLogger(Client.class.getName()).log(Level.SEVERE,null,ex);
}
}
public static String ReadMessage(ObjectInputStream in)
{
String reader=”";
try{
reader=(String)in.readObject();
}catch(IOException ex){
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null,ex);
}catch(ClassNotFoundException ex){
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null,ex);
}
System.out.println(reader);
return reader;
}
}
Hasil running dengan console yang disediakan Netbeans:
Komentar
Posting Komentar