Write a client server program using TCP where client sends a string and server checks whether that string is palindrome or not and responds with appropriate message.
TCP Client:
import java.io.*;
import java.net.*;
import java.util.*;
class PalindromClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("Localhost", 9874);
Scanner sc = new Scanner(System.in);
InputStream recStr = socket.getInputStream();
OutputStream sndStr = socket.getOutputStream();
DataInputStream inputStr = new DataInputStream(recStr);
DataOutputStream outputStr = new DataOutputStream(sndStr);
System.out.print("Enter string: ");
String str = sc.nextLine();
outputStr.writeUTF(str);
System.out.println(inputStr.readUTF());
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
TCP Server:
import java.io.*;
import java.net.*;
import java.util.*;
class PalindromServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9874);
Socket socket = serverSocket.accept();
Scanner sc = new Scanner(System.in);
InputStream recStr = socket.getInputStream();
OutputStream sndStr = socket.getOutputStream();
DataInputStream inputStr = new DataInputStream(recStr);
DataOutputStream outputStr = new DataOutputStream(sndStr);
String str = inputStr.readUTF();
String palindromStr = "";
for(int i=str.length()-1; i>=0; i--) {
char c = str.charAt(i);
palindromStr += c;
}
if(str.equalsIgnoreCase(palindromStr))
outputStr.writeUTF(str+" is palindrom.");
else
outputStr.writeUTF(str+" is not palindrom.");
socket.close();
serverSocket.close();
}
catch(Exception ex) {
System.out.println(ex);
}
}
import java.io.*;
import java.net.*;
import java.util.*;
class PalindromClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("Localhost", 9874);
Scanner sc = new Scanner(System.in);
InputStream recStr = socket.getInputStream();
OutputStream sndStr = socket.getOutputStream();
DataInputStream inputStr = new DataInputStream(recStr);
DataOutputStream outputStr = new DataOutputStream(sndStr);
System.out.print("Enter string: ");
String str = sc.nextLine();
outputStr.writeUTF(str);
System.out.println(inputStr.readUTF());
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
TCP Server:
import java.io.*;
import java.net.*;
import java.util.*;
class PalindromServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9874);
Socket socket = serverSocket.accept();
Scanner sc = new Scanner(System.in);
InputStream recStr = socket.getInputStream();
OutputStream sndStr = socket.getOutputStream();
DataInputStream inputStr = new DataInputStream(recStr);
DataOutputStream outputStr = new DataOutputStream(sndStr);
String str = inputStr.readUTF();
String palindromStr = "";
for(int i=str.length()-1; i>=0; i--) {
char c = str.charAt(i);
palindromStr += c;
}
if(str.equalsIgnoreCase(palindromStr))
outputStr.writeUTF(str+" is palindrom.");
else
outputStr.writeUTF(str+" is not palindrom.");
socket.close();
serverSocket.close();
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
0 Comments