create a java program for Two Way Chat Application using Java Socket Programming
import java.io.*;
import java.net.*;
public class Server{
public static void main(String[] args){
try{
ServerSocket server_socket=new ServerSocket(5555);
Socket socket=server_socket.accept();
DataInputStream d=new DataInputStream(socket.getInputStream());
String str1=(String)d.readUTF();
System.out.println("Message: "+str1);
server_socket.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args) {
try{
Socket socket=new Socket("localhost",5555);
DataOutputStream d2=new DataOutputStream(socket.getOutputStream());
d2.writeUTF("Hello Server");
d2.flush();
d2.close();
socket.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
Comments
Leave a comment