2011-04-29 125 views
5

我想從客戶端和服務器發送多個字節數組?通過套接字發送多個字節數組

我能夠發送/從客戶端接收一個字節數組,併發送/從服務器接收一個字節數組:

我的代碼是這樣的:

服務器:

Socket sock=null; 
    ByteArrayOutputStream input=null; 
    OutputStream out=null; 
    InputStream in=null; 
    try{ 
     ServerSocket server_sock=new ServerSocket(2972); 


    sock=server_sock.accept(); 

    in = 
     sock.getInputStream(); 
    out=sock.getOutputStream(); 
    }catch(IOException e){ 
     System.out.println(e.getMessage()); 
    } 
String word=""; 

    //1-Receive 

    try{ 

    ByteArrayOutputStream serverinput=new ByteArrayOutputStream(); 
int len=0; 
byte[] buf=new byte[1000]; 
     while ((len = in.read(buf))>=0) { 
       serverinput.write(buf, 0, len); 

     } 

     sock.shutdownInput(); 

     word=new String(serverinput.toByteArray()); 
    System.out.println("Client send 1"+word); 

    }catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 


    String st="Server is a king"; 
try{ 
    out.write(st.getBytes()); 

    out.flush(); 

}catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 

客戶:

Socket sock=null; 
    OutputStream out=null; 
    InputStream in=null; 


    try{ 
     sock=new Socket("127.0.0.1",2972); 
     }catch(IOException e){ 
     System.out.println(e.getMessage()); 
    } 


String word="Hellow World" ; 
    try{ 
    in = 
     sock.getInputStream(); 
    out=sock.getOutputStream(); 
    }catch(IOException e){ 
     System.out.println(e.getMessage()); 
    } 

//1- send 
    try{ 

     System.out.println("Your string is"+word+"converted to byte"+word.getBytes()); 

    out.write(word.getBytes()); 
    out.flush(); 
sock.shutdownOutput(); 
    }catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 

    try{ ByteArrayOutputStream serverinput=new ByteArrayOutputStream(); 
int len=0; 
byte[] buf=new byte[1000]; 
     while ((len = in.read(buf))>=0) { 
       serverinput.write(buf, 0, len); 


     } 
    System.out.println("server send 1 "+new String(serverinput.toByteArray())); 
    System.out.println("Your string is"+word+"converted to byte"+word.getBytes()); 

    }catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 

此代碼工作正常,從客戶端和服務器提交,但它不工作時,我想發送/接收更多的字節數組?

它只在我使用shutdown時工作,因爲客戶端和服務器都讀寫數據。

因此,我不能再次使用套接字通道...是否有其他解決方案? ......這不會導致僵局。

回答

13

你的問題是,你現在沒有辦法說什麼時候一個字節數組結束和下一個開始。 (在你的「一個數組」的解決方案中,字節數組的末尾對應於數據流的結尾,當然,一旦數據流已經結束/關閉,就不能在不創建新的Socket等的情況下重新打開。)

簡單的方法來解決這個是如下,使用圍繞相應的插座流包裹DataOutputStream類和DataInputStream類雙:

  • 要發送的字節數組:

    1. 轉換數據,以字節。

    2. 使用DataOutputStream.writeInt(int)方法發送字節數組大小。

    3. 使用DataOutputStream.write(byte[])方法發送字節數組。

  • 要接收的字節數組:

    1. 使用DataInputStream.readInt()方法,接收字節數組的大小。

    2. 分配所需大小的字節數組。

    3. 重複使用DataInputStream.read(byte[], int, int)方法...將字節接收到字節數組中,直到獲得所有字節。

通過在前面發送的字節數組的大小,你告訴接收機如何讀取的字節數。您可以根據需要多次重複此過程。發送者可以通過簡單地關閉套接字流來指示接收者沒有更多的字節數組要發送。

注 - 這是僞代碼。我假設你有能力將它變成可工作的Java。

不要忘記將BufferedInputStreams和BufferedOutputStreams插入到相應的流鏈中...以減少系統調用開銷。

+1

這是工作...非常感謝。 – 2011-04-29 22:44:32

+0

不錯的答案,它也適用於我!謝謝。 – cabreracanal 2011-10-04 10:21:22

+0

@Javalover ...呃,你想在工作之後分享代碼嗎? – gumuruh 2014-08-21 23:37:44

0

嘗試在DataInputStream和DataOutputStream中封裝套接字流。這應該讓你做你想做的。

+0

我做了,但它不起作用... – 2011-04-29 02:54:32

+0

@Javalover然後你沒有正確地做到這一點,我們很難幫助你,除非你更新帖子以反映你所做的。 – Fredrik 2011-04-29 04:56:23

0

你真的應該看看這個教程:Reading from and Writing to a Socket

這似乎概括瞭如何讀取和寫入套接字。閱讀應該像創建服務器套接字一樣簡單,然後監聽您期望的端口,然後等待數據。

+0

這是不寫或讀插座,它通過網絡轉換數據 – 2011-04-29 02:54:14

0

你也可以創建一個對象來保存你的字節數組,然後用ObjectOutputStream發送出去,然後用writeObject(Object)方法發送信息。