-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main.conn; | ||
|
||
|
||
/** | ||
* A MinecraftServer instance provides methods to create connection to server and communicate with peer. | ||
* This is the basic instance and any info-getting function is base on this class. | ||
* | ||
* @author Rock Chin | ||
*/ | ||
public class MinecraftServer extends Thread{ | ||
@Override | ||
public void run(){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package test.conn; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class CommunicateTest { | ||
static String host="192.168.1.3"; | ||
static int port=25565; | ||
static DataOutputStream dataOutputStream; | ||
static DataInputStream dataInputStream; | ||
public static void main(String[] args)throws Exception { | ||
Socket socket=new Socket(host,port); | ||
System.out.println("established."); | ||
dataOutputStream=new DataOutputStream(socket.getOutputStream()); | ||
dataInputStream=new DataInputStream(socket.getInputStream()); | ||
System.out.println("streamObjectCreated."); | ||
writeVarInt(18); | ||
writeVarInt(0x00); | ||
writeVarInt(754); | ||
writeString("192.168.1.3"); | ||
writeVarInt(25567); | ||
writeVarInt(1); | ||
// dataOutputStream.write(1); | ||
// dataOutputStream.flush(); | ||
System.out.println("wroteHandshake."); | ||
writeVarInt(0); | ||
System.out.println("recv:"+readString()); | ||
} | ||
public static void writeVarInt(int value)throws Exception { | ||
do { | ||
byte temp = (byte)(value & 0b01111111); | ||
// Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone | ||
value >>>= 7; | ||
if (value != 0) { | ||
temp |= 0b10000000; | ||
} | ||
dataOutputStream.write(temp); | ||
dataOutputStream.flush(); | ||
} while (value != 0); | ||
} | ||
|
||
static void writeString(String s)throws Exception{ | ||
byte[] b=s.getBytes(); | ||
writeVarInt(b.length); | ||
dataOutputStream.write(b); | ||
dataOutputStream.flush(); | ||
} | ||
static void writePacket(){} | ||
public static String readString()throws Exception{ | ||
int len=readVarInt(); | ||
byte[] b=new byte[len]; | ||
dataInputStream.read(b,0,len); | ||
return new String(b); | ||
} | ||
public static int readVarInt()throws Exception { | ||
int numRead = 0; | ||
int result = 0; | ||
byte read; | ||
do { | ||
read = dataInputStream.readByte(); | ||
int value = (read & 0b01111111); | ||
result |= (value << (7 * numRead)); | ||
|
||
numRead++; | ||
if (numRead > 5) { | ||
throw new RuntimeException("VarInt is too big"); | ||
} | ||
} while ((read & 0b10000000) != 0); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package test.conn; | ||
|
||
import java.io.DataInputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class FakeServerTest { | ||
static DataInputStream dataInputStream; | ||
public static void main(String[] args)throws Exception { | ||
ServerSocket socket=new ServerSocket(25567); | ||
while (true){ | ||
Socket client=socket.accept(); | ||
System.out.println("newConn."); | ||
try{ | ||
dataInputStream=new DataInputStream(client.getInputStream()); | ||
int len=0; | ||
byte[] recv=new byte[2048]; | ||
System.out.println("Length(varInt):"+readVarInt()); | ||
System.out.println("PacketID(varInt):"+readVarInt()); | ||
System.out.println("ProtocolVersion(varInt):"+readVarInt()); | ||
System.out.println("ServerAddress(String):"+readString()); | ||
System.out.println("ServerPort(UnsignedShort):"+readUnsignedShort()); | ||
System.out.println("NextState(varIntEnum):"+readVarInt()); | ||
while((len=dataInputStream.read(recv))!=-1){ | ||
if (len==0) | ||
continue; | ||
// System.out.println("recv:"+new String(recv, StandardCharsets.UTF_8)); | ||
for (int i=0;i<1024;i++){ | ||
System.out.print(recv[i]+" "); | ||
} | ||
recv=new byte[1024]; | ||
} | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
public static int readVarInt()throws Exception { | ||
int numRead = 0; | ||
int result = 0; | ||
byte read; | ||
do { | ||
read = dataInputStream.readByte(); | ||
int value = (read & 0b01111111); | ||
result |= (value << (7 * numRead)); | ||
|
||
numRead++; | ||
if (numRead > 5) { | ||
throw new RuntimeException("VarInt is too big"); | ||
} | ||
} while ((read & 0b10000000) != 0); | ||
|
||
return result; | ||
} | ||
public static String readString()throws Exception{ | ||
int len=readVarInt(); | ||
byte[] b=new byte[len]; | ||
dataInputStream.read(b,0,len); | ||
return new String(b); | ||
} | ||
public static int readUnsignedShort()throws Exception{ | ||
return dataInputStream.readUnsignedShort(); | ||
} | ||
|
||
} |