如何在 Java 中使用 UDP 发送文件

2022-01-22 00:00:00 sockets udp 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.

相关文章