2017-10-05 145 views
-1

我想發送字節,並通過我的套接字連接接收它們,但他們也沒有做。我不確定它是否與我發送字節和字符串的方式有關,或者因爲我不知道如何從服務器和客戶端讀取。我的套接字不發送或接收bytearrays

客戶

public class Client implements Runnable { 

private Socket socket; 
private ByteArrayOutputStream buffer; 
private OutputStream output; 
private Stage stage; 

public Client() { 
    try { 
     this.socket = new Socket("localhost", 1337); 
     this.socket.setTcpNoDelay(true); 
     this.socket.setKeepAlive(true); 
     this.output = this.socket.getOutputStream(); 
     InputStream input = this.socket.getInputStream(); 
     this.buffer = new ByteArrayOutputStream(); 
     Thread connection = new Thread(this); 
     connection.start(); 
     this.sendPacket(0, ByteBuffer.allocate(16 + "TEST".length()).putInt("TEST".length()).put("TEST".getBytes(Constants.UTF8)).array()); 
     System.out.println("[CLIENT] Successfully connected to server."); 
    } catch (Exception e) { 
     IOUtils.output("[CLIENT] Error when connecting to server."); 
     System.exit(1337); 
    } 
} 

@Override 
public void run() { 
    try { 
     while (this.connected()) { 
      byte[] bytes = this.buffer.toByteArray(); 
      Constants.received += bytes.length; 
      if (bytes.length < 8) return; 
      ByteBuffer cbuf = ByteBuffer.wrap(bytes); 
      int size = cbuf.getInt(); 
      int id = cbuf.getInt(); 
      if (bytes.length < size + 8) continue; 
      byte[] data = Arrays.copyOfRange(bytes, 8, 8 + size); 
      this.processPacket(id, data); 
      this.buffer.close(); 
      (this.buffer = new ByteArrayOutputStream()).write(bytes, 8 + size, bytes.length - 8 - size); 
     } 
     System.out.println("[CLIENT] Disconnected from server."); 
     System.exit(1337); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void processPacket(int id, byte[] bytes) { 
    ByteBuffer data = ByteBuffer.wrap(bytes); 
    if (id == 0) { 
    System.out.println("Received packet from server with id 0"); 
    } else if (id == 1) { 
    System.out.println("Received packet from server with id 1"); 
    } 
} 

private void sendPacket(int id, byte[] data) { 
    try { 
     ByteBuffer bytebuffer = ByteBuffer.allocate(8 + data.length); 
     bytebuffer.putInt(data.length); 
     bytebuffer.putInt(id); 
     bytebuffer.put(data); 
     byte[] bytes = bytebuffer.array(); 
     Constants.sent += bytes.length; 
     this.output.write(bytes); 
     this.output.flush(); 
    } catch (IOException e) { 
     try { 
      socket.close(); 
     } catch (IOException io) { 
      IOUtils.output("[CLIENT] Error with client."); 
      System.exit(1337); 
     } 
    } 
} 

private boolean connected() { 
    return this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket.isOutputShutdown() && !this.socket.isClosed(); 
} 


} 

ServerHandler

public class Handler implements Runnable { 

private Socket socket; 
private ByteArrayOutputStream buffer; 
private OutputStream output; 

public Handler(Socket socket) { 
    this.socket = socket; 
    try { 
     this.output = this.socket.getOutputStream(); 
     InputStream input = this.socket.getInputStream(); 
     this.buffer = new ByteArrayOutputStream(); 
     Thread connection = new Thread(this); 
     connection.start(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void run() { 
    try { 
     IOUtils.output("[HANDLER] Connection from " + socket.getInetAddress()); 
     while (connected()) { 
      byte[] bytes = this.buffer.toByteArray(); 
      if (bytes.length < 8) return; 
      ByteBuffer buffer = ByteBuffer.wrap(bytes); 
      int size = buffer.getInt(); 
      int id = buffer.getInt(); 
      if (bytes.length < size + 8) continue; 
      byte[] data = Arrays.copyOfRange(bytes, 8, 8 + size); 
      this.processPacket(id, data); 
      this.buffer.close(); 
      (this.buffer = new ByteArrayOutputStream()).write(bytes, 8 + size, bytes.length - 8 - size); 
     } 
     IOUtils.output("[HANDLER] Client ended connection - " + socket.getInetAddress()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void sendPacket(int id, byte[] data) { 
    try { 
     ByteBuffer bytebuffer = ByteBuffer.allocate(8 + data.length); 
     bytebuffer.putInt(data.length); 
     bytebuffer.putInt(id); 
     bytebuffer.put(data); 
     byte[] bytes = bytebuffer.array(); 
     this.output.write(bytes); 
     this.output.flush(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void processPacket(int id, byte[] bytes) { 
    ByteBuffer data = ByteBuffer.wrap(bytes); 
    if (id == 0) { 
     IOUtils.output("Recieved packet with id 0"); 
    } else if (id == 1) { 
     //TODO: authenticate user. 
    } 
} 

private boolean connected() { 
    return this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket.isOutputShutdown() && !this.socket.isClosed(); 
} 

} 

服務器

public class Server implements Runnable { 

private int port; 
private ServerSocket sock; 

public Server(int port) { 
    this.port = port; 
    launch(); 
} 

private void launch() { 
    this.run(); 
} 

@Override 
public void run() { 
    try { 
     sock = new ServerSocket(port); 
     System.out.println("[SERVER] Server started"); 
     while(!sock.isClosed()) { 
      new Handler(sock.accept()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

} 

我覺得這個問題可能與ByteArrayOutputStream。我想使用ByteBuffer,因爲我聽說它比正常的DataInput和輸出流快得多。

回答

1

你永遠不會調用Socket#讀取你的run方法......如果你沒有閱讀任何你沒有任何東西在你的循環中工作,甚至當你連接!

看看本教程的套接字:

https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

+0

我明白你的意思,但我能讀什麼我發送的字節不是整數。 inputstream.read返回一個int。 –

+0

@OmarAhmed看看方法描述,read()讀取字節並用int來表示它,你也有方法讀取(byte [])它返回你讀取的數字字節 – user902383

+0

@ user902383我需要得到bytearrays而不是字節 –

0

您的服務器代碼應該使用ServerSocket而不是插座。套接字用於表示Java中的客戶端套接字對象。

+0

它使用ServerSocket我的處理程序中的其他套接字用於獲取客戶端信息(它連接正常)問題是發送和接收字節數組 –

+0

您發佈的代碼不使用ServerSocket任何地方。如果您發佈服務器和客戶端的實際代碼,您將更快獲得幫助。 – SCCC

+0

我剛剛更新了它應該在那裏的帖子 –

相關問題