socket programming in java | java network programming tutorial | tech with code

socket programming in java:

If you are just starting Network or Socket programming java, you should visit this first


Let's Continue with our Topic

Today we will create a connection between client and server such that Client and Server both will sending and receiving data from each other. When anyone ( Client or Server) receives any data they will invert the data and Print that on the console.


Source Code For Server:-     MyServer.java

FileName Should be MyServer.java

import java.net.*;
import java.io.*;

 class MyClass{
    public String invert(String st){
        
        StringBuilder sb =new StringBuilder();
        for(int i=0;i<st.length();i++){
            char c = st.charAt(i);
            int temp = (int) c;
            if(temp <= 90 && temp >= 65 )
                temp= temp + 32;
                            
            
            else if(temp <= 122 && temp >= 97 )
                temp= temp - 32;
            
            c=(char) temp;
            sb.append(c);
        }
        st=sb.toString();
        return st;
    }
    
}
public class MyServer {

    public static void main(String args[]) throws IOException {
        ServerSocket s = new ServerSocket(50136);
        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 ,i am server");
        //Server is Recieving the data
        DataInputStream dis = new DataInputStream(s1.getInputStream());
        String st = new String(dis.readUTF());
        MyClass ob = new MyClass();
        st = ob.invert(st);
        System.out.println(st);
        dos.close();
        s1.close();
        s.close();

    }

}

Source Code For Client:-     MyClient.java

FileName Should be MyServer.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("Connection established");
        DataInputStream dis = new DataInputStream(s1.getInputStream());
        String st = new String(dis.readUTF());
        MyClass ob = new MyClass();
        st = ob.invert(st);
        System.out.println(st);
        //Client is sending data
        DataOutputStream dos = new DataOutputStream(s1.getOutputStream());
        dos.writeUTF("Hi There ,i am Client");        
        dis.close();
        
        s1.close();
    }
}


output of the above program:-





Final Word:-

In this article, We have shown you about the "Today we have created a connection between client and server such that Client and Server both will send and receive data from each other. When anyone ( Client or Server) receives any data they will invert the data and Print that on the console.".We hope you like it

If you have done this by a different approach or you have implemented something in any language please comment down below and share your code with us.we will publish that with your name.

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.