0

1) I am able to input and send my file name from my client to my server. but once it reaches my server I am unable to access the file even though the location and name of the file is correct. 2) since I am limited to 1500 bytes per transmission, I have to send my data in small 1500 byte packets. but I am facing problems sending data with more than 1500 bytes. this could also be because I am using a byte array. 3) I am unable to convert the mp3 file that I have read and converted to bytes back to an mp3 file

I have tried using other files to see if I made a mistake, but no matter what kind of file I use the result is always the same.

import java.io.*;
import java.net.*;
import java.util.*;
class UDPClientThreads {
 public static void main(String args[]) throws Exception
 { 
   String ul;

   Scanner cin = new Scanner(System.in);

   System.out.print("Enter uname: ");
   ul = cin.nextLine();

   long tstart = System.currentTimeMillis();

   DatagramSocket clientSocket=new DatagramSocket();
   InetAddress IPAddress=InetAddress.getByName("localhost");

    byte[] sendData=new byte[1500];
    byte[] receiveData=new byte[1500];

    System.out.println("sent: "+ul);
    sendData=ul.getBytes();
    DatagramPacket sendPacket=
     new DatagramPacket(sendData, sendData.length,IPAddress, 12313);
    clientSocket.send(sendPacket);
    DatagramPacket receivePacket=
     new DatagramPacket(receiveData, receiveData.length);
    clientSocket.receive(receivePacket);

    System.out.println(receivePacket.getData().length);

   clientSocket.close();   
 }
}
import java.io.*;
import java.net.*;
import java.util.*;
import java.nio.file.Files;
public class UDPServerThreads {

  public class UDPClientHandler1 implements Runnable {

   byte[] bytesFromFile;
   InetAddress address;
   int port;

   public UDPClientHandler1(byte[] bytesFromFile, InetAddress address, int port) {
    this.bytesFromFile=bytesFromFile;
    this.address=address;
    this.port=port;
   }

    public void run() {   
     byte[] sendData=new byte[1500];
     try{
      String threadName =
      Thread.currentThread().getName();
      String message="in HandleClient";

      long cstarttime = System.currentTimeMillis();
      System.out.println("before csocket");
      DatagramSocket csocket=new DatagramSocket();

      sendData= bytesFromFile;
      DatagramPacket sendPacket=
        new DatagramPacket(sendData, sendData.length, address, port);
      csocket.send(sendPacket);
      System.out.println("after send in thread "+"IPAddress="+address+" port="+port);
      long cendtime = System.currentTimeMillis();
      System.out.println("time="+(cendtime-cstarttime));
     }
     catch (IOException e) {}
    }
 }

  public void nonStatic(byte[] bytesFromFile, InetAddress address, int port) {
   Thread t = new Thread(new UDPClientHandler1(bytesFromFile,address,port));
     t.start();  
  }

 public static void main(String args[]) throws Exception
  {  
    UDPServerThreads udpserver= new UDPServerThreads();
    try {
     DatagramSocket serverSocket=new DatagramSocket(12313);
     byte[] receiveData=new byte[1500];
     int count=0;
     while(true)
     {
      DatagramPacket receivePacket=
       new DatagramPacket(receiveData, receiveData.length);
      serverSocket.receive(receivePacket);
      System.out.println("after rcv in server");
      String udpmessage=new String(receivePacket.getData());
      System.out.println("sentence"+udpmessage);

       try
    {
      File f = new File(udpmessage);
      byte[] bytesFromFile = Files.readAllBytes(f.toPath());
      System.out.println(bytesFromFile.length);

      InetAddress address=receivePacket.getAddress();
      int port=receivePacket.getPort();
      udpserver.nonStatic(bytesFromFile,address,port);
      count++;
      System.out.println("after start thread"+count);  
    }
    catch(Exception e)
    {
      System.out.println("not working");
    }

     }
   }
   catch (IOException e) {} 
 }
}

At the end I expect my server to send the mp3 file to my client then my client will create an mp3 file that can be opened and played.

chrisuye
  • 11
  • 2
  • Possible duplicate of [sending and receiving UDP packets using Java?](https://stackoverflow.com/questions/10556829/sending-and-receiving-udp-packets-using-java) – sh.seo Apr 26 '19 at 03:47
  • Well you need to break up the file data into 1500-byte chunks; you need to add sequence numbers to the packets; you need to deal with missing or duplicated or out-of-order packets a the receiver. Too broard. – user207421 Apr 26 '19 at 03:55
  • How do I implement that in code though? That is my problem – chrisuye Apr 26 '19 at 04:39

0 Answers0