I want to pass the string from client to server and the server will display the string. First the client enter the option 1 and the server will reply ask from the movie name. My problem is i cannot enter the name from client and send it to server and display the name.
Here is my code for server and client
TCPServer
import java.io.*;
import java.net.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
class TCPServer {
public static void main(String argv[]) throws Exception{
String clientSentence;
String capitalizedSentence;
String smoviename;
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("Waiting for connection......\n");
while(true) {
Socket connectionSocket = welcomeSocket.accept();
System.out.println("Connected to the client...");
BufferedReader inFromClient =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
Integer x = Integer.valueOf(clientSentence);
if(x==1){
capitalizedSentence = "Enter movie title to query:";
outToClient.writeBytes(capitalizedSentence);
smoviename=inFromClient.readLine();
System.out.println(smoviename);
}else if(x==2){
capitalizedSentence = "option2";
outToClient.writeBytes(capitalizedSentence);
}else if(x==3){
capitalizedSentence = "option3";
outToClient.writeBytes(capitalizedSentence);
}else{
capitalizedSentence = "Invalid input!";
outToClient.writeBytes(capitalizedSentence);
}
capitalizedSentence = ""+ '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
TCPClient
import java.io.*;
import java.net.*;
import java.util.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String option;
int moption;
String modifiedSentence;
String moviename;
BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
//Scanner br = new Scanner (System.in);
Socket clientSocket = new Socket("localhost", 6789);
System.out.println("Connected to server...\n");
do{
System.out.println(" Movie Database");
System.out.println(" ******************\n");
System.out.println("[1] Search Movie\n");
System.out.println("[2] Append Movie\n");
System.out.println("[3] Exit Movie\n");
System.out.println("Enter selection <1 or 2 or 3>:");
option = inFromUser.readLine();
moption=Integer.valueOf(option);
if(moption !=1 && moption !=2 && moption !=3){
System.out.println("Invalid Input, please enter only 1/2/3!!!\n");
}else if (moption==3){
clientSocket.close();
System.exit(0);
}else if(moption==1){
DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(option + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence +"\n");
System.out.print("FROM CLIENT: ");
moviename = inFromUser.readLine();
//System.out.println(moviename);
outToServer.writeBytes(moviename + '\n');
}
}while(moption !=1 && moption !=2 && moption !=3);
clientSocket.close();
}
}