Tôi viết một tập Client/Server của chương trìnhNghe cho TCP và UDP yêu cầu trên cùng một cổng
Tùy thuộc vào hoạt động theo yêu cầu của khách hàng, tôi sử dụng làm giao thức TCP hoặc yêu cầu UDP.
Thực hiện phía máy khách là thẳng về phía trước, vì tôi có thể dễ dàng mở kết nối với bất kỳ giao thức nào và gửi yêu cầu đến phía máy chủ.
Ở phía máy chủ, mặt khác, tôi muốn nghe cả hai đối với kết nối UDP và TCP trên cùng một cổng. Hơn nữa, tôi thích các máy chủ để mở thread mới cho mỗi yêu cầu kết nối.
tôi đã áp dụng phương pháp này được giải thích trong: link text
tôi đã mở rộng mẫu mã này bằng cách tạo chủ đề mới cho mỗi giao thức TCP/UDP yêu cầu.
Điều này hoạt động chính xác nếu tôi chỉ sử dụng TCP, nhưng nó không thành công khi tôi cố gắng thực hiện các ràng buộc UDP.
Vui lòng cho tôi bất kỳ đề xuất nào để tôi có thể sửa lỗi này.
tnx
Đây là Bộ luật Server:
public class Server {
public static void main(String args[]) {
try {
int port = 4444;
if (args.length > 0)
port = Integer.parseInt(args[0]);
SocketAddress localport = new InetSocketAddress(port);
// Create and bind a tcp channel to listen for connections on.
ServerSocketChannel tcpserver = ServerSocketChannel.open();
tcpserver.socket().bind(localport);
// Also create and bind a DatagramChannel to listen on.
DatagramChannel udpserver = DatagramChannel.open();
udpserver.socket().bind(localport);
// Specify non-blocking mode for both channels, since our
// Selector object will be doing the blocking for us.
tcpserver.configureBlocking(false);
udpserver.configureBlocking(false);
// The Selector object is what allows us to block while waiting
// for activity on either of the two channels.
Selector selector = Selector.open();
tcpserver.register(selector, SelectionKey.OP_ACCEPT);
udpserver.register(selector, SelectionKey.OP_READ);
System.out.println("Server Sterted on port: " + port + "!");
//Load Map
Utils.LoadMap("mapa");
System.out.println("Server map ... LOADED!");
// Now loop forever, processing client connections
while(true) {
try {
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
// Iterate through the Set of keys.
for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
SelectionKey key = i.next();
i.remove();
Channel c = key.channel();
if (key.isAcceptable() && c == tcpserver) {
new TCPThread(tcpserver.accept().socket()).start();
} else if (key.isReadable() && c == udpserver) {
new UDPThread(udpserver.socket()).start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(e);
System.exit(1);
}
}
}
Mã UDPThread:
public class UDPThread extends Thread {
private DatagramSocket socket = null;
public UDPThread(DatagramSocket socket) {
super("UDPThread");
this.socket = socket;
}
@Override
public void run() {
byte[] buffer = new byte[2048];
try {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String inputLine = new String(buffer);
String outputLine = Utils.processCommand(inputLine.trim());
DatagramPacket reply = new DatagramPacket(outputLine.getBytes(), outputLine.getBytes().length,
packet.getAddress(), packet.getPort());
socket.send(reply);
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
tôi nhận được:
Exception in thread "UDPThread" java.nio.channels.IllegalBlockingModeException
at sun.nio.ch.DatagramSocketAdaptor.receive(Unknown Source)
at server.UDPThread.run(UDPThread.java:25)
10x
Không thế nào? Bạn có thêm thông tin, chẳng hạn như thông báo lỗi hoặc mã mẫu không? –
Vui lòng giải thích chi tiết hơn về "nó không thành công". – BalusC