Client and Server Socket programming in java | java network programming | Tech With Code

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- 
  1. MyServer.java 
  2. 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:-

After running the above program, you will get the following two outputs-




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

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

Follow us on Instagram
Follow us on Facebook
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.

                               Thank you.

                                        ( नमस्ते )

                                     🙏 

No comments:

Powered by Blogger.