Server Client program in java

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
Socket Client Program code :
//Socket Client Program
import java.net.*;
import java.io.*;  

public class SocketClient {

    public static void main(String args[])
    {
     try
     {
      Socket client = new Socket("localhost", 3026);
      DataOutputStream out = new DataOutputStream(client.getOutputStream());
      out.writeUTF("Hello Sir!");
      out.writeUTF("How are you?");
      out.writeUTF("Done sending.");
      DataInputStream in = new DataInputStream(client.getInputStream());
      String message;
   while((message = in.readUTF()).equals("Done sending.") != true)
    System.out.println(message);
         client.close();
     }
     catch(Exception e)
     {
      e.printStackTrace();
     }
    }
}
Socket Server code :
//Socket Server Program 
import java.net.*;
import java.io.*; 

public class SocketClient {

    public static void main(String args[])
    {
     try
     {
      Socket client = new Socket("localhost", 3026);
      DataOutputStream out = new DataOutputStream(client.getOutputStream());
      out.writeUTF("Hello Sir!");
      out.writeUTF("How are you?");
      out.writeUTF("Done sending.");
      DataInputStream in = new DataInputStream(client.getInputStream());
      String message;
   while((message = in.readUTF()).equals("Done sending.") != true)
    System.out.println(message);
         client.close();
     }
     catch(Exception e)
     {
      e.printStackTrace();
     }
    }
}
output:-
Hello Sir!
How are you?
Time Out!
--------------------------------

simple server client program :

Socket Client Program code :
//Socket Client Program
import java.io.*;
import java.net.*;
 public class MyClient {  
     public static void main(String args[]){
         try{
          Socket   socket = new Socket("localhost", 25000);
             BufferedReader inp = new BufferedReader (new InputStreamReader(System.in)); 
                String input = inp.readLine();          // to take input from USER   
                input = input + "\n";           
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
             bw.write(input);             // pass to server
             bw.flush();
             System.out.println("Message sent to the server : "+input);
                BufferedReader br = new BufferedReader (new InputStreamReader(socket.getInputStream()));
             String message = br.readLine();         // accept value from  server
             System.out.println("Message received from the server : " +message);
         }
         catch(Exception e){
             System.out.println(e);
         }
     }
 }
Socket Server code :
//Socket Server Program 
import java.net.*;
import java.io.*; 
 public class MyServer {
     public static void main(String[] args) {
         try{
          Socket socket;
             ServerSocket serverSocket = new ServerSocket(25000);
             System.out.println("Server Started and listening to the port 25000");
             while(true) {
                 socket = serverSocket.accept();
                 BufferedReader br = new BufferedReader (new InputStreamReader(socket.getInputStream())); 
                 String output = br.readLine();       //receive value from client
                 System.out.println("Message received from client is "+output);
                 int x = Integer.parseInt(output);
                 output = String.valueOf(x*2) + "\n";
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                 bw.write(output);         //pass value to client
                 System.out.println("Message sent to the client is "+output);
                 bw.flush();   
             }
         }
         catch(Exception e){
             System.out.println(e);
         }  
     }
 }
output:-
2
Message sent to the server : 2

Message received from the server : 4
--------------------------------

0 comments: