本人在做一个有关UDP传输的东西~~!这是我们实习的课题,眼看实习马上就要结束了,可是程序却一直卡着进行不下去了!课题的要求就是:一个server,一个client, server 不断的发图片和文本等系列文件~~!server端的界面要有个文件列表供选择发送~~!发送过去的东西要显示客户端~~!最后的效果就时,屏幕里面每隔几秒就出现一张图片~~!我们现在的进度还只做到传一个图片~~!一个文本~~!我们现在遇到了一个问题,以前,我们传输了一个1。53kb的图片,成功传输,成功接收!但是当我们用了一个120kb的图片传输的时候,发现有信息丢失现象,所以我们决定标记每个packet,以及给他们编号,做完后发现不理想,接受到的图片,一半是完整的,另外一半是条纹,意味着我的第二个packet发送不成功~~!
客户端:import java.net.*; 
import java.io.*; 
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;public class BroadcastClient{ 
 static TextArea ta;
 Frame f;
 Panel p,p1;
 public static final 
 int PORT = 1200; 
 public static int size;
public void createFrame() {
 p=new Panel();
 p1=new Panel();
 Icon pp1 = new ImageIcon( "brios91.jpg" );
 JButton b4=new JButton(pp1);
 ta=new TextArea();
 f=new Frame("BroadcastClient");
 p.setLayout(new GridLayout(2,1));
 p.add(b4);
 p.add(ta);
 f.setSize(300,300);
 f.setLayout(new BorderLayout());
 f.add(p,BorderLayout.WEST);
 f.add(p1,BorderLayout.EAST);
 f.setVisible(true);
 f.pack();
   WindowHandler wh = new WindowHandler();
   f.addWindowListener(wh);
 
 }
 public class WindowHandler extends WindowAdapter {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
  }
 }public static void main(String args[]) throws Exception{ 
  BroadcastClient win = new BroadcastClient();
   win.createFrame();
  
 
 MulticastSocket socket; 
 DatagramPacket packet; 
 InetAddress address; 
 address = InetAddress.getByName("230.0.0.1"); 
 socket = new MulticastSocket(PORT); //join a Multicast group and send the group salutations 
        socket.joinGroup(address); 
     System.out.println("joined"); 
        File received = new File("receive.txt"); 
        FileOutputStream fos = new FileOutputStream(received); 
   int num;
   int count=0;
   while (true) {
     packet = new DatagramPacket(new byte[1024], 1024);
  
   // receive the packets  
    socket.receive(packet);   
   
   
 
   num=packet.getLength();
   System.out.println("Length: "+packet.getLength()); 
   count++;
   System.out.println(count);
 
//create a file to put the recieve data 
        byte[] data = new byte[packet.getLength()]; 
        byte[] temp = packet.getData(); //throw out the extra length 
        for (int i = 0; i < data.length; i++) { 
        data[i] = temp[i]; 
         
        } 
    if(count==20)
     {  
   break;
     }
 
  
// Read and display data
     fos.write(data); 
    }
      fos.close();    
       ta.append("\nFile : "+received.getName()+"\nSize :"+received.length()+"byte");  
    
      FileInputStream fis=new FileInputStream(received);
   BufferedReader br=new BufferedReader(new InputStreamReader(fis));
      int b=fis.available();
      int c;
      String st2;
      while((st2=br.readLine())!=null){
    
     System.out.println(st2); 
      ta.append("\n"+st2);
     }}
  
  // main 
} // class Broadcast  
server:import java.net.*; 
import java.io.*; 
import java.util.*; 
import java.io.DataInputStream; 
import java.io.FileInputStream; public class BS{ 
public static final int PORT = 1200;
private static int size;
 
public static void main(String args[]) throws Exception{ 
    MulticastSocket socket; 
 DatagramPacket packet; 
 InetAddress address = InetAddress.getByName("230.0.0.1"); 
 socket = new MulticastSocket(); 
  
// join a Multicast group and send the group salutations 
       socket.joinGroup(address); 
        
          for(;;){
         Thread.sleep(3000);//keep transforing packet// Create a data input stream 
       DataInputStream fis = new DataInputStream(new FileInputStream("bobmgame.txt"));
   size = fis.available();
   int available = size; 
   int count=0;// Read and display data
   while (available>0) {
    byte[] data2 = new byte[1024];
     if (available>1024) 
      fis.read(data2,0,1024); 
     else 
      fis.read(data2,0,available);
 
 
         if 
          (available>= data2.length) 
           packet = new DatagramPacket (data2,data2.length,address,PORT); 
         else 
          packet = new DatagramPacket (data2,available,address,PORT);//Sends the packet 
       socket.send(packet);
       
       System.out.println(packet);
       
  System.out.println(packet.getLength());  
    count++; 
     System.out.println("\ncount: "+count);
  available-=1024;
     }
      
       fis.close(); 
       
} // main 
} // class BroadcastServer详聊:qq38132124,请注java