package com.company; import java.io.*; import java.net.Socket; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { int port = 9003; String ip = "172.30.116.172"; Scanner scanner = new Scanner(System.in); System.out.println("Starting..."); Socket socket = new Socket(ip, port); System.out.println(socket.isConnected()); char response = '\0'; int no; DataInputStream socketIn = new DataInputStream(socket.getInputStream()); DataOutputStream socketOut = new DataOutputStream(socket.getOutputStream()); while(response != '=') { System.out.print("Enter a number: "); no = scanner.nextInt(); socketOut.writeInt(no); socketOut.flush(); // C char is 1 byte, Java char is 2 bytes, so it waits until a second byte is received // workaround: read only one byte from the stream and convert it to a char byte[] b = new byte[] { 0 }; socketIn.readFully(b, 0, 1); response = (char)b[0]; if(response == '<') { System.out.println("Enter a lesser number!"); } else if(response == '>') { System.out.println("Enter a bigger number!"); } } int attempts = socketIn.readInt(); System.out.println("Attempts: " + attempts); } }