Java2all.com  




Share This Topic -

java socket programming example

Two variable addition and passing message from client to server two different java socket programming example is given below but before going through program directly you should have some knowledge about Java Network Programming and Socket.

These both things are already available in previous chapter so you can learn from there.

Now let`s move to program 1.

Program: Write down a program for addition the two different variable by Socket programming.
 
Client_Addition.java:
 

 

Server_Addition.java

 

For Output follow the below step:

(1) Run Server_Addition.java
 
Console:
 
Server run successfully......
 
(2) Run Client.java
 
Console:
 
Enter first value: 
5
Enter second value: 
25
 
(3) Now, press Enter
 
(4) Server run successfully......
Addition: 30
 
java socket programming example 2:
 
Program: Write down a program which demonstrate the Socket programming for passing the message from client to server and also apply EXIT properties.
 
Client.java:

 

Server.java:

 

For Output follow the below step:
 
(1) Put the both file in the bin folder at jdk.
 
For example: C:\Program Files (x86)\Java\jdk1.6.0\bin.
 
(2) Open Command Prompt & reach up to bin path.
 
 

java socket programming example

 
(3) Compile the Server.java & Client.java
 
…\bin>javac Server.java
 
…\bin>javac Client.java
 
(4) Run the Server.java
 
…\bin>java Server
 
 

java socket programming example

 

(5) Open new command prompt:
(6) Now revise step-2.
(7) Run the Client.java.
 
…\bin>java Client
 
 

java socket programming example

 
 

java socket programming example

 
Check the Message at Server Side Command Prompt.
 
(8) Write down the message on Client Side Command Prompt Like:
Input the data…
Ashutosh
 
(9) Now Press Enter & Check the Output at Both Windows.
 

java socket programming example

 

(10) If want to Exit then type exit on Client side Window.

Like: Input the data…
exit
 
 
java socket programming example