我试图将GPS数据发送到特定的服务器端口 已经听取了协议。
AdmProtocolTester.java
package MockTracker.TrackerSimulator;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.nio.ByteOrder;
import javax.xml.bind.DatatypeConverter;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelFuture;
public class AdmProtocolDecoderTesterOne {
public static void main(String[] args) throws Exception{
new AdmProtocolDecoderTesterOne("localhost", 5092).run();
}
private final String host;
private final int port;
public AdmProtocolDecoderTesterOne(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(group);
clientBootstrap.channel(NioSocketChannel.class);
clientBootstrap.remoteAddress(new InetSocketAddress("localhost", 5092));
clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture channelFuture = (ChannelFuture) clientBootstrap.connect().sync();
channelFuture.getChannel().getCloseFuture().sync();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true) {
((Channel) channelFuture).write(in.readLine()+ "\r\n");
((Channel) channelFuture).flush();
}
} finally {
group.shutdownGracefully().sync();
}
}
}
ClientHandler.java
package MockTracker.TrackerSimulator;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import com.cloudhopper.commons.charset.CharsetUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class ClientHandler extends SimpleChannelInboundHandler<ByteOrder> {
@Override
public void channelActive(ChannelHandlerContext channelHandlerContext){
channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", (Charset) CharsetUtil.CHARSET_UTF_8));
}
@Override
protected void messageReceived(ChannelHandlerContext ctx, ByteOrder msg) throws Exception {
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
cause.printStackTrace();
channelHandlerContext.close();
}
}
ChannelInitializer Cannel.java
package MockTracker.TrackerSimulator;
import java.net.SocketAddress;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
public class Channel extends ChannelInitializer<SocketChannel>{
clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
private static final long serialVersionUID = 1L;
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ChannelHandler());
}
});
}
AdmProtocolTester.java
这是要发送到服务器端口的实际数据,而不是使用Junit Test I尝试使用单独的Java GPS模拟应用程序发送数据。
public void testDecode()抛出异常{
AdmProtocolDecoder decoder = new AdmProtocolDecoder(new AdmProtocol()); verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "010042033836313331313030323639343838320501000000000000000000000000000000000000000000000000000000000000000000000000000000000000000073")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, "01002680336510002062A34C423DCF8E42A50B1700005801140767E30F568F2534107D220000")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, "010022003300072020000000000000000044062A330000000000107F10565D4A8310")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, "0100268033641080207AA34C424CCF8E4239030800005B01140755E30F560000F00F70220000")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, "01002680336510002062A34C423DCF8E42A50B1700005801140767E30F568F2534107D220000")); verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, "01002200333508202000000000000000007F0D9F030000000000E39A1056E24A8210")); } }
我试图通过控制台发送值,但我无法通过以下行发送,如果我错过了任何内容,请你帮助我
((Channel)channelFuture).write(in.readLine()+&#34; \ r \ n&#34;);
端口 5092 是服务器侦听 AdmProtocol 的地方。