问题描述
客户端packagecstrans;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedInputStream;importjava.io.DataInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;importjava.util.Date;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JTextField;publicclassClientextendsJFrameimplementsActionListener{/****/privatestaticfinallongserialVersionUID=1L;privateJTextFieldtext;//信息接收文本域privateJTextFieldcomplete;//信息接收文本域privateJTextFieldipText;//IP文本框privateJButtonbutton;//发送按钮privateDatagramSocketds=null;privateDatagramPacketdp=null;privatebyte[]data=null;privatebyte[]fileinfor=null;privatebyte[]filecurrent=null;privateStringfilename,filePath;longfilelength,filecurrentlength;privateFilefile=null;privateRandomAccessFileraFile=null;booleanflag=false;publicClient(){setTitle("UDP传输");//设置窗体标题setBounds(200,200,500,300);//窗体定位与大小setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭操作为退出程序setLayout(null);//窗体使用边界布局管理器text=newJTextField("输入文件路径");//实例化信息接收文本域text.setBounds(10,20,150,30);add(text);complete=newJTextField("");//实例化信息接收文本域complete.setBounds(200,20,150,30);add(complete);ipText=newJTextField("115.200.4.95");//实例化IP文本框(请输入你自己IP地址)ipText.setBounds(10,70,150,30);add(ipText);button=newJButton("发送");//实例化发送按钮button.setBounds(50,130,80,30);add(button);setVisible(true);//显示窗体button.addActionListener(this);//添加按钮事件监听器}publicvoidactionPerformed(ActionEventev){try{filePath=text.getText();StringIP=ipText.getText();//获取IP文本框内容,这里为本机ipInetAddressip=InetAddress.getByName(IP);file=newFile(filePath);filename=file.getName();//文件名filelength=file.length();//文件大小fileinfor=newbyte[1024];//文件信息fileinfor=(filename+"::"+filelength).getBytes();sendfile(fileinfor,5555,3666,ip);//发送文件信息给服务器filecurrent=newbyte[1024];receivefile(filecurrent,4466);//接收服务器发来的已传输文件大小filecurrentlength=Long.parseLong(newString(dp.getData(),0,dp.getLength()));;//转化为长整型if(0<filecurrentlength&&filecurrentlength<filelength){//如果文件只传了一部分,则定位到文件位置raFile=newRandomAccessFile(filePath,"rw");raFile.seek(filecurrentlength);flag=true;}DataInputStreamfis=newDataInputStream(newBufferedInputStream(newFileInputStream(file)));intread=0;longa=newDate().getTime();data=newbyte[1024*10];while(true){if(flag)//如果文件只传了一部分就通过下面内容传输{if(read!=-1){read=raFile.read(data);}}else{//如果文件没传过,就按下面内容传输if(fis!=null){read=fis.read(data);}}if(read==-1){System.out.print("用时:");System.out.println(newDate().getTime()-a);break;}sendfile(data,6633,3355,ip);}complete.setText("传输完成");}catch(Exceptione){e.printStackTrace();}finally{if(null!=ds)ds.close();}}publicvoidreceivefile(byte[]bytes,intport)throwsIOException//数据包接收类{ds=newDatagramSocket(port);dp=newDatagramPacket(bytes,bytes.length);ds.receive(dp);}publicvoidsendfile(byte[]bytes,intport1,intport2,InetAddressip)throwsIOException//数据包传送类{ds=newDatagramSocket(port1);dp=newDatagramPacket(bytes,bytes.length,ip,port2);ds.send(dp);}publicstaticvoidmain(String[]args){Clientclient=newClient();client.setVisible(true);}}
————————————————————————————————————————————————————服务器packagecstrans;importjava.io.BufferedOutputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;importjava.net.SocketException;importjava.util.StringTokenizer;importjavax.swing.JFrame;importjavax.swing.JProgressBar;importjavax.swing.JTextField;publicclassServer{privateJProgressBarprogressBar;privateJTextFieldtext;//信息接收文本域privateDatagramSocketds=null;privateDatagramPacketdp=null;privateFilefile=null;privateStringfilecurrentlength;publicServer(){JFrameframe=newJFrame();frame.setBounds(200,200,500,300);frame.setLayout(null);frame.setTitle("UDP传输");//设置窗体标题frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭操作为退出程序frame.setLayout(null);//窗体使用边界布局管理器text=newJTextField("D:\");//实例化信息接收文本域text.setBounds(10,20,150,30);frame.add(text);progressBar=newJProgressBar();progressBar.setBounds(10,150,300,30);progressBar.setStringPainted(true);progressBar.setMaximum(100);//设置最大值progressBar.setMinimum(0);//设置最小值progressBar.setValue(0);//设置初始值frame.add(progressBar);frame.setVisible(true);//显示窗体serverstar();}privatevoidserverstar(){Runnablerunnable=newRunnable(){//创建线程接收publicvoidrun(){try{InetAddressip=InetAddress.getLocalHost();//本地ipbyte[]bytename=newbyte[1024];receivefile(bytename,3666);//接收客户端发来的文件名称和文件大小Stringfileinfor=newString(dp.getData(),0,dp.getLength());//文件名称和文件大小信息StringTokenizert=newStringTokenizer(fileinfor,"::");Stringfilename=t.nextToken();//文件名file=newFile("D:\"+filename);if(file.exists())//文件是否存在{filecurrentlength=String.valueOf(newFile("D:\"+filename).length());//文件当前大小}else{filecurrentlength="0";}byte[]sendlength=newbyte[1024];sendlength=filecurrentlength.getBytes();sendfile(sendlength,5555,4466,ip);//把文件当前大小发给客户端DataOutputStreamfileOut=newDataOutputStream(newBufferedOutputStream(newFileOutputStream("D:\"+filename)));inti=0;while(i==0){byte[]receiveByte=newbyte[1024*10];receivefile(receiveByte,3355);//接收数据包i=dp.getLength();if(i>0){fileOut.write(receiveByte,0,i);fileOut.flush();i=0;//循环接收}progressBar.setValue((int)((receiveByte.length)/(newFile("D:\"+filename).length())*100));//进度条}}catch(SocketExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(null!=ds)ds.close();}}};newThread(runnable).start();}publicvoidreceivefile(byte[]bytes,intport)throwsIOException//接收数据包类{ds=newDatagramSocket(port);dp=newDatagramPacket(bytes,bytes.length);ds.receive(dp);}publicvoidsendfile(byte[]bytes,intport1,intport2,InetAddressip)throwsIOException//发送数据包类{ds=newDatagramSocket(port1);dp=newDatagramPacket(bytes,bytes.length,ip,port2);ds.send(dp);}publicstaticvoidmain(String[]args){Serverserver=newServer();}}
报错信息java.net.SocketException:UnrecognizedWindowsSocketserror:0:Cannotbindatjava.net.PlainDatagramSocketImpl.bind0(NativeMethod)atjava.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:82)atjava.net.DatagramSocket.bind(DatagramSocket.java:368)atjava.net.DatagramSocket.<init>(DatagramSocket.java:210)atjava.net.DatagramSocket.<init>(DatagramSocket.java:261)atjava.net.DatagramSocket.<init>(DatagramSocket.java:234)atcstrans.Server.sendfile(Server.java:108)atcstrans.Server$1.run(Server.java:69)atjava.lang.Thread.run(Thread.java:619)
解决方案
解决方案二:
函数被调用了两次,端口已经被占用了。
解决方案三:
好像不是,应该是你bind的端口为0,bind失败了。
解决方案四:
什么意思???
解决方案五:
你是不是第二次接收的抛这个异常?
解决方案六:
两次接收端口不一样啊