I'm sort of new to Sockets and I'm attempting to write a program where I can basically send some simple requests from Client program to Server program. Right now I just want to have in Client like 1 or 2 options that user can choose from. For example if user chooses "Option 1" in Client then Server sends back a message "You choose Option 1" and so on, but I can't figure out how to read inputs on Server that has been sent from Client.
Client Code:
Socket socket = null; try { System.out.println("Connecting to Server"); socket = new Socket("192.168.0.104", 7003); socket.setSoTimeout(10000); System.out.println("Connected"); InputStreamReader input = new InputStreamReader(socket.getInputStream()); BufferedReader buffer = new BufferedReader(input); PrintWriter print = new PrintWriter(socket.getOutputStream(), true); String line = buffer.readLine(); //Not Sure which buffer to user here System.out.println("Option 1"); System.out.println("Option 2"); System.out.println("Option 3"); } System.out.println("Closing Client Connection"); buffer.close(); input.close(); print.close(); socket.close(); System.exit(0); Server Code:
ServerSocket serverSock = null; Socket standSock = null; try { serverSock = new ServerSocket(7003); standSock = serverSock.accept(); InputStreamReader input = new InputStreamReader(standSock.getInputStream()); BufferedReader read = new BufferedReader(input); PrintWriter print = new PrintWriter(standSock.getOutputStream(), true); String dateTime = (Calendar.getInstance()).getTime().toString(); print.println("You're connected to the Server at: " + dateTime); print.println("Type Q to disconnect"); String line = read.readLine(); //Not sure what to do here System.out.println("Client: " + line); print.println("Server" + line); System.out.println("Closing Server Connection"); read.close(); input.close(); print.close(); standSock.close(); Do I need two different BufferedReaders in Clinet one for user input and one for socket? Really confused about this part.
Thanks