Advertisement
Guest User

Untitled

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