Pemrograman Soket Java Tanpa GUI
04.26.001. Program Server
import java.io.*;
import java.net.*;
public class ProgramServer {
public static void main(String[]args){
try{
ServerSocket a=new ServerSocket(8000);
System.out.print("Server dimulai pukul "+ new java.util.Date() + '\n');
Socket soket =a.accept();
DataInputStream masukanClient = new DataInputStream(
soket.getInputStream());
DataOutputStream keluaranDiClient = new DataOutputStream(
soket.getOutputStream());
while(true){
double jari2 = masukanClient.readDouble();
double luas = jari2 * jari2 * 22 / 7;
keluaranDiClient.writeDouble(luas);
System.out.println("Jari-jari diterima dari client : " + jari2 + "\n");
System.out.println("Luas nya : " + luas +"\n");
}
}catch(IOException ex){
System.err.println(ex);
}
}
}
2. Program Client
import java.io.*; //mengambil paket java.io.*
import java.net.*; //mengambil paket java.net.*
import java.util.Scanner; //mengambil paket java.util.Scanner
public class ProgramClient {
private DataOutputStream keServer;
private DataInputStream dariServer;
public static void main(String[]args){
new ProgramClient();
}
public ProgramClient(){
try{
Socket soket = new Socket("localhost",8000);
dariServer = new DataInputStream(soket.getInputStream());
keServer = new DataOutputStream(soket.getOutputStream());
Scanner sken = new Scanner(System.in);
System.out.print("masukkan jari2 : ");
double jari2 = Double.parseDouble(sken.next());
keServer.writeDouble(jari2);
keServer.flush();
double luas = dariServer.readDouble();
System.out.println("Jari-jarinya : "+jari2+"\n");
System.out.println("Luas yang diterima dari server : "+luas+"\n");
}catch(IOException ex){
System.out.println(ex.toString()+"\n");
}
}
}
3. ScreenShoot Program
4. Penjelasan Program
Download PDF
0 komentar