-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send_Rec.java
49 lines (36 loc) · 1.33 KB
/
Send_Rec.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
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class Send_Rec {
public static Charset charset = Charset.forName("UTF-8");
public static void send(String sent, SocketChannel sock_chan) {
ByteBuffer send_data = ByteBuffer.wrap(sent.getBytes(charset));
try {
sock_chan.write(send_data);
System.out.println("here1");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("key gone");
}
}
public static String receive(SocketChannel sock_chan) throws IOException {
ByteBuffer num = ByteBuffer.allocate(1000);
String data = "";
if(sock_chan.read(num) == -1){
return "";
}
data += charset.decode(num.position(0)).toString();
return data;
}
public static String receive(SocketChannel sock_chan,int i) throws IOException {
ByteBuffer num = ByteBuffer.allocate(1000);
num.clear();
String data = "";
while(sock_chan.read(num) == -1){
}
System.out.println(num.position(0).toString());
data += charset.decode(num.position(0)).toString();
return data;
}
}