Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

LAPORAN PRAKTIKUM

PEMOGRAMAN JARINGAN

NAMA : JULIANSYAH

NIM : 210504047

UNIT : 01

SEMESTER : 6/GENAP

MATA KULIAH : PEMOGRAMAN JARINGAN

DOSEN : MUNAWIR S.S.T.,M.T

Pratikum 1
Percobaan untuk mengetahui IP Address Localhost dan NamaHost

-Kodingan
-hasilnya:
Praktikum 2
Percobaan Program Socket Client-Server TCP via Localhost

Kodingan Program Server:

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

public class TCPServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(1234); // Port yang digunakan
System.out.println("Server is running...");

while (true) {
Socket clientSocket = serverSocket.accept(); // Menerima koneksi dari client
System.out.println("Client connected: " +
clientSocket.getInetAddress().getHostAddress());

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

String message = in.readLine(); // Menerima pesan dari client


System.out.println("Client sent: " + message);

out.println("Server received: " + message); // Mengirim balasan ke client

clientSocket.close(); // Tutup koneksi dengan client


}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Kodingan Program Client:

import java.net.*;
import java.io.*;
import java.util.*; // Corrected typo in import statement

public class TCPEchoClient {


private static InetAddress host; // Corrected typo in variable name
private static final int PORT = 2480;

public static void main(String[] args) { // Corrected typo in parameter type


try {
host = InetAddress.getByName("192.168.137.149");
} catch (UnknownHostException uhEx) {
System.out.println("Host ID not found!");
System.exit(1);
}
accessServer();
}

private static void accessServer() {


Socket link = null; // Corrected typo in class name
try {
link = new Socket(host, PORT);

Scanner input = new Scanner(link.getInputStream());


PrintWriter output = new PrintWriter(link.getOutputStream(), true);

// Set up stream for keyboard entry...


Scanner userEntry = new Scanner(System.in);
String message, response;
do {
System.out.print("Client> Enter message: ");
message = userEntry.nextLine();
output.println(message);

if (!message.equals("QUIT")) {
response = input.nextLine();
System.out.println("\nServer> " + response);
}
} while (!message.equals("QUIT"));

// Close the connection after the loop


System.out.println("\n* Closing Connection... *");
link.close();
} catch (IOException ioEx) {
ioEx.printStackTrace(); // Print the stack trace for debugging purposes
}
}
}
Hasilnya:
Laporan 3
Percobaan Client-Server TCP (2 komputer )

Kodingan Server:
Hasil Server

Kodingan Client:
Hasil Client

You might also like