Today I’ll be super busy preparing the order of a true legend — @Joseinnewworld, who just swept around 58 #NFTs 😳🔥 That’s insane support… looks like I’ll be pulling an all-nighter to get everything ready 😅🙌 #NFT #NFTCollection #NFTCollectors #eCash $XEC #CryptoMevXBOT https://t.co/5iHxVEGjRo pic.twitter.com/xjpvlw34L6
— NFToa (@nftoa_) August 19, 2025
On this occasion, we were given an assignment by Mr. Untung Subagyo, S.Kom as a lecturer in charge of Network Programming, to study socket programming used to send text messages from client applications to servers, and vice versa. The assignment was published on his blog .

Learn Socket Programming
However, before we move to the coding stage, it would be good if we first get to know some of the Java statements / classes that we will use later. What the saying says is true, "DON'T KNOW THEN YOU DON'T LOVE"
1. Understanding the Use of Library / Datagram and Class Packages
import
This statement is used to import functions/methods, objects/classes, or primitive data types, which are found in certain library packages.
java.io
Is a library package that is needed to perform input / output (I / O) data in Java. All forms of streams always represent input sources and output destinations. Well, so is the Stream (flow) package of the java.io library which is defined by;
- InputStream, which is a class used to read data from a source (variable).
- OutputStream, which is a class used to write data to a destination (variable).
In addition, the java.io package also supports a lot of data such as primitives, objects, local characters, etc.
InputStream
It is an abstract class as a superclass that represents a byte input stream. Applications that define subclasses of InputStream must always include a method that returns the next byte of the input.
getInputStream()
This method is used to return an InputStream representing the data and the thrown exception (throws IOException). - Attention! a new InputStream object must be returned each time this method is called, and the stream must be positioned at the beginning of the data.
OutputStream
It is an abstract class as a superclass that represents a byte output stream. Applications that define a subclass of OutputStream must always include a method that writes at least one byte to the output.
getOutputStream()
This method is used to return an OutputStream, which represents the data written and the exception thrown (throws IOException). Caution! a new OutputStream object must be returned each time this method is called, and the stream must be positioned at the location of the data to be written.
java.net
It is a library package of the J2SE API that contains a collection of classes and interfaces that provide low-level communication details.
Network programming refers to writing programs that execute across multiple devices (computers), all of which are connected to each other by a network.
The java.net library package provides support for two network protocols such as:
- TCP - TCP, stands for Transmission control Protocol, which allows reliable communication between two applications. TCP is commonly used via Internet Protocol, hence the name TCP/IP.
- UDP - UDP, stands for User Datagram Protocol, which is a connection-less protocol that allows data packets to be transmitted to many applications.
Socket
It is a class in the java.net package that implements one side of a two-way connection between a Java program and another program on the network. Its format is → Socket(String host, int port).
byte[]
Retrieves all or part of a BLOB value, where the BLOB is a representation of a byte array object.
try ... catch
Is a statement in Java that is used to handle error messages, and can continue the program without having to stop because of an error. Errors handled by try..catch can be called exceptions. The format is → try{} catch(){}
IOException
It is an I/O exception from the program flow. It is a general class of exception created to handle I/O operation failures.
Exception is a condition that causes a program to hang or exit (quit) from the normal flow that has been determined when the program is run. This exception is triggered by a Runtime Error, which is an error that occurs when the program is executed by the interpreter. For example:
- Entering character data when requesting input in the form of numeric data
- Division by zero occurs
- The path or file location provided is incorrect.
- Operation to access array variable at index number out of bounds
read()
Used to read the next byte from the input stream. The byte value is returned as an integer ranging from 0 to 255. If there are no more bytes because the reading process has reached the end of the stream, then the value -1 is returned. This Method block will be executed from the input data available until the end of the stream, or until an exception.
write()
Used to write specified bytes to the OutputStream.
java.util.Arrays.fill(int[] a, int val)
A method that assigns a specific int value to each element of a given array.
Parameter:
- a → Array to be filled
- val → The value to be stored in all elements of the array.
string.trim()
Called Java String Trim, it is a method used to remove spaces or eliminate certain characters in a string. You can see the demo HERE and HERE .
string.equals()
The method used to compare a string to a specified object. The result will be true if and only if the argument is not null and is a string object that represents the same sequence of characters as the object.
2. Getting Started with Coding
In this case, I assume that all of you have installed Netbeans IDE, so please create the following project;
2.a. Create a Client Project
package com.gatewan.client;
import java.io.*;
import java.net.*;
public class ComGatewanClient {
public static void main(String[] args) throws IOException {
// TODO code application logic here
Socket client = null; //deklarasi objek socket dengan nama client
InputStream in = null; //deklarasi objek InputStream dengan nama in
OutputStream out = null; //deklarasi objek OutputStream dengan nama out
byte[] receiveMessage = new byte [64];
try {
client = new Socket ("localhost",8881); /*untuk membuat sebuah stream socket dan juga koneksi ke
suatu port tertentu pada sebuah komputer berdasar namanya.
Formatnya adalah --> Socket(String host, int port)
*/
in=client.getInputStream(); //mengisi objek in dengan input dari client
out=client.getOutputStream(); //menulis obje
} catch (IOException e) {
System.out.println(e.getMessage());System.exit(-1);
}
/*try..catch adalah statement pada java yang digunakan untuk menangani pesan error,
dan dapat melanjutkan jalannya program tanpa harus berhenti karena error.
Error yang ditangani try..catch bisa disebut dengan exeption.*/
String fromServer;
String fromUser;
in.read(receiveMessage);
fromServer = new String(receiveMessage);
System.out.println("Server:"+fromServer);
fromUser = "Salam dari Client";
System.out.println("Kirim ke server :"+fromUser);
out.write(fromUser.getBytes());
fromUser = "Ini data dari Client";
System.out.println("Kirim ke server :"+fromUser);
out.write(fromUser.getBytes());
fromUser = "Keluar";
System.out.println("Kirim ke server :"+fromUser);
out.write(fromUser.getBytes());
out.close();in.close();client.close();
}
}2.b. Creating a Project Server
package com.gatewan.server;
import java.io.*;
import java.net.*;
public class ComGatewanServer {
public static void main(String[] args) throws IOException{
// TODO code application logic here
ServerSocket server = null;
Socket client = null;
byte[] receiveBuf = new byte[64];
int receiveMesssageSize;
try {
server = new ServerSocket(8881);
System.out.println("Server Started");
client = server.accept();
System.out.println("Client terkoneksi");
} catch (IOException e) {
System.out.println(e.getMessage()); System.exit(-1);
}
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
String data;
data = "Salam dari server";
out.write(data.getBytes());
java.util.Arrays.fill(receiveBuf, (byte)0);
while(true){
receiveMesssageSize = in.read(receiveBuf);
data = new String (receiveBuf);
if(data.trim().equals("Keluar")){
out.write(data.getBytes());break;
}
java.util.Arrays.fill(receiveBuf, (byte)0);
System.out.println("Client :" +data);
}
out.close();in.close();client.close();server.close();
}
}3. Program Execution
The first thing to run is the Server, then the Client. If you reverse this step, then the Client will error because there is no communication partner.
Server is running
Client is running
Server Response
This is an example of execution with a wrong rule. The client contacts a dead server.
Client cannot communicate with Server is DEAD
Reference:
- https://pemprogramanjaringan.wordpress.com/2017/03/08/first-blog-post/
- http://yuliana.lecturer.pens.ac.id/Prog%20Lanjut/Networking/tutorial%20Socket.pdf
- http://felix.staff.unisbank.ac.id/files/2011/03/BPP-Pemprograman-Jaringan.pdf
- https://www.codepolitan.com/coba-statement-try-catch-di-java
- https://7seasons.wordpress.com/tag/try-catch-java/
- http://www.webbasedprogramming.com/Java-Unleashed-Second-Edition/ch26.htm
- https://www.tutorialspoint.com/java/java_files_io.htm
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
- https://www.tutorialspoint.com/java/java_networking.htm
- https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
- https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Blob.html
- http://rahmakekeys.blogspot.co.id/2013/04/besar-input-output-stream-pada-java.html
- https://docs.oracle.com/javase/7/docs/api/javax/activation/DataSource.html
- https://www.tutorialspoint.com/java/util/arrays_fill_int.htm
- http://javaprogramming26.blogspot.co.id/2010/06/stream-exception.html
- http://www.javatpoint.com/java-string-trim
- https://www.tutorialspoint.com/java/java_string_equals.htm
