在Java中在套接字上编写协议的问题 [英] Problems writing a protocol on top of sockets in Java

查看:157
本文介绍了在Java中在套接字上编写协议的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在套接字上编写协议,因此我决定实现标头然后发送信息。因此,服务器上的每个连接都有一个线程,它位于读取头部,然后委托方法在其到达时读取其余信息。

I'm writing a protocol on top of sockets, so I've decided to implement headers then send the information. So there is one thread per connection on the server which sits there reading in headers, then delegates off to methods to read in the rest of the information when it arrives.

所以基本上看起来像这样:

So essentially it looks like this:

while ((length = inStream.read(buffer)) != -1)
{
   dispatch(buffer, length);
}

因此,调度方法会解密标头并根据找到的方法委托方法在标题中。它看起来类似于:

So the dispatch method then decrypts the headers and delegates the method depending what is found in the header. It looks similar to:

byte[] clearText = decrypt(message,length);
if (cleartext == foo) sendFooToSocket();

然后,sendFooToSocket()会坐在那里从内流中读取或发送到流出。
这是我似乎遇到一些问题的地方,在客户端我发送标题然后刷新,然后发送其余的数据,但它似乎都是一体而不被分成标题那么数据。还有一种强制退出sendFooToSocket方法的最佳方法吗?

So then sendFooToSocket() would then sit there and read from the instream or send to the outstream. This is where I seem to run into some problems, in the client I'm sending the header then flushing, then sending the rest of the data, but it appears it's all coming as one and not being split up into header then data. Also is there a best way to force out of the sendFooToSocket method?

public void sendFooToSocket()
{
  byte[] buffer = new byte[1024];
  int length = 0;
  while ((length = inStream.read(buffer) >0)
  {
    message = decrypt(buffer, length);
  }
}

我会假设同花顺将允许我突破这个方法,因为它关闭然后打开流?

I would assume flush would allow me to break out of this method as it closes then opens the stream?

所以我有2个问题,flush似乎没有分解我的消息而flush似乎不允许丢弃诸如sendFooToSocket()之类的方法建议?

So I have 2 problems, flush doesn't seem to be breaking up my messages and flush doesn't seem to be allowing to drop out of methods such as sendFooToSocket(), any suggestions?

为清楚起见,客户只是这样做:

For clarity sake, the client just does this:

byte[] header = "MESG".getBytes();
cipher = encrypt(header);
outStream.write(cipher,0,cipher.length);
outStream.flush();
byte[] message = "Hi server".getBytes();
cipher = encrypt(message);
outStream.write(cipher,0,cipher.length);
outStream.flush();

但服务器收到此消息为1消息,即使它是每次写完后都会刷新。只发送标题就行了,我们卡在sendFooToSocket()方法中,但如果我在刷新后发送数据,它会立即发送。

But this is received by the server as 1 message even though it's been flushed after every write. Sending just the header works, and we get stuck in the sendFooToSocket() method, but if I send the data after the flush it comes all at once.

客户端仅使用套接字中的OutputStream和InputStreams。得到。客户端还使用OutputStream和InputStream。不确定这是否重要?

The client uses OutputStream and InputStreams just from the socket.get. The client also uses OutputStream and InputStream. Not sure if this matters?

推荐答案

您似乎想要的是记录边界。对于流通常没有隐含的记录边界。如果你想要这种功能,你需要自己实现它,通过缓冲输入并查找换行符来表示记录的结束。

What you seem to want is "record boundaries". With streams in general there are no implicit record boundaries. If you want that kind of functionality you will need to implement it yourself, by buffering the input and looking for, say, newlines to indicate the end of a record.

看看在BufferedInputStream。

Look at BufferedInputStream.

这篇关于在Java中在套接字上编写协议的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆