Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.tu_clausthal.in.mec;
- import de.tu_clausthal.in.mec.gui.Logger;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * client class for testing purpose
- * helper for sending data from server to server
- */
- public class Client extends Thread {
- /**
- * constant string to indicate end of header
- */
- private static final String END_HEADER = "END_HEADER";
- /**
- * constant string to indicate end of body
- */
- private static final String END_BODY = "END_BODY";
- /**
- * method to send new data
- *
- * @param p_url
- * @param p_port
- * @param p_data
- * @return
- */
- public static List<String> sendMessage(String p_url, int p_port, List<String> p_data) {
- List<String> response = new ArrayList<String>();
- try {
- Socket socket = new Socket(p_url, p_port);
- //write to server
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- if (p_data != null) {
- for (String data : p_data) {
- out.writeUTF(data);
- }
- }
- //read data from server and print (not needed maybe for testing purpose)
- DataInputStream in = new DataInputStream(socket.getInputStream());
- String line;
- while ((line = in.readUTF()) != null) {
- if (line.equals("END_BODY"))
- break;
- Logger.loggerDebug(line);
- }
- //close connections
- out.close();
- in.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement