contoh simple aplikasi chat menggunakan java dengan pemrograman socket

HelloServer.java
——————————————————————————————————–


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

public class HelloServer {
 public static void main(String[] args) throws IOException {
 ServerSocket server = new ServerSocket(9999);
 System.out.println("Server Ready ");
 Socket clientSocket = server.accept();

BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream());

String line = is.readLine();
 System.out.print("We received: " + line);

os.writeBytes("Hello too Client !!! \n");
 os.close();
 is.close();
 clientSocket.close();

}
 }

———————————————————————————————————

HelloClient.java
——————————————————————————————————–


import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.net.Socket;

public class HelloClient

{
 public static void main(String[] args)
 throws IOException,NumberFormatException{
 PrintWriter out;
 BufferedReader in;
 Socket client;
 if(args.length==0)
 client=new Socket("alfi-eeepc",9999);
 else
 client=new Socket(args[0],Integer.parseInt(args[1]));
 in = new BufferedReader (new InputStreamReader(client.getInputStream()));
 out = new PrintWriter(client.getOutputStream());

out.println("Hello Server dari syafaat !!!!");
 out.flush();
 System.out.print(in.readLine());
 in.close();
 out.close();
 client.close();
 }
 }

———————————————————————————————————