Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package loggi;
- import java.io.*;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Patrick
- */
- public class LSocket {
- InetAddress address;
- int port;
- Socket socket;
- BufferedReader rd;
- BufferedWriter wr;
- public LSocket(InetAddress address, int port) {
- this.address = address;
- this.port = port;
- connect();
- }
- public LSocket(String address, int port) {
- try {
- this.address = InetAddress.getByName(address);
- this.port = port;
- connect();
- } catch (UnknownHostException e) {
- }
- }
- public void connect() {
- try {
- socket = new Socket(address, port);
- rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- } catch (IOException e) {
- }
- }
- public boolean isConnected() {
- return !socket.isClosed() && socket.isConnected();
- }
- public String readline() throws IOException {
- String str;
- if ((str = rd.readLine()) != null) {
- System.out.println("<-- " + str);
- return str;
- }
- return null;
- }
- public void writeline(String str) {
- write(str + "\r\n");
- }
- public void write(String str) {
- System.out.print("--> " + str);
- try {
- wr.write(str);
- wr.flush();
- } catch (IOException e) {
- }
- }
- public void close() {
- try {
- socket.close();
- } catch (IOException ex) {
- Logger.getLogger(LSocket.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment