java 客户端向服务端发送数据包_已知服务端数据包格式,客户端怎样用Java实现向服务器端(C#写的)接收和发送数据包(其他同理)...

///

/// 消息包

/// 内存布局

/// {

/// short PacketFlag;//特殊的值

/// short PacketLength;//消息包的大小,消息不能大于32k

/// int PacketID;//消息包类型id;从这以后的数据可以加密

/// int SerialNumber;//消息包序列号,网络连接每发一个消息包,这个值+1

/// int DispatcherID;//发送者的id

/所以消息头的大小是2+2+4+4+4 = 16

/// }

///

public classPacket

{public const short MaxLength = 32000;//32k

public const short InvalidLength = -1;public const short NeedCompressMinLength = 100;public const short NoCryptHeaderLength = 4; //2+2//消息包标志

public PacketFlag PacketFlag =PacketFlag.Normal;public intPacketID;///

/// 消息序列号

///

public intSerialNumber;public int DispatcherID;//if id <0 ,is -serverid

protectedPacketWriter m_writer;public Packet(intpacketID)

:this(packetID,0)

{

}public Packet(int packetID,intdispatcherid)

{

PacketID=packetID;

DispatcherID=dispatcherid;

m_writer= new PacketWriter();/*PacketWriter.CreateInstance()*/;

}private byte[] m_databuffer; //已经有的数据的缓冲

private int m_datalength; //已经有的数据的大小

///

/// 消息总长度

///

public intLength

{

get{if (m_databuffer == null)return Packet.HeaderSize +m_writer.Length;else

return m_datalength +m_writer.Length;

}

set{if (m_databuffer == null)return;

m_datalength=value;

}

}

internalbyte[] DataBuffer{

get{returnm_databuffer;

}

set{

m_databuffer=value;if (m_databuffer != null)

m_datalength=m_databuffer.Length;

}

}publicPacketWriter Writer{

get{returnm_writer;

}

}

internal virtualbyte[] ToArray()

{short packet_length = (short)this.Length;

bool needcreatebuffer= false;if (DataBuffer == null || m_writer.Length > 0)

needcreatebuffer= true;if(needcreatebuffer)

{byte[] newbuffer = new byte[packet_length];if (m_databuffer != null)

{

Buffer.BlockCopy(m_databuffer,0, newbuffer, 0, m_datalength);

}

m_databuffer=newbuffer;

}//写消息头//ArrayUtility.SetShort(m_databuffer, (short)PacketFlag, 0);//ArrayUtility.SetShort(m_databuffer, packet_length, 2);//ArrayUtility.SetInt(m_databuffer, this.PacketID, 4);//ArrayUtility.SetInt(m_databuffer, this.SerialNumber, 8);//ArrayUtility.SetInt(m_databuffer, this.DispatcherID, 12);

WriteHead();if (needcreatebuffer && m_writer.Length > 0)

{

System.Buffer.BlockCopy(m_writer.ToArray(),0, m_databuffer,

packet_length-m_writer.Length, m_writer.Length);

m_writer.Length= 0;

}

m_datalength=packet_length;returnm_databuffer;

}protected voidWriteHead()

{//写消息头

ArrayUtility.SetShort(m_databuffer, (short)PacketFlag, 0);

ArrayUtility.SetShort(m_databuffer, (short)this.Length, 2);

ArrayUtility.SetInt(m_databuffer,this.PacketID, 4);

ArrayUtility.SetInt(m_databuffer,this.SerialNumber, 8);

ArrayUtility.SetInt(m_databuffer,this.DispatcherID, 12);

}///

/// 消息头大小 2+2+4+4+4 = 16

///

public static intHeaderSize

{

get

{return 16;

}

}public voidReset()

{

DataBuffer= null;

Writer.Clear();

}

}

以下是利用Java实现WebSocket客户端服务端发送已知指令及接收服务端返回信息并打印出来的代码示例: ```java import java.net.URI; import java.net.URISyntaxException; import java.util.Scanner; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; public class WebSocketExample extends WebSocketClient { public WebSocketExample(URI serverUri) { super(serverUri); } @Override public void onOpen(ServerHandshake handshakedata) { System.out.println("Connected to server: " + this.getURI()); } @Override public void onMessage(String message) { System.out.println("Received message: " + message); } @Override public void onClose(int code, String reason, boolean remote) { System.out.println("Connection closed: " + code + ", " + reason); } @Override public void onError(Exception ex) { ex.printStackTrace(); } public static void main(String[] args) { try { WebSocketExample client = new WebSocketExample(new URI("ws://localhost:8080")); client.connect(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Enter command to send to server: "); String command = scanner.nextLine(); if (command.equalsIgnoreCase("exit")) { break; } client.send(command); } scanner.close(); client.close(); } catch (URISyntaxException e) { e.printStackTrace(); } } } ``` 在该示例中,我们使用了Java-WebSocket库来实现WebSocket客户端。在WebSocketExample类中,我们覆盖了WebSocketClient的四个方法,以便在连接打开、收到消息、连接关闭或出现错误时执行相应的操作。 在main方法中,我们创建了一个WebSocketExample对象并连接到了本地的WebSocket服务器。然后,我们使用Scanner对象从控制台读取用户输入的指令,并将其发送到服务器上。当用户输入“exit”时,程序将退出。 当WebSocket服务器向客户端发送消息时,onMessage方法将被调用,并打印出收到的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值