Making Connection between Client and Server using java programming:
Source Code For Establish Connection between Client and Server:-
To establish the connection, you need two files-
- MyServer.java
- MyClient.java
1. MyServer.java
import java.net.*; import java.io.*; public class MyServer { public static void main(String[] args) throws IOException{ ServerSocket s = new ServerSocket(50125); System.out.println("This is server Side"); Socket s1 = s.accept(); System.out.println("Sever is connected to client"); //Server is sending data DataOutputStream dos = new DataOutputStream(s1.getOutputStream()); dos.writeUTF("Hi There"); dos.close(); s1.close(); s.close(); } }
2. MyClient.java
import java.net.*; import java.io.*; public class MyClient { public static void main(String args[]) throws IOException{ Socket s1= new Socket("localhost",50125); System.out.println("This is Client Side"); System.out.println("Connection established"); DataInputStream dis = new DataInputStream(s1.getInputStream()); System.out.println("Following Data sent by Server"); String st = new String(dis.readUTF()); System.out.println(st); dis.close(); s1.close(); } }
OutPut of JAVA program For Establish Connection between Client and Server:-
Final Word:-
In this article, We have shown you about the "How to implement Client and Server Connection using Socket programming in java ".We hope you like it
In this article, We have shown you about the "How to implement Client and Server Connection using Socket programming in java ".We hope you like it
If This post helps you a little bit then please share this post with your friends. For more Computer and programming Related Tips and tricks, please
Subscribe us on Youtube
Follow us on Pinterest
Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.
scvzscvzsx
ReplyDeleteThank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. thoptv pc, thop tv for windows
ReplyDelete