-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySocket.java
44 lines (43 loc) · 1.22 KB
/
MySocket.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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MySocket {
public Socket s;
public DataInputStream dis;
public DataOutputStream dos;
public MySocket(Socket s){
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public MySocket(String host, int port){
try {
this.s = new Socket(host, port);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void println(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public String readLine(){
try {
byte[] bytes = new byte[1024];
dis.read(bytes);
return new String(bytes);
} catch (IOException e) {
return e.getMessage();
}
}
}