Skip to main content

A Simple Java TCP Client

As followed my post about simple java TCP server and I promised to post about simple java TCP client, so here it is. If you haven't read my simple java TCP server already, then why don't you give it a go. There you'll find how to compile java code that I will not be covering in this post. So go ahead to this post and get back here as soon as you know how to compile java code. If you already know how to do it, then it's okay to continue to read this post.
import java.io.*;
import java.net.*;
import java.util.Scanner;

class Client{
    private static Client client;
    private static int socketNumber = 1234;
    private static Socket clientSocket;

    public Client(){
        try{
            clientSocket = new Socket("localhost", getSocket());
        }catch(Exception e){
            System.out.println("Error."+e.getMessage());
        }
    }

    private int getSocket(){
        Scanner in = new Scanner(System.in);
        System.out.print("Input socket server: ");
        socketNumber = in.nextInt();
        System.out.println("Socket server "+socketNumber+", Ready to send message");
        return socketNumber;
    }

    public static void main(String argv[]) throws Exception{
        client = new Client();
        String sentence;  
        String modifiedSentence;   
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));   
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());   
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));   
        sentence = inFromUser.readLine();   
        outToServer.writeBytes(sentence + '\n');   
        modifiedSentence = inFromServer.readLine();   
        System.out.println("FROM SERVER: " + modifiedSentence);   
        clientSocket.close();
    }
} 
Compile and run the code.
To test this out, we need to run the server as well so go ahead open another terminal and run the server. Put the socket port you want your server will be using and press ENTER Then in your client, put the server port that you just typed in and press ENTER. Now you'll see "Ready to send message" message, it indicates that both client and server are now connected.
Just type something from client then you'll see your message will be appeared in the server side.

To test our server from a different computer, we need know our PC ip address. To find out our IP address, simply type in terminal ipconfig for windows or ifconfig for ubuntu and our IP address is shown next to inet label.
After we know our PC's IP,  open terminal from another computer which runs ubuntu and type in telnet <our ip address> <server port> and we're ready to send message to server.

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

Comments

Popular posts from this blog

[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

[Arduino Project-2] -- Lampu Fade in - Fade out dengan PWM

Good Day, Semoga hari-hari selalu menjadi hari baik bagi kita semua. Masih kesengsem sama Arduino, pada postingan kali ini saya akan menjelaskan (kalau bisa disebut) mempraktikan cara membuat lampu LED yang bisa nyala terang dan gelap (fade-in / fade-out ) dengan PWM. Yang akan kita lakukan adalah membuat lampu LED berubah tingkat terangnya (brightness). Sebelumnya kita perlu tahu apa itu PWM. PWM atau Pulse Width Modulation adalah teknik yang dilakukan untuk mengubah tingkat ke terang an (brightness) LED dengan cara mengatur waktu delay. Jika kita memberikan delay pada LED dengan Arduino maka lampu tersebut akan berkedip. Akan tetapi jika delay sangat cepat, maka lampu tersebut terlihat tidak berkedip lagi, melainkan kita bisa mengatur tingkat terang (brightness) lampu tersebut. Jika delay yang sangat cepat tersebut diturunkan kecepatannya sedikit, maka yang terlihat adalah brightness lampu yang turun. Begitu pula sebaliknya. Jika membingungkan, Anda tidak perlu mengingatnya. Baiklah

[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 di