-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTestPeople.java
73 lines (55 loc) · 1.87 KB
/
TestPeople.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
71
72
import com.example.tutorial.PersonProtos.People;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.InputStream;
import java.io.BufferedOutputStream;
class TestPeople {
public static void main(String args[]) {
String host = "127.0.0.1";
int port = 8000;
People people = People.newBuilder()
.setName("deli")
.setId(2)
.setEmail("lytsing@hotmail.com").build();
try {
Socket client = new Socket(host, port);
// Send message to Server
byte[] result = people.toByteArray() ;
client.getOutputStream().write(result);
// Receive message from Server
InputStream input = client.getInputStream();
byte[] msg = recvMsg(input);
People p = People.parseFrom(msg);
System.out.println(p.getName());
System.out.println(p.getId());
System.out.println(p.getEmail());
input.close();
client.close();
} catch (UnknownHostException e){
System.out.println("UnknownHostException:" + e.toString());
} catch (java.io.IOException e) {
System.out.println("IOException :" + e.toString());
e.printStackTrace();
}
}
/**
* Receive mssage from Server
*
* @return
*/
public static byte[] recvMsg(InputStream inpustream) {
try {
byte len[] = new byte[1024];
int count = inpustream.read(len);
byte[] temp = new byte[count];
for (int i = 0; i < count; i++) {
temp[i] = len[i];
}
return temp;
} catch (Exception e) {
System.out.println("recvMsg() occur exception!" + e.toString());
}
return null;
}
}