本帖最后由 forward 于 2013-8-24 21:22 编辑
- package day23;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- class Send2 {
- public static void main(String[] args) throws Exception {
- DatagramSocket ds = new DatagramSocket();
- try {
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line = bufr.readLine()) != null) {
- if("over".equals(line))
- break;
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.103"), 10000);
- ds.send(dp);
- }
- ds.close();
- } catch(Exception e) {
- throw new RuntimeException("发送失败");
- }
- }
- }
- class Rec2 {
-
- public static void main(String[] args) throws Exception {
- DatagramSocket ds = new DatagramSocket(10000);
- try {
- while(true) {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf, buf.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostAddress();
- String data = new String(dp.getData(), 0, dp.getLength());
- int port = dp.getPort();
- System.out.println(ip + "::" + data + "::" + port);
- }
- } catch(Exception e) {
- System.out.println("接收失败");
- }
- // ds.close();
- }
- }
复制代码 java文件名是ChatDemo2
输入javac ChatDeme2.java可以通过编译
紧接着输入java Rec2时就报错
输入java Send2也报错
为什么啊?!
|
|