Advertisement
Guest User

Untitled

a guest
May 30th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package de.tu_clausthal.in.mec;
  2.  
  3. import de.tu_clausthal.in.mec.gui.Logger;
  4.  
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import java.io.IOException;
  8. import java.net.Socket;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. /**
  13.  * client class for testing purpose
  14.  * helper for sending data from server to server
  15.  */
  16. public class Client extends Thread {
  17.  
  18.     /**
  19.      * constant string to indicate end of header
  20.      */
  21.     private static final String END_HEADER = "END_HEADER";
  22.     /**
  23.      * constant string to indicate end of body
  24.      */
  25.     private static final String END_BODY = "END_BODY";
  26.  
  27.  
  28.     /**
  29.      * method to send new data
  30.      *
  31.      * @param p_url
  32.      * @param p_port
  33.      * @param p_data
  34.      * @return
  35.      */
  36.     public static List<String> sendMessage(String p_url, int p_port, List<String> p_data) {
  37.         List<String> response = new ArrayList<String>();
  38.  
  39.         try {
  40.             Socket socket = new Socket(p_url, p_port);
  41.  
  42.             //write to server
  43.             DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  44.             if (p_data != null) {
  45.                 for (String data : p_data) {
  46.                     out.writeUTF(data);
  47.                 }
  48.             }
  49.  
  50.             //read data from server and print (not needed maybe for testing purpose)
  51.             DataInputStream in = new DataInputStream(socket.getInputStream());
  52.             String line;
  53.             while ((line = in.readUTF()) != null) {
  54.                 if (line.equals("END_BODY"))
  55.                     break;
  56.                 Logger.loggerDebug(line);
  57.             }
  58.  
  59.             //close connections
  60.             out.close();
  61.             in.close();
  62.             socket.close();
  63.  
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         }
  67.  
  68.         return response;
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement