Advertisement
Guest User

Untitled

a guest
May 29th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. package com.javarush.task.task30.task3008;
  2.  
  3. import java.io.Closeable;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.net.Socket;
  8. import java.net.SocketAddress;
  9.  
  10. /**
  11. * Created by Skwazer on 23.04.2017.
  12. */
  13. public class Connection implements Closeable{
  14. private final Socket socket;
  15. private final ObjectOutputStream out;
  16. private final ObjectInputStream in;
  17.  
  18. public Connection(Socket socket) throws IOException {
  19. this.socket = socket;
  20. this.out = new ObjectOutputStream(socket.getOutputStream());
  21. this.in = new ObjectInputStream(socket.getInputStream());
  22. }
  23.  
  24. public void send(Message message) throws IOException {
  25. synchronized (out) {
  26. out.writeObject(message);
  27. out.flush();
  28. }
  29. }
  30.  
  31. public Message receive() throws IOException, ClassNotFoundException {
  32. Message message;
  33. synchronized (in) {
  34. message = (Message) in.readObject();
  35. return message;
  36. }
  37. }
  38.  
  39. public SocketAddress getRemoteSocketAddress() {
  40. return socket.getRemoteSocketAddress();
  41. }
  42.  
  43. public void close() throws IOException {
  44. out.close();
  45. in.close();
  46. socket.close();
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement