Skip to content

Commit

Permalink
Test out handshaking with server
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jun 14, 2021
1 parent 4bca663 commit 970184a
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .idea/libraries/libs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BasicInfoAPI/BasicInfoAPI.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="libs" level="project" />
</component>
</module>
15 changes: 15 additions & 0 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
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(){

}
}
76 changes: 76 additions & 0 deletions BasicInfoAPI/src/test/conn/CommunicateTest.java
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;
}
}
66 changes: 66 additions & 0 deletions BasicInfoAPI/src/test/conn/FakeServerTest.java
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();
}

}

0 comments on commit 970184a

Please sign in to comment.