本帖最后由 sunhaodi 于 2012-6-22 18:36 编辑
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class UdpSend {
public static void main(String[] args) throws IOException {
/* 1.创建udp服务,通过DatagramSocket对象
DatagramSocket ds = new DatagramSocket(9999);
2.确定数据,并封装成数据包,datagramPacket(byte[] buf,int length,InetAddress address,int port)
*/
DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket(10002);
Send d =new Send(ds1);
Rece r = new Rece(ds2);
Thread t1 = new Thread(d);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
class Send implements Runnable{
private DatagramSocket dr ;
public Send(DatagramSocket dr){
this.dr = dr;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String len = null;
while((len = br.readLine())!=null){
if("88".equals(len))
break;
byte [] b=len.getBytes();
DatagramPacket dp = new
DatagramPacket(b,b.length,InetAddress.getByName("192.168.1.104"),10000);
//3.通过socket服务,将已有的数据包发送出去,通过send方法。
dr.send(dp);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(dr!=null)
//4,关闭资源。
dr.close();
}
}
}
class Rece implements Runnable{
private DatagramSocket ds ;
public Rece(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
while(true){
try {
byte [] bu = new byte[1024];
DatagramPacket dp = new DatagramPacket(bu, bu.length);
ds.receive(dp);
String ip= dp.getAddress().getHostAddress();
String s = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"........"+s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是第一个程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class UdpRce {
public static void main(String[] args) throws IOException {
/* 1.创建udp服务,通过DatagramSocket对象
DatagramSocket ds = new DatagramSocket(9999);
2.确定数据,并封装成数据包,datagramPacket(byte[] buf,int length,InetAddress address,int port)
*/
DatagramSocket ds1 = new DatagramSocket();
DatagramSocket ds2 = new DatagramSocket(10000);
Send d =new Send(ds1);
Rece r = new Rece(ds2);
Thread t1 = new Thread(d);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
class Send implements Runnable{
private DatagramSocket dr ;
public Send(DatagramSocket dr){
this.dr = dr;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String len = null;
while((len = br.readLine())!=null){
if("88".equals(len))
break;
byte [] b=len.getBytes();
DatagramPacket dp = new
DatagramPacket(b,b.length,InetAddress.getByName("192.168.1.104"),10002);
//3.通过socket服务,将已有的数据包发送出去,通过send方法。
dr.send(dp);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(dr!=null)
//4,关闭资源。
dr.close();
}
}
}
class Rece implements Runnable{
private DatagramSocket ds ;
public Rece(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
while(true){
try {
byte [] bu = new byte[1024];
DatagramPacket dp = new DatagramPacket(bu, bu.length);
ds.receive(dp);
String ip= dp.getAddress().getHostAddress();
String s = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"........"+s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
第二个程序。
聊天好使, 但是 if("88".equals{len})
break;
为什么不管用了。。 打完了 88 也不停止程序, 这个break不是应该能停止程序的嘛?
|