1)我可以输入文件名并将其从客户端发送到服务器。但是一旦到达我的服务器,即使文件的位置和名称正确,我也无法访问该文件。 2)由于每次传输限制为1500字节,因此我必须以1500字节的小数据包发送数据。但我在发送超过1500个字节的数据时遇到了问题。这也可能是因为我使用的是字节数组。 3)我无法将已阅读并转换为字节的mp3文件转换回mp3文件
我尝试使用其他文件来查看是否出错,但是无论使用哪种文件,结果始终相同。
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) {}
}
}
最后,我希望服务器将mp3文件发送给客户端,然后客户端将创建一个可以打开并播放的mp3文件。