Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercicio1;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.Random;
- public class ClientTCP extends Thread {
- private final String TheOneRing =
- "Three Rings for the Elven-kings under the sky,"
- + "Seven for the Dwarf-lords in halls of stone,"
- + "Nine for Mortal Men doomed to die,"
- + "One for the Dark Lord on his dark throne,"
- + "In the Land of Mordor where the Shadows lie,"
- + "One Ring to rule them all,"
- + "One Ring to find them,"
- + "One Ring to bring them all and in the darkness bind them,"
- + "In the Land of Mordor where the Shadows lie";
- private String[] strings = TheOneRing.split(",");
- private String host;
- private int port;
- private Socket clientSocket;
- private DataOutputStream outToServer;
- private DataInputStream inFromServer;
- private int sentMessageSize;
- private int receiveMessageSize;
- private Random rng = new Random();
- private int selectedNumber;
- private Message receivedMessage;
- public ClientTCP(String host1, int port){
- this.host = host1;
- this.port = port;
- }
- public void run(){
- while(true){
- try {
- selectedNumber = rng.nextInt(8);
- send(new Message(strings[selectedNumber]));
- System.out.println("Text Sent to Server: "+ strings[selectedNumber]);
- receivedMessage = deserialize(receive());
- System.out.println("Text Received From Server: " + receivedMessage.getMessage());
- wait(2000);
- } catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- public void send(Message message) throws IOException, InterruptedException {
- byte[] msg = serialize(message);
- clientSocket = new Socket(host, port);
- outToServer = new DataOutputStream(clientSocket.getOutputStream());
- inFromServer = new DataInputStream(clientSocket.getInputStream());
- sentMessageSize = msg.length;
- outToServer.writeInt(sentMessageSize);
- outToServer.write(msg, 0, sentMessageSize); //int off de offset
- outToServer.flush();
- }
- public byte [] receive() throws IOException, InterruptedException{
- receiveMessageSize = inFromServer.readInt();
- byte msg [] = new byte[receiveMessageSize];
- inFromServer.read(msg,0,receiveMessageSize);
- clientSocket.close();
- outToServer.close();
- inFromServer.close();
- return msg;
- }
- public byte[] serialize(Message message) throws IOException{
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
- ObjectOutputStream outStream = new ObjectOutputStream(byteStream);
- outStream.writeObject(message);
- byte[] marshalledMessage = byteStream.toByteArray();
- return marshalledMessage;
- }
- public Message deserialize(byte[] data) throws IOException, ClassNotFoundException {
- ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
- ObjectInputStream inputStream = new ObjectInputStream(byteStream);
- Message unmarshalledMessage = (Message) inputStream.readObject();
- return unmarshalledMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement