Guest User

Untitled

a guest
Jul 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package loggi;
  6.  
  7. import java.io.*;
  8. import java.net.InetAddress;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15. *
  16. * @author Patrick
  17. */
  18. public class LSocket {
  19. InetAddress address;
  20. int port;
  21. Socket socket;
  22. BufferedReader rd;
  23. BufferedWriter wr;
  24.  
  25. public LSocket(InetAddress address, int port) {
  26. this.address = address;
  27. this.port = port;
  28. connect();
  29. }
  30.  
  31. public LSocket(String address, int port) {
  32. try {
  33. this.address = InetAddress.getByName(address);
  34. this.port = port;
  35. connect();
  36. } catch (UnknownHostException e) {
  37. }
  38. }
  39.  
  40. public void connect() {
  41. try {
  42. socket = new Socket(address, port);
  43. rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  44. wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  45. } catch (IOException e) {
  46. }
  47. }
  48.  
  49. public boolean isConnected() {
  50. return !socket.isClosed() && socket.isConnected();
  51. }
  52.  
  53. public String readline() throws IOException {
  54. String str;
  55. if ((str = rd.readLine()) != null) {
  56. System.out.println("<-- " + str);
  57. return str;
  58. }
  59. return null;
  60. }
  61.  
  62. public void writeline(String str) {
  63. write(str + "\r\n");
  64. }
  65.  
  66. public void write(String str) {
  67. System.out.print("--> " + str);
  68. try {
  69. wr.write(str);
  70. wr.flush();
  71. } catch (IOException e) {
  72. }
  73. }
  74.  
  75. public void close() {
  76. try {
  77. socket.close();
  78. } catch (IOException ex) {
  79. Logger.getLogger(LSocket.class.getName()).log(Level.SEVERE, null, ex);
  80. }
  81. }
  82. }
Add Comment
Please, Sign In to add comment