怎么把Socket1的OutputStream中的数据放到Socket1的InputStream中???????

解决方案 »

  1.   

    中间缓冲一下 先读取到string里面去
      

  2.   

    我的情况是Socket0发送数据到Socket1,然后Socket1再把收到的数据发送到另一个Socket2,所以我想把Socket1的OutputStream中的数据放到Socket1的InputStream中,这样不就可以把收到的数据发出去了吗?怎么弄啊
      

  3.   

    在Socket1中先用Socket1的输入流接受Socket0的数据到一个字节数组或者String中(看你用的是什么流了)
    然后再用Socket1调用Socket1的输出流将这个字节数组或者String中的内容写入到Socket2
      

  4.   


    如果放到byte数组中怎么写啊
      

  5.   

    类似于下面的代码:
    byte[] buf = new byte[[100];//定义一个长度为100的字节数组
    int len = 0;
    while((len=in.read())!=-1){
        os.wirte(buf,0,len);
    }这是用字节流进行IO操作时用的
      

  6.   

    Socket inSock;
    Socket outSock;
    InputStream in  = inSock.getInputStream();
    OutputStream out = outSock.getOutputStream();
    /*方法一*/
    int b;
    while((b=in.read())!=-1){
        out.write(b);
    }/*方法二*/
    byte buffer [] = new byte[1000];
    int len ;
    while((len=in.read(buffer))!=-1){
        out.write(buff,0,len);out.flush();
    }
    方法三:可以尝试管道通信。
    最后,不要忘记in.close();
    out.close();
      

  7.   

    刚才写了代码了,提交失败。哎~InputStream in = socket0.getInputStream();
    OutputStream out = socket1.getOutputStream();
    byte buff [] = new byte[1000];
    int len=0;
    while((len=in.read(buff))!=-1){
          out.write(buff,0,len);
          out.flush();
    }
    in.close();
    out.close();当然,还有一个字节一个字节的读写的,不过,效率不高。
    最后,还有使用管道通信的,貌似不常用。
      

  8.   

    我是新手,正开始学,我想问一下,这个是在一台计算机实现的吧,如果是3台计算机也就是A发给B,B收到后在发给C,它们的区别在哪呢?
      

  9.   

    什么叫“在一台计算机实现的” ?A发送给B的代码,以及C的接收代码,我觉得并没有什么难度,所以,并没有考虑也贴出来。我写的代码,主要就是转发端的代码。也就是B转发给C的部分代码。
    这个,应该一看就明白的。
    其实,就是一个输入流的读取,并且写到输出流中去。当然,上述的A、B、C三者的程序,都可以部署到同一台电脑上面进行测试运行。
    分开来运行,也并没有什么太大的差别(NAT情况除外)。
      

  10.   

    你看看我的代码,为什么收到消息没反应啊,应该打印出来才对啊,哪的毛病???import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.nio.ByteBuffer;public class Client { public static void main(String[] args) throws InterruptedException, IOException {
      //创建套接字对象
      Socket sk=null;  PrintStream ps;  try {
       sk=new Socket("localhost",8900);
       System.out.println("连接成功");
       //ps=new PrintStream(sk.getOutputStream());
      // ps.println("你好"); 
    //Thread.sleep(3000L);
    OutputStream ops = sk.getOutputStream();
    ops.write("helloworld!!!\r\n".getBytes());
    ops.flush();
    InputStream ips = sk.getInputStream();
    ByteBuffer bbb = ByteBuffer.allocate(100);
    for (int c = ips.read(); c != -1; c = ips.read()){
    if (c != 10 && c != 13)
    System.out.println(c);
    bbb.put((byte)c);
    System.out.println(bbb);
    }

    int length = bbb.position();
    bbb.flip();
    if (length != 0)
    System.out.println((new StringBuilder("终端收到信息:")).append(new String(bbb.array(), 0, length)).toString());  } catch (Exception e) {
       // TODO Auto-generated catch block
       System.out.println("连接服务器失败");
      }
     }
    }