Skip to content

Instantly share code, notes, and snippets.

@ketankr9
Created September 16, 2018 20:10
Show Gist options
  • Save ketankr9/194a562113ded8896caf8dababea6256 to your computer and use it in GitHub Desktop.
Save ketankr9/194a562113ded8896caf8dababea6256 to your computer and use it in GitHub Desktop.
Java Socket implementation of Client.
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
class SimpleTCPClient
{
private static Socket socket;
private static InputStream input;
private static InputStreamReader reader;
private static BufferedReader br;
private static OutputStream os;
private static OutputStreamWriter osw;
private static BufferedWriter bufferedWriter;
public static void initialize(String hostname, int port) throws UnknownHostException, IOException{
socket = new Socket(hostname, port);
input = socket.getInputStream();
reader = new InputStreamReader(input);
br =new BufferedReader(reader);
os = socket.getOutputStream();
osw = new OutputStreamWriter(os);
bufferedWriter = new BufferedWriter(osw);
}
public static boolean send(String msg) throws IOException{
bufferedWriter.write(msg); bufferedWriter.flush();
return true;
}
public static String receive() throws IOException{
String message;
String data="";
while((message=br.readLine())!=null){
System.out.println("N:"+message);
data+=message;
}
return data;
}
public static void main(String args[]) throws UnknownHostException, IOException
{
initialize("misc.chal.csaw.io", 9000);
String message = receive();
send("Hiii\n");
receive();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment