|
@@ -0,0 +1,36 @@
|
|
|
|
|
+package model;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.DataInputStream;
|
|
|
|
|
+import java.io.DataOutputStream;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.Socket;
|
|
|
|
|
+import java.net.UnknownHostException;
|
|
|
|
|
+
|
|
|
|
|
+public class NetworkConnection {
|
|
|
|
|
+ private static final int port = 1234;
|
|
|
|
|
+ private static final String address = "62.195.226.247";
|
|
|
|
|
+ private DataOutputStream dout;
|
|
|
|
|
+ private DataInputStream din;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void connect(String name) throws UnknownHostException, IOException{
|
|
|
|
|
+ if(checkConnection()){
|
|
|
|
|
+ throw new IOException("Already connected!");
|
|
|
|
|
+ }
|
|
|
|
|
+ Socket server = new Socket(address , port);
|
|
|
|
|
+ dout = new DataOutputStream(server.getOutputStream());
|
|
|
|
|
+ din = new DataInputStream(server.getInputStream());
|
|
|
|
|
+ dout.writeUTF(name);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkConnection(){
|
|
|
|
|
+ try {
|
|
|
|
|
+ dout.write(0);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|