-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
171 lines (137 loc) · 4.77 KB
/
Copy pathMain.java
File metadata and controls
171 lines (137 loc) · 4.77 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* @Author: ywc
* @Date: 2020-03-15 20:07:00
* @LastEditTime: 2020-03-16 12:30:16
* @LastEditors: Please set LastEditors
* @Description: test
* @FilePath: \test\Main.java
*/
import java.net.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.lang.Character;
public class Main {
byte[] rawBuffer = new byte[1024];
ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
InputStream inFromServer;
public boolean readSocket()throws IOException{
buffer.compact();
int head = buffer.position();
//采用包头+包体,标识符 报头:7E len1 len2+ [报文体]
int r = inFromServer.read(rawBuffer, head,rawBuffer.length - head);
if (r == -1) {
return false;
// throw new EOFException("Controller socket closed");
}
buffer.position(head + r);
buffer.flip();
return true;
}
public String Next(){
int packLen =processPacket();
if( packLen==-1) return null;
int savelimit = buffer.limit();
int curpos = buffer.position();
buffer.limit(packLen+curpos);
Charset cs = Charset.forName ("UTF-8");
CharBuffer cb = cs.decode (buffer);
String str = cb.toString();
// System.out.println(str);
// System.out.println(str.length());
buffer.limit(savelimit);
// buffer.position(str.length());
return str;
}
public int processPacket() {
int res = -1;
while(true){
if (!buffer.hasRemaining() ||(buffer.remaining() <= 3)) {
res = -1;
break;
}
int savePosition = buffer.position();
byte firstBy = buffer.get();
if(0x7e == firstBy){
byte bh = buffer.get();
byte bl = buffer.get();
res = (((int)bh)<<8)|bl;
if(res>buffer.remaining())
{
res =-1;
buffer.position(savePosition);
}
break;
}
}
return res;
}
public void run()throws IOException{
System.out.println("--------------");
String serverName ="127.0.0.1";
int port = 8080;
try
{
System.out.println("连接到主机:" + serverName + " ,端口号:" + port);
Socket client = new Socket(serverName, port);
System.out.println("远程主机地址:" + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
inFromServer = client.getInputStream();
buffer.limit(0);
while(true)
{
boolean res = readSocket();
if(!res)break;
while(true) {
String msg = Next();
if(msg==null){
break;
}
System.out.println(msg);
System.out.println(msg.length());
}
}
client.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Main obj=new Main();
obj.run();;
/*
InputStream input = new FileInputStream("D:/my1.txt");
byte[] rawBuffer = new byte[1024];
ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
buffer.limit(0);
while(true)
{
int head = buffer.position();
buffer.compact();
head = buffer.position();
int r = input.read(rawBuffer, head,rawBuffer.length - head);
if (r == -1) {
break;
// throw new EOFException("Controller socket closed");
}
buffer.position(head + r);
buffer.flip();
int savedPosition = buffer.position();
int type = buffer.get();
int a = buffer.get();
int b = buffer.get();
boolean fla = buffer.hasRemaining();
String str;
str = (char)type+""+(char)a+""+(char)b;
System.out.println(str);
}
input.close();
*/
}
}