关于Socket的问题 怎么把Socket1的OutputStream中的数据放到Socket1的InputStream中??????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 中间缓冲一下 先读取到string里面去 我的情况是Socket0发送数据到Socket1,然后Socket1再把收到的数据发送到另一个Socket2,所以我想把Socket1的OutputStream中的数据放到Socket1的InputStream中,这样不就可以把收到的数据发出去了吗?怎么弄啊 在Socket1中先用Socket1的输入流接受Socket0的数据到一个字节数组或者String中(看你用的是什么流了)然后再用Socket1调用Socket1的输出流将这个字节数组或者String中的内容写入到Socket2 如果放到byte数组中怎么写啊 类似于下面的代码:byte[] buf = new byte[[100];//定义一个长度为100的字节数组int len = 0;while((len=in.read())!=-1){ os.wirte(buf,0,len);}这是用字节流进行IO操作时用的 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(); 刚才写了代码了,提交失败。哎~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();当然,还有一个字节一个字节的读写的,不过,效率不高。最后,还有使用管道通信的,貌似不常用。 我是新手,正开始学,我想问一下,这个是在一台计算机实现的吧,如果是3台计算机也就是A发给B,B收到后在发给C,它们的区别在哪呢? 什么叫“在一台计算机实现的” ?A发送给B的代码,以及C的接收代码,我觉得并没有什么难度,所以,并没有考虑也贴出来。我写的代码,主要就是转发端的代码。也就是B转发给C的部分代码。这个,应该一看就明白的。其实,就是一个输入流的读取,并且写到输出流中去。当然,上述的A、B、C三者的程序,都可以部署到同一台电脑上面进行测试运行。分开来运行,也并没有什么太大的差别(NAT情况除外)。 你看看我的代码,为什么收到消息没反应啊,应该打印出来才对啊,哪的毛病???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("连接服务器失败"); } }} 初学者的初级问题,望能给予指点 SSLSocket通信问题 诡异的Lucene:控制台查询正常,web不正常 如何把矩形轮廓添加到 GeneralPath中 使用ObjectOutputStream 向文件进行多次追加写时,如何使后面启动的写入操作不写头标志? 系统自动提供默认构造器的可见度 改造一个小功能,先给100分,完了再加100分! Vector里元素对调问题! 请教一个对象拷贝问题 用java开发过的比较优秀的大型系统有哪些,望高手给举几个例子啊 如何终止一个处理网络访问的线程 用计算机颠覆你小学学的知识 2-1你都不知道等于几。。。
然后再用Socket1调用Socket1的输出流将这个字节数组或者String中的内容写入到Socket2
如果放到byte数组中怎么写啊
byte[] buf = new byte[[100];//定义一个长度为100的字节数组
int len = 0;
while((len=in.read())!=-1){
os.wirte(buf,0,len);
}这是用字节流进行IO操作时用的
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();
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();当然,还有一个字节一个字节的读写的,不过,效率不高。
最后,还有使用管道通信的,貌似不常用。
这个,应该一看就明白的。
其实,就是一个输入流的读取,并且写到输出流中去。当然,上述的A、B、C三者的程序,都可以部署到同一台电脑上面进行测试运行。
分开来运行,也并没有什么太大的差别(NAT情况除外)。
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("连接服务器失败");
}
}
}