-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetFunc.java
70 lines (63 loc) · 1.77 KB
/
netFunc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.net.*;
import java.util.*;
import java.io.*;
public class netFunc{
public static final int PORT=1234;
public static String getIP(){
try{
java.net.URL URL = new java.net.URL("http://bot.whatismyipaddress.com");
java.net.HttpURLConnection Conn = (HttpURLConnection)URL.openConnection();
java.io.InputStream InStream = Conn.getInputStream();
java.io.InputStreamReader Isr = new java.io.InputStreamReader(InStream);
java.io.BufferedReader Br = new java.io.BufferedReader(Isr);
return Br.readLine();
}catch(Exception e){e.printStackTrace();}
return "error";
}
public static String readString(Socket socket){
try{
BufferedReader tmp = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return tmp.readLine();
}catch(IOException e){
return null;
}
}
public static boolean sendString(Socket socket, String s){
try {
PrintWriter tmp = new PrintWriter(socket.getOutputStream());
tmp.println(s);
tmp.flush();
return true;
}catch(IOException e){
return false;
}
}
public static Object readObject(Socket socket){
try{
ObjectInputStream tmp=new ObjectInputStream(socket.getInputStream());
return tmp.readObject();
}catch(IOException e){
return null;
}catch(ClassNotFoundException e){
return null;
}
}
public static <K extends Serializable> boolean sendObject(Socket socket, K obj){
try {
ObjectOutputStream tmp = new ObjectOutputStream(socket.getOutputStream());
tmp.writeObject(obj);
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}
}
public static boolean closeSocket(Socket soc){
try{
soc.close();
return true;
}catch(Exception IOException){
return false;
}
}
}