Last active
April 26, 2016 13:20
-
-
Save kell18/e026f6d4c449ce5f42cdb9a97c508c66 to your computer and use it in GitHub Desktop.
Java TCP Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Client { | |
private static final int BUFFER_SIZE = 1056; | |
private Socket socket; | |
private PrintWriter outWriter; | |
private BufferedReader inReader; | |
public void PingPong() { | |
outWriter.write("ping"); | |
outWriter.flush(); | |
Thread.sleep(1000L); | |
System.out.println("Sent"); | |
CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); | |
int charsRead = inReader.read(buffer); | |
String response = new String(buffer.array(), 0, charsRead); | |
System.out.println("response: " + response); | |
} | |
public Client(String hostName, int portNumber) { | |
try { | |
socket = new Socket(hostName, portNumber); | |
outWriter = new PrintWriter(socket.getOutputStream(), false); | |
inReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment