Chinaunix首页 | 论坛 | 博客
  • 博客访问: 738243
  • 博文数量: 251
  • 博客积分: 10367
  • 博客等级: 上将
  • 技术积分: 2750
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-10 14:43
文章分类

全部博文(251)

文章存档

2009年(2)

2008年(86)

2007年(163)

分类: Java

2007-11-08 18:27:36

/**
 * @(#)Client.java
 *
 *
 * 
@author pkgold1
 * 
@version 1.00 2007/4/16
 
*/


import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;



public class Client extends Frame implements  Runnable, ActionListener {
    Button b
=new Button("get picture");
    ImageCanvas canvas;
        
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()
            
{
                
public void windowClosing(WindowEvent e){
                    System.exit(
0);
                }

            }
);
            thread.start();
         }

         
public void actionPerformed(ActionEvent event){
             
byte b[]="please send picture".trim().getBytes();
             
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");
                 
             }

             
catch(Exception e){
                 e.printStackTrace();
                 }

         }

     
public void run()
     
{
         DatagramPacket pack
=null;
         DatagramSocket mailReceive
=null;
         
byte b[]=new byte[8192];
         ByteArrayOutputStream out
=new ByteArrayOutputStream();
         
try {
             pack
=new DatagramPacket(b,b.length);
             mailReceive
=new DatagramSocket(5678);
             System.out.println(
"reay receive data");
                }

                
catch(Exception e){
                    e.printStackTrace();
                    
                }

         
try {
             
while(true){
                 mailReceive.receive(pack);
                 String message
=new String(pack.getData(),0,pack.getLength());
                 
if(message.startsWith("end"))
                 
{
                     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();
         }

         
catch(IOException e){
             e.printStackTrace();
             }

             
     }

     
public static void main(String args[])
     
{
         
new Client();
     }

    
}
import java.awt.*;
import java.awt.event.*;
class ImageCanvas extends Canvas
{
    Image image
=null;
    
{
        setSize(
200,200);
    }

    
public void paint(Graphics g){
        
if(image!=null)
        
{
            g.drawImage(image,
0,0,this);
        }

        
else
        
{
            
this.image=image;
        }

    }

    
public void setImage(Image image)
    
{
        
this.image=image;
    }

    
}
希望对学习java 网络编程的同学有所帮助。我的email : pony1976@sina.com.cn. 欢迎转载,请保留我的email.
阅读(1695) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~