Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class networking {
- private static PrintWriter out;
- private static BufferedReader in;
- private static Socket server;
- public void connect(String host, int port) throws IOException, UnknownHostException {
- try {
- server = new Socket(host, port);
- out = new PrintWriter(server.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(server.getInputStream()));
- } catch (UnknownHostException e) {
- System.err.println("Don't know about host: " + host);
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for the connection to: " + host);
- }
- }
- public void disconnect() {
- System.out.println("Disconnecting...");
- try {
- server.close();
- out.close();
- in.close();
- System.out.println("...Disconnected!");
- } catch (IOException e) {
- System.err.println("Derp.");
- } catch (NullPointerException e) {
- System.err.println("Cannot disconnect: Not connected to a server!");
- }
- }
- public void send(String str) throws NullPointerException {
- try {
- out.println(str);
- } catch (NullPointerException e) {
- System.err.println("Cannot send: Not connected to a server!");
- }
- }
- public String read() throws IOException, NullPointerException {
- String a = null;
- try {
- a = in.readLine();
- } catch (NullPointerException e) {
- System.err.println("Cannot read: Pipe is empty!");
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement