2011-04-29 123 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)方法发送信息。