السلام عليكم ورحمة الله تعالى وبركاته
المطلوب عمل كود لنقل ملف صورة او ملف نص من المرسل للمستقبل اي client to server باستخدام طريقة UDP File Transfer
وعملت الكود ولكن للاسف مابينقل الملف بس اعمل run للكود
وهي الكود ..
receiver
import java.io.*;
import java.net.*;
class Receiver {
public static void main(String args[]) throws Exception
{
//create a datagram socket and bind it with a suitable port
DatagramSocket socket = new DatagramSocket(3000);
//array of byte used to store received data
byte[] receiveData = new byte[128];
System.out.println("Waiting for client file ...\n");
// create an empty datagramPacket to store received data and length
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
FileOutputStream outStream = new FileOutputStream("image.GIF");
}
}أما كود الـ client وفيه أخطاء :(
import java.io.*;
import java.net.*;
class Sender {
private static Object sendData;
private static Object clientSocket;
public static void main(String args []) throws Exception
{
//create a datagram socket
DatagramSocket socket = new DatagramSocket();
//get the IP address of the receiver
InetAddress IP = InetAddress.getByName("localhost");
//array of byte to store data
FileInputStream fileIn = new FileInputStream("image.GIF");
long fileLen = (new File("image.GIF")).length();
int intFileLen = (int)fileLen;
byte[] byteArray = new byte[intFileLen];
fileIn.read(byteArray);
fileIn.close();
sendData = sentence.getBytes();
//create a datagram packet and give it the requierd parameters
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IP, 3000);
//function used to send data
socket.send(sendPacket);
// close the datagramsocket and terminate the connection
clientSocket.close();
}
}إذا حد ممكن يقولي وين الخطأ .. بكون ممنونه له كثير