socket debugger: 2bytes for string length

This commit is contained in:
Jindra Petřík
2014-10-28 18:09:04 +01:00
parent 6f5d460029
commit 925dfc4622
3 changed files with 29 additions and 22 deletions

View File

@@ -26,7 +26,8 @@
private static function writeString(msg){
var b:ByteArray = new ByteArray();
b.writeUTFBytes(msg);
s.writeByte(b.length);
s.writeByte((b.length>>8) & 0xff);
s.writeByte(b.length & 0xff);
s.writeBytes(b,0,b.length);
}

View File

@@ -51,25 +51,31 @@ public class Debugger {
}
}
private String readString(InputStream is) throws IOException
{
private String readString(InputStream is) throws IOException {
int len = is.read();
if (len == -1) {
throw new EOFException();
}
byte buf[] = new byte[len];
for(int i=0;i<len;i++){
int rd = is.read();
if(rd == -1){
throw new EOFException();
}
buf[i] = (byte)rd;
}
return new String(buf, "UTF-8");
if (len == -1) {
throw new EOFException();
}
int len2 = is.read();
if (len2 == -1) {
throw new EOFException();
}
len = (len << 8) + len2;
byte buf[] = new byte[len];
for (int i = 0; i < len; i++) {
int rd = is.read();
if (rd == -1) {
throw new EOFException();
}
buf[i] = (byte) rd;
}
return new String(buf, "UTF-8");
}
@Override
public void run() {
String clientName = ""+id;
String clientName = "" + id;
try (InputStream is = s.getInputStream()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -89,7 +95,7 @@ public class Debugger {
}
} else {
String name = readString(is);
if(!name.equals("")){
if (!name.equals("")) {
clientName = name;
}
while (true) {
@@ -98,8 +104,8 @@ public class Debugger {
l.onMessage(clientName, ret);
}
}
}
}
} catch (IOException ex) {
//ignore
}
@@ -128,9 +134,9 @@ public class Debugger {
@Override
public void run() {
try {
ss = new ServerSocket(port,50,InetAddress.getByName("localhost"));
ss.setReuseAddress(true);
try {
ss = new ServerSocket(port, 50, InetAddress.getByName("localhost"));
ss.setReuseAddress(true);
while (true) {
Socket s = ss.accept();
if (s != null) {