Palindrome Check - Client-Server Program - Network-DBMS Lab - Java program


Program:

//ServerCode2 .java

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

public class ServerCode2 {
ServerSocket ss;
Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ServerCode2()
    {
    try{
        ss=new ServerSocket(8765);
        System.out.println("Server is listening port 8765");
        socket=ss.accept();
        System.out.println("Connection established...");
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {
        System.out.println("Msg from client");
        str=sock_in.readLine();
        int k=str.length();
        System.out.println(str);
        int left=0,right=k-1;int flag=1;
        while(left<=right)
        {
            if(str.charAt(left)!=(str.charAt(right)))
            {
                flag=0;
                break;
            }
            else
            {
                left++;right--;
            }
        }
        if(flag==1)
            str="....Palindrome....";
        else
            str="....Not Palindrome....";
        //System.out.println("Enter the msg");
        //str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        if(str.equals("bye"))
            break;
        }
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ServerCode2();
}}


//ClientCode.java

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

public class ClientCode{

Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ClientCode()
    {
    try{

        Socket socket=new Socket("127.0.0.1",8765);
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {
       
        System.out.println("Enter the msg");
        str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        System.out.println("Msg from Server");
        str=sock_in.readLine();
        System.out.println(str);
        if(str.equals("bye"))
            break;
        }
        socket.close();
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ClientCode();
}}

Output:

Terminal 1 (Server):

students@ccflab-desktop:~$ javac ServerCode2.java
students@ccflab-desktop:~$ java ServerCode2
Server is listening port 8765
Connection established...
Msg from client
malayalam
Msg from client
abc
Msg from client

Terminal 2(Client):

students@ccflab-desktop:~$ javac ClientCode.java
students@ccflab-desktop:~$ java ClientCode
Enter the msg
malayalam
Msg from Server
....Palindrome....
Enter the msg
abc
Msg from Server
....Not Palindrome....
Enter the msg

1 comment:

  1. hello, I need the program in C. CAn u mail me the program for client server palindrome in "C". e-mail id: sumiyashaikh@gmail.com

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...