问题出在哪儿咧?两端怎么不能实现通信啊?高手指教下啊!
package com.baidu.www;
import java.net.*;
import java.io.*;
public class UDPClient {
//客户端向服务器端发数据报
public static void main(String[]args)throws Exception{
DatagramSocket ds= new DatagramSocket(8001);//本地端口
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("客户端正在等待键盘输入数据+++");
while(true){
String str=br.readLine();
byte[] bf=str.getBytes();
DatagramPacket dp=new DatagramPacket( bf, bf.length,8000 );
ds.send(dp);
if((str==null)||str.equals("bye"))
break;
}
ds.close();
}
}
package com.baidu.www;
import java.net.*;
public class UDPServer {
public static void main(String[] args)throws Exception {
byte[] bf=new byte[1024];
DatagramSocket ds= new DatagramSocket(8001);
//DatagramPacket dp=new DatagramPacket(bf,bf.length);
System.out.println("服务器端正在等待客户端发送数据+++++");
while(true){
DatagramPacket dp=new DatagramPacket(bf,bf.length);
ds.receive(dp);
String st=new String(dp.getData(),0,dp.getLength());
System.out.println(st);
if((st==null)||st.equals("bye"))
break;
}
ds.close();
}
}
}
------解决方案--------------------
一次通讯
import java.io.*;
import java.net.*;
public class TestUDPClient {
public static void main(String[] args) {
byte[] buf = "hello".getBytes();
DatagramPacket dp = null;
DatagramSocket ds = null;
try {
dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1", 5555));
ds = new DatagramSocket(6666);
ds.send(dp);
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class TestUDPServer {
public static void main(String[] args) {
byte[] buf = new byte[1024];
DatagramPacket dp = null;
DatagramSocket ds = null;
try {
dp = new DatagramPacket(buf,buf.length);
ds = new DatagramSocket(5555);
ds.receive(dp);
system.out.println(new String(dp.getData()));
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
package com.baidu.www;
import java.net.*;
public class UDPServer {
public static void main(String[] args)throws Exception {
byte[] bf=new byte[1024];
DatagramSocket ds= new DatagramSocket(8001);
//DatagramPacket dp=new DatagramPacket(bf,bf.length);
System.out.println("服务器端正在等待客户端发送数据+++++");
while(true){
DatagramPacket dp = new DatagramPacket(bf,bf.length);
ds.receive(dp);
String st = new String(dp.getData(),0,dp.getLength());
System.out.println(st);
if((st==null)||st.equals("bye"))
break;
}
ds.close();
}
}
package com.baidu.www;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UDPClient {
//客户端向服务器端发数据报
public static void main(String[]args)throws Exception{
DatagramSocket ds= new DatagramSocket(8888);//本地端口
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("客户端正在等待键盘输入数据+++");
while(true){
String str = br.readLine();
byte[] bf = str.getBytes();