Advertisement
athacks

Untitled

Jun 9th, 2015
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.   public static void main(String args[]) throws Exception{
  2.         TCPClient client = new TCPClient();
  3.         client.SendToServer("payload");
  4.         System.out.println("Server Said(1): "+client.RecieveFromServer());
  5.         client.close();
  6.     }
  7.  
  8.     TCPClient(String _host, int _port) throws Exception{
  9.         host = _host;
  10.         port = _port;
  11.         socket = new Socket(host, port);
  12.     }
  13.     TCPClient() throws Exception{
  14.         socket = new Socket(host, port);
  15.     }
  16.     void SendToServer(String msg) throws Exception{
  17.         //create output stream attached to socket
  18.         PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
  19.         //send msg to server
  20.         outToServer.print(msg + '\n');
  21.         outToServer.flush();
  22.     }
  23.     String RecieveFromServer() throws Exception{
  24.         //create input stream attached to socket
  25.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream()));
  26.         //read line from server
  27.         String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException
  28.         return res;
  29.     }
  30.     void close() throws IOException{
  31.         socket.close();
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement