Now Access to Gemini (Google) : Really beat to ChatGPT ?

Client - Server Programming - Java : Network Programming

Java client Server(Socket Programming)programming is used for communication between the applications running on different JRE.

Here we are going to make one-way client-server programming. In this application  First, we establish the connection between client and server. Next, send the message from client to server and server-side will display message.



Note : Learn more about class that used in this program Just click Java Networking Documentation

Client program : 

 import java.io.*;  

import java.net.*;  

class Client {  

public static void main(String[] args) {  

try{      

Socket s=new Socket("127.0.0.1",6666);  

DataOutputStream dout=new DataOutputStream(s.getOutputStream());  

dout.writeUTF("Hello World");  

dout.flush();  

dout.close();  

s.close();  

}catch(Exception e){System.out.println(e);}  

}  

}  


Server Program :

import java.io.*;  

import java.net.*;  

 class Server {  

public static void main(String[] args){  

try{  

ServerSocket ss=new ServerSocket(5000);  

Socket s=ss.accept();//establishes connection   

DataInputStream dis=new DataInputStream(s.getInputStream());  

String  str=(String)dis.readUTF();  

System.out.println("message= "+str);  

ss.close();  

}catch(Exception e){System.out.println(e);}  

}  

}  

Follow Instruction : 

First compile both program. 

Second , Open two command prompt , 

Next , first run server program and second , run client program ,


 

 More Questions : 

     - How to send Mail in Java

     - How to make To-Do List in React

     - How to Use Bootstrap in React

     - How to run python command in java

     -Bootstrap sidebar 



If you have any queries, you can put your question in a comment.





Comments