mina问题
我写一个socket程序访问mina服务器,socket端代码如下:
public static void main(String[] args) {
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
try {
socket =new Socket(HOSTNAME,PORT);
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
short uid=0x0032;
short pwd=0x0032;
String data=getData(uid,16)+getData(pwd,16);
Message msg=new Message(Protocol.CMSG_BET,data);
System.out.println(msg.toString());
out.write(msg.toString());
out.flush();
String line =in.readLine();
System.out.println(line);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务端发送消息的方法如下:
public void sendMessage(IoSession session, Message message) {
short len = (short) (message.datalen + Protocol._HEADERLEN);
ByteBuffer wb = ByteBuffer.allocate(len);
wb.put((message.toString()).getBytes());
wb.flip();
session.write(wb);
}
服务端能接收到消息,可socket端不能用readLine读取到服务端返回的信息。请问各位前辈,这个问题怎么解决。
[解决办法]
我也遇到这个问题, 在服务器端和客户端你的数据只要超过1024就会出现这个问题.
acceptor.getSessionConfig().setReadBufferSize(bodySize);
用这个方法不管用.当数据小于这个的时候没问题.
哎,,共解吧!!