日期:2014-05-20  浏览次数:20946 次

如何实现图片在网络间的传递?
额,菜鸟,想实现建立通道一直在服务端和客户端传递图片

服务端是这么写的
public void run() {
try
{
while (isConnect)
{
System.out.println("准备接受信息");
BufferedImage image ;
image = ImageIO.read(dis);
System.out.println("服务端已收到图片,准备发送.")
System.out.println(image);
for (int i = 0; i < drawClients.size(); i++)
{
drawClient c = drawClients.get(i);
c.sendImage(image);
}
dis.close();
System.out.println("发送图片完毕");
System.out.println("tt");
}
                         }catch......finally..........

其中sendImage代码如下
public void sendImage(BufferedImage image) {
try
{
dos = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
ImageIO.write(image, "PNG", dos);
System.out.println("已发送给一个客户端");
} catch (IOException e)
{
drawClients.remove(this);
}
}catch......finally..........






然后是客户端
按下发送按钮时的动作
void sendPicture(BufferedImage image)
{
try
{
ImageIO.write(image, "PNG", dos);
System.out.println("图片已发送到服务端");
}catch......finally..........

然后另一个线程里面是这样
public void run() {
try
{
while (isConnection)
{
if(dis != null)
{
BufferedImage image = null;
image = ImageIO.read(dis);
System.out.println("已经接收到图片");
System.out.println(image);
drawBoard.image = image;
}
System.out.println("cc");
}
}catch......finally..........



运行后,控制台显显示如下

_________________________________________________________________________
服务端已收到图片,准备发送到客服端
BufferedImage@6c121f1d: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@19b8e059 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 700 height = 500 #numDataElements 3 dataOff[0] = 2
已发送给一个客户端
发送图片完毕
tt
准备接受信息
服务端无法读取信息!客户端已经关闭程序
Socket is closed
[Ljava.lang.StackTraceElement;@2d20cc56
最后时刻,关闭与客户端连接的通道!
现在客户端的数量0
___________________________________________________________________________
收到图片后通道就关闭了,这是为什么?

客户端方面控制台显示如下

______________________________________________________________________________

已经和画画服务器连接
图片已发送到服务端
send
已经接收到图片
BufferedImage@d4dd758: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@7174d93a transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 700 height = 500 #numDataElements 3 dataOff[0] = 2
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
已经接收到图片
null
cc
.......死循环
___________________________________________________________________________
上面不是阻塞的么,为什么会死循环了,跪求答案,谢了