Salut,


Je n'arrive pas à lire ce qui arrive dans ma socket...

nb: je suis obligé de passer par les nio et les bytebuffer


voilà ce que j'ai pour le moment
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package tp01;
 
import java.io.IOException;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
 
 
public class ServeurFTP {
	public static int DEFAULT_PORT = 4000;
 
	protected ServerSocketChannel serverChannel;
	protected Selector selector;
 
	public void openPort() {
		try {
			serverChannel = ServerSocketChannel.open();
			ServerSocket ss = serverChannel.socket();
			InetSocketAddress address = new InetSocketAddress(DEFAULT_PORT);
			ss.bind(address);
			serverChannel.configureBlocking(false);
			selector = Selector.open();
			serverChannel.register(selector, SelectionKey.OP_ACCEPT);
		} catch (IOException ex) {
			ex.printStackTrace();
			return;
		}
	}
 
	protected void processIfAcceptable(SelectionKey key) {
		try {		
			ServerSocketChannel server = (ServerSocketChannel) key.channel();
			SocketChannel client = server.accept();
			System.out.println("Accepted connection from " + client);
			client.configureBlocking(false);
			SelectionKey clientKey = client.register( selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ);
			ByteBuffer buffer = ByteBuffer.allocate(100);
			clientKey.attach(buffer);
		} catch (IOException ex) {
			key.cancel();
			try {
				key.channel().close();
			} catch (IOException cex) { }
		}
	}
 
	protected ByteBuffer processIfReadable(SelectionKey key) {
		try {			
			SocketChannel client = (SocketChannel) key.channel();
			ByteBuffer output = (ByteBuffer) key.attachment();
			client.read(output);
			return output;
		} catch (IOException ex) {
			key.cancel();
			try {
				key.channel().close();
			} catch (IOException cex) { }
			return (ByteBuffer) null;
		}
	}
 
	public void startProcess() {
		while (true) {
			try {
				selector.select();
			} catch (IOException ex) {
				ex.printStackTrace();
				break;
			}
 
			Set readyKeys = selector.selectedKeys();
			Iterator iterator = readyKeys.iterator();
			while (iterator.hasNext()) {
				SelectionKey key = (SelectionKey) iterator.next();
				iterator.remove();
 
				if (key.isAcceptable()) {
					processIfAcceptable(key);
				}
 
				if (key.isReadable()) {
					CharBuffer buf = processIfReadable(key).asCharBuffer();
					System.out.println(buf.length() + " " + buf.capacity() + " " + buf.remaining());
					buf.flip();
					char[] tmp = new char[buf.length()];
					buf.get(tmp);
					String msg = new String(tmp);
 
					if (msg.equals("")) {
						System.out.println("string vide");
						System.out.flush();
					} else {
						System.out.println(msg);
						System.out.flush();						
					}
				}					
			}
		}		
	}
 
	public static void main(String[] args) {
		ServeurFTP myServer = new ServeurFTP();
		myServer.openPort();
		myServer.startProcess();
	}
}

si quelqu'un voit où j'ai faux... ça m'arrangerait beaucoup