?? 0095386dfd60001d1634a073c72f607a
字號:
import java.net.*;
import java.io.*;
public class UDPClientDemo {
private DatagramSocket s;
private InetAddress hostAddress;
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf, buf.length);
public UDPClientDemo() {
try {
s = new DatagramSocket();
hostAddress = InetAddress.getByName("localhost");
System.out.println("Client start....");
while (true) {
String strOutMess = "";
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
strOutMess = stdin.readLine();
if (strOutMess.equals("bye"))
break;
String strOut = "[Client say]:" + strOutMess;
byte[] buf = strOut.getBytes();
DatagramPacket out = new DatagramPacket(buf, buf.length,
hostAddress, 4200);
s.send(out);
s.receive(dp);
String strReceived = "(" + dp.getAddress() + ":" + dp.getPort()
+ ")" + new String(dp.getData(), 0, dp.getLength());
System.out.println(strReceived);
}
} catch (UnknownHostException e) {
System.out.println("未找到服務(wù)器");
System.exit(1);
} catch (SocketException se) {
System.out.println("打開套接字錯誤!");
se.printStackTrace();
System.exit(1);
}
catch (IOException ie) {
System.err.println("IO error!");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -