【发布时间】:2011-05-27 09:20:11
【问题描述】:
我有一个简单的程序,无论是服务器还是客户端,都不确定它是哪一端。我打开一个端口并在其上接受消息。这可行,但昨晚当我通过 GET 而不是 POST 恢复一条消息时它崩溃了。您可能已经注意到,我对网络非常陌生。
我的传输正常(套接字的东西),但我无法阅读消息。
这是我目前使用的代码。
//read HTTp header until the message size comes in
for(int eight = 0; eight < 8; eight++)
{
message = in.readLine();
LOGGER.fatal(eight);
LOGGER.fatal(message);
if(message.contains("Content-Length"))
{
try {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
sizeInt = Integer.parseInt(matcher.group());
}
}
catch (Exception e)
{
LOGGER.fatal("size not found on header line", e);
//System.exit(-1);
}
}
}
LOGGER.fatal(sizeInt);
LOGGER.fatal("---------------------------");
char[] buffer =new char[sizeInt];
//skip blank line between header and message
message = in.readLine();
//read message
int fullRead = 0;
int thisRead = 0;
do
{
thisRead = in.read(buffer, fullRead, sizeInt - fullRead);
LOGGER.info(fullRead + " of " + sizeInt + " bytes of message read");
fullRead += thisRead;
}while(fullRead != sizeInt);
我遇到的问题是 GET 方法看不到消息的大小(以字节为单位),没有它我不知道如何在没有挂起的情况下读取消息。
我正在使用 java。
谁能建议我如何编辑此代码以理解 GET 消息。
【问题讨论】: