如何在Java中使用UDP发送文件 [英] How to Send Files using UDP in Java
问题描述
我有一个使用Java进行套接字编程的项目.我们必须编写客户端和服务器代码来传输文件,该代码在编译时没有显示错误,但没有执行,当我输入文件名时它会冻结.
i have a project in socket programming using java. We must write the Client and server Codes to transmit a file , The code shows no error at compiling but doesn't execute , it freezes when i put the name of the file .
我知道UDP并不是传输文件的好主意,但是我必须作为一个项目来做.我的代码是:
I know that UDP is not a good idea for transmitting files but i have to do it as a project My codes are :
客户代码
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
static InetAddress dest;
public static void main(String [] args) throws Exception
{
DatagramSocket clskt = new DatagramSocket();
Scanner input = new Scanner (System.in);
int port =input.nextInt();
System.out.println("Enter Destination Host name");
String hostname=input.next();
dest.getByName(hostname);
int packetcount=0;
System.out.println("Enter The path of the file you want to send");
String path = input.next();
File initialFile = new File(path);
FileInputStream targetStream = new FileInputStream(initialFile);
int filesize=targetStream.available();
//int neededpackets =(int)Math.ceil((double)(size/1024));
byte [] data= new byte[1024];
// counting bytes
for (int i=0;i<1024;i++)
{
data[i]=(byte)targetStream.read();
}
//create a packet
DatagramPacket clpkt=new DatagramPacket(data,data.length,dest,port);
packetcount++;
clskt.send(clpkt);
if(packetcount >neededpackets)
clskt.close();
}
}
服务器代码
import java.io.*;
import java.net.*;
import java.util.*;
class Server1
{
public static void main(String args[])throws Exception
{
System.out.println("Enter Port number !!!");
Scanner input = new Scanner(System.in);
int SPort = input.nextInt();
DatagramSocket srvskt = new DatagramSocket(SPort);
byte[] data =new byte[1024];
System.out.println("Enter a full file name to save data to it ?");
String path = input.next();
System.out.println("file : "+path+" will be created.");
FileOutputStream FOS = new FileOutputStream(path);
DatagramPacket srvpkt = new DatagramPacket(data,1024);
System.out.println("listening to Port: "+SPort);
int Packetcounter=0;//packet counter
while(true)
{
srvskt.receive(srvpkt);
Packetcounter++;
String words = new String(srvpkt.getData());
InetAddress ip= srvpkt.getAddress();
int port = srvpkt.getPort();
System.out.println("Packet # :"+Packetcounter+"
Received from Host / Port: "+ip+" / "+port);
FOS.write(data);
//out16.flush();
if (Packetcounter >=100)
break;
}
FOS.close();//releasing file.
System.out.println("Data has been written to the file !");
}
}
预先感谢所有人.
推荐答案
我乍一看在客户端中看到的是,您尝试使用的 dest
字段从未被统一,它仍然存在空值.您应该编写 dest = InetAddress.getByName(anArgument)
,以便 dest
获得新的InetAddress实例的值.因此,很可能在代码可编译时得到Null指针异常.现在没有,只要未定义 neededpackets
.
What I see at the first glance in the client is that the dest
field that you try to use gets never unitialized, it remains null. You should write dest = InetAddress.getByName(anArgument)
so that the dest
get a value of a new InetAddress instance. So, most likely you'll get the Null pointer exception when your code gets compilable. Now it is not, as long as the neededpackets
is not defined.
这篇关于如何在Java中使用UDP发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!