Advertisement
Guest User

Untitled

a guest
Feb 16th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5.  
  6. public class networking {
  7.    
  8.    private static PrintWriter out;
  9.    private static BufferedReader in;
  10.    private static Socket server;
  11.    
  12.    public void connect(String host, int port) throws IOException, UnknownHostException {
  13.       try {
  14.          server = new Socket(host, port);
  15.          out = new PrintWriter(server.getOutputStream(), true);
  16.          in = new BufferedReader(new InputStreamReader(server.getInputStream()));
  17.       } catch (UnknownHostException e) {
  18.          System.err.println("Don't know about host: " + host);
  19.       } catch (IOException e) {
  20.          System.err.println("Couldn't get I/O for the connection to: " + host);
  21.         }
  22.    }
  23.    
  24.    public void disconnect() {
  25.       System.out.println("Disconnecting...");
  26.       try {
  27.          server.close();
  28.          out.close();
  29.          in.close();
  30.          System.out.println("...Disconnected!");
  31.       } catch (IOException e) {
  32.          System.err.println("Derp.");
  33.       } catch (NullPointerException e) {
  34.          System.err.println("Cannot disconnect: Not connected to a server!");
  35.       }
  36.    }
  37.    
  38.    public void send(String str) throws NullPointerException {
  39.       try {
  40.          out.println(str);
  41.       } catch (NullPointerException e) {
  42.          System.err.println("Cannot send: Not connected to a server!");
  43.       }
  44.    }
  45.    
  46.    public String read() throws IOException, NullPointerException {
  47.       String a = null;
  48.       try {
  49.          a = in.readLine();
  50.       } catch (NullPointerException e) {
  51.          System.err.println("Cannot read: Pipe is empty!");
  52.       }
  53.       return a;
  54.    }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement