Voila, j'ai fait mon serveur et client UDP, mais pour le moment je n'arrive qu'a envoyer des messages.
Hors j'aimerai reussir a envoyer des fichiers. J'ai donc ce code:


SERVEUR

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
public class TopologyUDPServer extends Thread {
 
    protected DatagramSocket socket = null;
    protected BufferedReader in=null;
    protected boolean servstart = true;
	private int servport;
 
	/**
         * Constructor TopologyUDPServer: to create a TopologyUDPServer object
         * @param port: <B>The UDP port number</B>
         */
    public TopologyUDPServer(int port){
		servport = port;
		try {
			socket = new DatagramSocket(servport);
		} catch(IOException ioe){
			System.out.println("Server can't start at port:"+servport);
			servstart=false;
		}
    }
 
	/**
         * Method run: to make server operation
         */
    public void run() {
		if(servstart!=false){
			try{
				//System.out.println("Server started...");		
		        while (servstart) {
		            try {
						//Receive request
		                byte[] buf = new byte[256]; //A modifier pour taille fichier
		                DatagramPacket packet = new DatagramPacket(buf, buf.length);
		                socket.receive(packet);
						String msg_read = new String(packet.getData(), 0, packet.getLength());
 
						//Figure out response
		                buf = "SUCCESS".getBytes();
 
						//Send the response to the client at "address" and "port"
		                InetAddress address = packet.getAddress();
		                int port = packet.getPort();
		                packet = new DatagramPacket(buf, buf.length, address, port);
		                socket.send(packet);
 
		            } catch (IOException e) {
						System.out.println("UDP server error: port "+servport+" not allowed");
						servstart = false;
		            }
		        }
			}
			catch(Exception e){
				System.out.println("Server UDP error: can't create server!");
			}
			finally{
				try{
					socket.close();
				} catch(Exception e){
					System.out.println("Server UDP error: can't close connection!");
				}
			}
		}
 
    }
}
CLIENT

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 
package tcp;
 
import java.io.*;
import java.net.*;
 
public class TopologyUDPClient {
	private int clientport;
	private String serverhost;
	private DatagramSocket socket;
 
	/**
         * Constructor: TopologyUDPClient: to make a TopologyUDPClient object
         * @param host: <B>The server host name</B>
         * @param port: <B>The server port number</B>
         */
	public TopologyUDPClient(String host, int port){
		clientport = port;
		serverhost = host;
	}
 
	/**
         * Method startclient: to start client operation
         */
    public void startclient(){
		long thelongtimer=-999;
 
		try{
	        //Get a datagram socket
	        socket = new DatagramSocket();
 
			//Start a timer to analyze the connection time
			TopologyTimer timeranalyze = new TopologyTimer();
			timeranalyze.start();
 
			/////////////////////////////
//			File filetoread = new File("md5summer.exe");
//			RandomAccessFile lecture = new RandomAccessFile(filetoread,"r");
//			byte[] buf = new byte[(int)lecture.length()];
//			for(int i = 0 ; i < lecture.length() ; i++) {
//			    buf[i] = lecture.readByte();
//			}
			////////////////////////////
 
	        //Send request
	        byte[] buf = new byte[256];//A modifier pour taille fichier
			InetAddress address = InetAddress.getByName(serverhost);
			String messagetosend="Sending a very lon string to analyze the connection quality" +
    			"test test test test test test test test test test test test test " +
    			"test test test test test test test test test test test test test " +
    			"test test test test test test test test test test test test test " +
	    		"test test test test test test test test test test test test test " +
	    		"test test test test test test test test test test test test test " +
	    		"test test test test test test test test test test test test test " +
	    		"test test test test test test test test test test test test test " +
	    		"test test test test test test test test test test test test test " +
		        "test test test test test test test test test test test test test ";
			buf=messagetosend.getBytes();
			DatagramPacket packet = new DatagramPacket(buf, buf.length, address, clientport);
			socket.send(packet);
	        //Get response
	        packet = new DatagramPacket(buf, buf.length);
	        socket.receive(packet);
 
		    //Display response
	        String received = new String(packet.getData());
	        System.out.println("UDP server answer: " + received.substring(0,7));
 
			//Terminate timer analyze
			timeranalyze.end();
			long timervalue;
			if ((timervalue= timeranalyze.duration())>=0){
				thelongtimer=timervalue;
			}
			System.out.println("Timer: "+ thelongtimer+"ms");
 
		} catch(IOException ioe){
			System.out.println("Client UDP error: can't create UDP connection!");
		}
		finally{
			try{
				socket.close();
			} catch(Exception e){
				System.out.println("Client UDP error: can't close connection!");
			}
		}
    }
}
Pour envoyer le fichier j'avais pensé utiliser ce code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
		/////////////////////////////
//			File filetoread = new File("md5summer.exe");
//			RandomAccessFile lecture = new RandomAccessFile(filetoread,"r");
//			byte[] buf = new byte[(int)lecture.length()];
//			for(int i = 0 ; i < lecture.length() ; i++) {
//			    buf[i] = lecture.readByte();
//			}
//			System.out.println("1111111\n"+(int)lecture.length());
			////////////////////////////
Et de cette façon j'aurai envoyer mon tableau de byte, hors rien ne marche, le client ne peut terminer l'envoie du fichier.

Quelqu'un a t'il une explication??

Merci