السلام عليكم ورحمة الله
عندي كود tcp لنقل ملف للسيرفر
كيف احوله ل udp
ارجو المساعدة
هادا السيندر
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.Scanner;
public class Sender
{
//declare a ServerSocket
private static ServerSocket serverSocket;
public static void main(String[] args)
{
System.out.println("Opening port...\n");
try
{
//create a ServerSocket with a port number 3000
serverSocket = new ServerSocket(3000);
}
catch(IOException ioEx)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
try
{
//create a new socket to connect with client and accept the connection
Socket link = serverSocket.accept();
//create ObjectOutputStream that enable us to buffer array of bytes
ObjectOutputStream outStream = new ObjectOutputStream(link.getOutputStream());
//creat FileInputStream to obtain input bytes from a file
FileInputStream fileIn = new FileInputStream("image.GIF");
//get the file length
long fileLen = (new File("image.GIF")).length();
//convert the length to int
int intFileLen = (int)fileLen;
//create an array of byte with the file length
byte[] byteArray = new byte[intFileLen];
//Read bytes of data from this input stream into an array of bytes
fileIn.read(byteArray);
//close this file input stream
fileIn.close();
//write the array of byte to the buffer (ObjectOutputStream)
outStream.writeObject(byteArray);
}
catch(IOException e)
{
System.out.println("error...");
}
}
}هادا الريسيفر
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Reciever
{
public static void main(String[] args) throws Exception
{
try
{
//create a socket with the agreed port
Socket link = new Socket("localhost",3000);
//create ObjectOutputStream that enable us to buffer array of bytes
ObjectInputStream inStream = new ObjectInputStream(link.getInputStream());
//create a array of bytes with the inputStream length (length of recieved array)
byte[] byteArray = (byte[])inStream.readObject();
//output stream for writing data to a File
FileOutputStream outStream = new FileOutputStream("image.GIF");
//Write byteArray to this file output stream
outStream.write(byteArray);
//close the connection
link.close();
}
catch(IOException e)
{
System.out.println("error...");
}
}
}