package com.myudp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestUdpRece {
private void udpReceive(){
DatagramSocket dp = null;
try {
dp = new DatagramSocket(8888);
while(true){
byte []buf = new byte[1024];
System.out.println("wait send.....................");
DatagramPacket dataPackage = new DatagramPacket(buf, buf.length);
try {
dp.receive(dataPackage);
System.out.print(dataPackage.getAddress().getHostAddress()+":");
System.out.println(new String(dataPackage.getData(),0,dataPackage.getLength()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(dp!=null){
dp.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// TestUdpRece test = new TestUdpRece();
// test.udpReceive();
}
}
package com.myudp;
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.SocketException;
import java.net.UnknownHostException;
public class TestUdpSend {
// try {
// InetAddress address = InetAddress.getLocalHost();
// System.out.println(address.getHostAddress());
// System.out.println(address.getHostName());
// } catch (UnknownHostException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
/*
* 用UDP协议发送一断文字
*/
// private void sendMessage(){
// DatagramSocket dp = null;
// try {
// dp = new DatagramSocket();
//
// BufferedReader fis = new BufferedReader(new InputStreamReader(System.in));
// String message = null;
// try {
// while((message=fis.readLine())!=null){
// System.out.print("请输入:");
// if(message.equals("over")){
// System.out.println("end");
// break;
// }
// byte []buf = message.getBytes();
// DatagramPacket dataPackage = new DatagramPacket(buf, buf.length, InetAddress.getByName("10.40.35.135"), 8888);
// try {
// dp.send(dataPackage);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// } catch (SocketException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally{
// if(dp!=null){
// dp.close();
// }
// }
//
// }
static class Rece implements Runnable{
private DatagramSocket dp = null;
public Rece(DatagramSocket dp){
this.dp = dp;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.print("please input:");
byte []buf = new byte[1024];
DatagramPacket dataPackage = new DatagramPacket(buf, buf.length);
try {
dp.receive(dataPackage);
//输出
System.out.println(dataPackage.getAddress()+":"+new String(dataPackage.getData(),0,dataPackage.getLength()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static class Write implements Runnable{
private DatagramSocket dp = null;
public Write(DatagramSocket dp){
this.dp = dp;
}
@Override
public void run() {
// TODO Auto-generated method stub
String str = null;
BufferedReader fis = new BufferedReader(new InputStreamReader(System.in));
try {
while((str=fis.readLine())!=null){
byte []buf = str.getBytes();
try {
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length,InetAddress.getByName("10.40.35.135"), 8888);
try {
dp.send(datagramPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// DatagramSocket dpWrite;
// DatagramSocket dpRece;
// try {
// dpWrite = new DatagramSocket();
// dpRece = new DatagramSocket(8888);
// new Thread(new Rece(dpRece)).start();
// new Thread(new Write(dpWrite)).start();
// } catch (SocketException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// }
}
}
} |
|