0
我的服務器套接字有一些問題。我創建一個DatagramSocket在服務器和客戶端之間進行聊天。Java套接字服務器阻塞
public static void main (String[] args) throws IOException {
byte[] send = new byte[1024];
byte[] receive = new byte[1024];
BufferedReader entree;
DatagramSocket serverSocket = null;
InetAddress ip;
InetAddress ipDest;
int port;
try {
serverSocket = new DatagramSocket(8888);
} catch (SocketException e) {
e.printStackTrace();
}
while (true) {
DatagramPacket recu = new DatagramPacket(receive, receive.length);
serverSocket.receive(recu);
String sentence = new String(recu.getData());
ipDest = recu.getAddress();
port = recu.getPort();
System.out.println("Reçu:"+sentence);
entree = new BufferedReader(new InputStreamReader(System.in));
String chaine = entree.readLine();
send = chaine.getBytes();
DatagramPacket dp = new DatagramPacket(send, send.length, ipDest, port);
serverSocket.send(dp);
send = new byte[1024];
receive = new byte[1024];
}
但是我用new BufferedReader(new InputStreamReader(System.in))
得到下一個東西送的,它阻止。所以,我無法接收客戶提供的信息並打印出來。
我該如何安排?
Merci,eo
如果您希望能夠同時發送和接收數據,您必須將其置於不同的線程中。 – Reinard 2012-03-26 13:41:11