-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessenger.java
63 lines (51 loc) · 1.73 KB
/
Messenger.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
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.Buffer;
/**
* Created by sameerraghuram on 4/25/17.
*/
public class Messenger {
InetAddress address;
int port;
Socket sendSocket;
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
public Messenger(String address, int port) throws IOException {
this(InetAddress.getByName(address), port);
}
public Messenger(InetAddress address, int port) throws IOException {
this.address = address;
this.port = port;
sendSocket = new Socket(this.address, this.port);
}
public void sendMessage(String message) throws IOException {
// Step 1: Convert String to byte-array
byte[] messageBytes = message.getBytes();
// Step 2: Send byte-array
sendMessage(messageBytes);
}
public void sendMessage(byte[] message) throws IOException {
try {
// Step 1: Get socket OutputStream
outputStream = sendSocket.getOutputStream();
bufferedOutputStream = new BufferedOutputStream(outputStream);
// Step 2: Write and flush
bufferedOutputStream.write(message, 0, message.length);
bufferedOutputStream.flush();
System.out.printf("Done\n");
} catch (IOException e) {
e.printStackTrace();
}
finally {
// Close everthing
if(bufferedOutputStream != null)
bufferedOutputStream.close();
if(outputStream != null)
outputStream.close();
}
}
}