멀티 플레이어 게임에 문제가 있습니다. 지금 당장은 서버가 현재 레벨을 클라이언트로 보내도록하려고합니다. 서버가 확실히 데이터를 보내고 있지만 클라이언트 응용 프로그램에 도달하지 않습니다.Java selector/SocketChannel 문제
클라이언트 코드 :
public void run() {
while(true)
{
try {
sel.select();
Set readyKeys = sel.selectedKeys();
Iterator itr = readyKeys.iterator();
while(itr.hasNext())
{
SelectionKey key = (SelectionKey) itr.next();
itr.remove();
SocketChannel ch = (SocketChannel) key.channel();
if(key.isReadable())
{
inputbuf.clear();
long bytesRead = ch.read(inputbuf);
inputbuf.flip();
byte[] test = inputbuf.array();
{
for(int i=0; i < inputbuf.remaining(); i++)
{
System.out.print(test[i]+" ");
}
}
byte cmd = 0;
cmd = inputbuf.get();
switch(cmd)
{
case 1:
//do something
case 2:
//do something else
break;
}
}
else if(key.isConnectable())
{
sc.finishConnect();
sc.register(sel, SelectionKey.OP_READ);
}
}
} catch (IOException ex) {
Buildsim.error("Error handling input from server", ex);
}
}
}
서버 코드 :
public RemotePlayer(SocketChannel s)
{
sc = s;
try {
sel = Selector.open();
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_WRITE);
socket = sc.socket();
ip = socket.getInetAddress().toString();
System.out.println("New connection from "+ip);
inputbuf = ByteBuffer.allocate(1048576);
outputbuf = ByteBuffer.allocate(1048576);
inputbuf.clear();
outputbuf.clear();
}
catch(Exception e)
{
Buildsim.error("Connection error: ", e);
}
sendLevel();
}
public void sendObject(GameObject obj)
{
//Sends a packet of the object, then does the same for the object's children
}
public void sendLevel()
{
try {
outputbuf.put((byte) 0x01);
outputbuf.flip();
sc.write(outputbuf);
sendObject(Buildsim.w.parts.get(0)); //root object
}
catch(IOException e)
{
Buildsim.error("Error sending level to "+ip, e);
}
}
클라이언트에 도달 유일한 것은 바이트 0x01로 인은 (레벨 변화를 신호).
미리 감사드립니다.
이 정보를 분석하고 디버깅하려고 시도한 내용을 말씀해주십시오. – bmargulies
내 대답을 편집 했습니까? – rsp