![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* @(#)Client.java
*
*
* @author pkgold1
* @version 1.00 2007/4/16
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class Client extends Frame implements Runnable, ActionListener ...{
Button b=new Button("get picture");
ImageCanvas canvas;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Client() ...{
super("I am a client");
setSize(600,600);
setVisible(true);
b.addActionListener(this);
add(b,BorderLayout.NORTH);
canvas=new ImageCanvas();
add(canvas,BorderLayout.CENTER);
Thread thread=new Thread(this);
validate();
addWindowListener(new WindowAdapter()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void windowClosing(WindowEvent e)...{
System.exit(0);
}
});
thread.start();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void actionPerformed(ActionEvent event)...{
byte b[]="please send picture".trim().getBytes();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try...{
InetAddress address=InetAddress.getByName("localhost");
DatagramPacket data=new DatagramPacket(b,b.length,address,7000);
DatagramSocket mailSend=new DatagramSocket();
mailSend.send(data);
System.out.println("client send data");
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(Exception e)...{
e.printStackTrace();
}
}
public void run()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DatagramPacket pack=null;
DatagramSocket mailReceive=null;
byte b[]=new byte[8192];
ByteArrayOutputStream out=new ByteArrayOutputStream();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try ...{
pack=new DatagramPacket(b,b.length);
mailReceive=new DatagramSocket(5678);
System.out.println("reay receive data");
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(Exception e)...{
e.printStackTrace();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while(true)...{
mailReceive.receive(pack);
String message=new String(pack.getData(),0,pack.getLength());
if(message.startsWith("end"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
System.out.println("get all data ok");
break;
}
out.write(pack.getData(),0,pack.getLength());
System.out.println("I am getting data");
}
byte imagebyte[]=out.toByteArray();
out.close();
Toolkit tool=getToolkit();
Image image=tool.createImage(imagebyte);
canvas.setImage(image);
System.out.println("paint image");
canvas.repaint();
validate();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(IOException e)...{
e.printStackTrace();
}
}
public static void main(String args[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
new Client();
}
}
import java.awt.*;
import java.awt.event.*;
class ImageCanvas extends Canvas
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Image image=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
setSize(200,200);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void paint(Graphics g)...{
if(image!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
g.drawImage(image,0,0,this);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.image=image;
}
}
public void setImage(Image image)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.image=image;
}
} 希望对学习java 网络编程的同学有所帮助。我的email : pony1976@sina.com.cn. 欢迎转载,请保留我的email.
阅读(1668) | 评论(0) | 转发(0) |