Skip to main content

A Simple Java TCP Server

Here is a simple example of TCP Server using java. It gives you idea about how TCP server opens up a port and receives message from TCP client.

To compile this code, simply install Java JDK to your computer. Then save this code with file name Server.java and then open terminal. Change your directory to where you save this file and type in javac Server.java - this will create new file Server.class, don't do anything with .class file unless you'll get an error: "Exception in thread "main" java.lang.NoClassDefFoundError". And then type in terminal java Server .

 import java.io.*;  
 import java.net.*;  
 import java.util.Scanner;  
   
 class Server{  
  private static Server server;  
  static int serverPort = 1234;  
  static ServerSocket welcomeSocket;  
    
  public Server(){  
  try{  
   welcomeSocket = new ServerSocket(setSocket());  
  }catch(Exception e){  
   System.out.println("Error: "+e.getMessage());  
  }  
  }  
    
  private int setSocket(){  
  Scanner in = new Scanner(System.in);  
  System.out.print("Enter socket number:");  
  serverPort = in.nextInt();  
  System.out.println("Socket number "+serverPort+" created.");  
  System.out.println("Listening...");  
  return serverPort;  
  }  
    
  public static void main(String argv[]) throws Exception {  
  server = new Server();  
  String clientSentence;       
  String capitalizedSentence;       
  while(true){  
   Socket connectionSocket = welcomeSocket.accept();         
   BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));  
   DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());  
   clientSentence = inFromClient.readLine();  
   System.out.println("Received: " + clientSentence);         
   capitalizedSentence = clientSentence.toUpperCase() + " from "+serverPort+'\n';         
   outToClient.writeBytes(capitalizedSentence);       
  }   
  }  
 }  


Next post will be a simple Java TCP Client.

#source: https://systembash.com/a-simple-java-tcp-server-and-tcp-client/

Comments

Popular posts from this blog

Korps Dai Mahasiswa (KDM) Salman ITB

Dai Corps Students, or shortened to KDM Salman ITB , is a recitation of activities held at Salman Mosque and intended for ITB students and also public. Located in the South Corridor Salman Mosque, KDM Salman ITB is trying to bring teaching activities with the concept of traditional pesantren (traditional boarding school). With meetings every day after dhuhur, the students who take part in this Salman KDM get the study material by using the yellow books (kitab kuning) as a handle, as in the traditional pesantren. Teachers who bring the study also vary depending on the items of its study, they are ustadzs from Salman mosque itself. KDM Salman ITB appears to address concerns some students, as well as some ustadzs in mosques Salman, who feel that it is necessary to foster student containers in religious matters. It is motivated by the proliferation of such activities 'mentoring' on campus. But the mentor who brought the material (ie students) were deemed less or even not ...

[Arduino Project-1] -- PushButton Lamp

Apa sih Arduino? Salam, Pada posting kali ini akan menjelaskan tentang tugas kuliah Interaksi Manusia dengan Komputer dan Antar Muka, yaitu membuat lampu push button menggunakan Arduino. Saya anggap pembaca telah mengetahui apa itu arduino. Jika belum tahu, akan saya jelaskan sedikit. Arduino bisa dikatakan sebagai alat untuk membuat sebuah prototye. Kita bisa  membuat apapun (hampir apapun) dengan menggunakan arduino. Seperti membuat sensor panas/dingin,bahkan hingga membuat robot. Arduino sebenarnya diperuntukkan bagi para artist maupun designer yang memerlukan kemudahan dalam merancang produk yang hendak mereka buat. Jadi, untuk menggunakannya tidak diperlukan keterampilan khusus yang tinggi karena di design untuk kemudahan (tentunya setelah tahu dasar-dasarnya). Seperti apa sih Arduino itu? Di bawah ini gambarnya Image Source: https://dlnmh9ip6v2uc.cloudfront.net//images/products/1/1/0/2/1/11021-01a.jpg Arduino ada banyak jenisnya yang bisa dilihat...

[Arduino Project -- 5] -- Using Keypad and LCD Screen at Once to make simple Calculator

Good day. This is my fifth times of my post about Arduino. We've learned about how to create something using Arduino from the very beginning that was just make a simple push button, till we made something more difficult which was creating a temperature censor and displaying the result in the LCD screen. And now, i'm gonna show you how to use a keypad and LCD Screen at once. With these two devices are combined with our Arduino, we are able to make something like a little application. And now, i'm gonna make a simple calculator using them. Stuffs: These are stuffs you are gonna need to prepare An Arduino Uno A BreadBoard Jumper Cables A Keypad A PC which has Arduino App Installed A Potensiometer A Resistor What do we do? First thing we gotta do is to wire our Arduino and keypad as shown below: Actually we dont need the speaker and the led. But I put them when i wired them. And here is my work: It was a little bit confusing i thought. And aft...