本人在做一个有关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);