DatagramSocket /** * This class represents a socket for sending and receiving datagram packets. * * <p>A datagram socket is the sending or receiving point for a packet * delivery service. Each packet sent or received on a datagram socket * is individually addressed and routed. Multiple packets sent from * one machine to another may be routed differently, and may arrive in * any order. * * <p>UDP broadcasts sends are always enabled on a DatagramSocket. * In order to receive broadcast packets a DatagramSocket * should be bound to the wildcard address. In some * implementations, broadcast packets may also be received when * a DatagramSocket is bound to a more specific address. * <p> * Example: * <code> * DatagramSocket s = new DatagramSocket(null); * s.bind(new InetSocketAddress(8888)); * </code> * Which is equivalent to: * <code> * DatagramSocket s = new DatagramSocket(8888); * </code> * Both cases will create a DatagramSocket able to receive broadcasts on * UDP port 8888. * * @author Pavani Diwanji * @version 1.90, 01/23/03 * @see java.net.DatagramPacket * @see java.nio.channels.DatagramChannel * @since JDK1.0 */ public class DatagramSocket {
XiXiangHou(西乡侯) 然后呢?详细点,行不?
/* * Server.java * * Created on 2006年8月28日, 下午2:47 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package testSocket;/** * * @author 千里冰封 */ import java.net.*; import java.io.*; public class Server implements Runnable{ DatagramSocket ds; /** Creates a new instance of Server */ public Server() { initSocket(); new Thread(this).start(); } private void initSocket(){ try{ ds=new DatagramSocket(10000); } catch(Exception exe){ exe.printStackTrace(); } } private void read(){ try{ DatagramPacket dp=new DatagramPacket(new byte[1024],1024); System.out.println("马上服务器收到东西了"); ds.receive(dp); System.out.println("已经收到了"); String st=new String(dp.getData()); System.out.println("东西是"+st); System.out.println("对方发来的端口是"+dp.getPort()); send(st,dp.getAddress(),dp.getPort()); } catch(Exception exe){ exe.printStackTrace(); } } private void send(String s,InetAddress net,int port){ try{ System.out.println("马上要发东西给客户端了"); byte[] data=s.getBytes(); DatagramPacket dp=new DatagramPacket(data,data.length); dp.setAddress(net); dp.setPort(port); ds.send(dp); System.out.println("东西已经发给客户端了"); } catch(Exception exe){ exe.printStackTrace(); } } public void run(){ while(true){ try{ Thread.sleep(1000); read(); } catch(Exception exe){ exe.printStackTrace(); } } } public static void main(String args[]){ new Server(); }
}这是服务器端
/* * Client.java * * Created on 2006年8月28日, 下午2:57 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package testSocket;/** * * @author 千里冰封 */ import java.net.*; import java.io.*; public class Client implements Runnable{ private DatagramSocket ds; /** Creates a new instance of Client */ public Client() { initSocket(); send("abcd"); new Thread(this).start(); } private void initSocket(){ try{ ds=new DatagramSocket(20000); ds.connect(InetAddress.getByName("192.168.1.58"),10000); System.out.println("已经收到了"); } catch(Exception exe){ exe.printStackTrace();; } } private String read(){ try{ System.out.println("马上要收服务器的东西了"); DatagramPacket dp=new DatagramPacket(new byte[1024],1024); ds.receive(dp); System.out.println("已经收到了服务器的东西了"); return new String(dp.getData()); } catch(Exception exe){ exe.printStackTrace(); return null; } } private void send(String s){ try{ byte[] data=s.getBytes(); DatagramPacket dp=new DatagramPacket(data,data.length); dp.setAddress(ds.getInetAddress()); dp.setPort(ds.getPort()); ds.send(dp); System.out.println("已经把东西发给服务器了"); } catch(Exception exe){ exe.printStackTrace(); } } public void run(){ while(true){ try{ Thread.sleep(100); String s= read(); send(s); } catch(Exception exe){ exe.printStackTrace(); } } } public static void main(String args[]){ new Client(); } } 这是客户端 先运行服务器端,然后再运行客户端就可以互发了 这是典型的UDP连接 UDP连接的好处就是快,耗的资源少 坏处就是不稳定,可能会丢数据包
package udp;import java.io.*; import java.net.*;class RecieveThread extends Thread { static final int MAX_PACKET_SIZE = 65507; DatagramSocket socket; public RecieveThread(DatagramSocket socket) { this.socket = socket; } public void run() { byte[] buffer = new byte[MAX_PACKET_SIZE]; DatagramPacket data = new DatagramPacket(buffer, buffer.length); while (true) { try { socket.receive(data); } catch (Exception e) { e.printStackTrace(); } byte[] r=new byte[data.getLength()]; System.arraycopy(data.getData(),0,r,0,r.length); String recv = new String(r); System.out.println("收到数据" + data.getLength()+"字节:"+recv); if (recv.equals("exit")) { socket.close(); break; } } } } public class UDPServer { static final int PORT = 9090; public UDPServer() { } public static void main(String[] args) { try { DatagramSocket dsocket = new DatagramSocket(PORT); RecieveThread r = new RecieveThread(dsocket); r.start(); System.out.println("监听服务已经启动!"); } catch (Exception e) { e.printStackTrace(); } } }
/**
* This class represents a socket for sending and receiving datagram packets.
*
* <p>A datagram socket is the sending or receiving point for a packet
* delivery service. Each packet sent or received on a datagram socket
* is individually addressed and routed. Multiple packets sent from
* one machine to another may be routed differently, and may arrive in
* any order.
*
* <p>UDP broadcasts sends are always enabled on a DatagramSocket.
* In order to receive broadcast packets a DatagramSocket
* should be bound to the wildcard address. In some
* implementations, broadcast packets may also be received when
* a DatagramSocket is bound to a more specific address.
* <p>
* Example:
* <code>
* DatagramSocket s = new DatagramSocket(null);
* s.bind(new InetSocketAddress(8888));
* </code>
* Which is equivalent to:
* <code>
* DatagramSocket s = new DatagramSocket(8888);
* </code>
* Both cases will create a DatagramSocket able to receive broadcasts on
* UDP port 8888.
*
* @author Pavani Diwanji
* @version 1.90, 01/23/03
* @see java.net.DatagramPacket
* @see java.nio.channels.DatagramChannel
* @since JDK1.0
*/
public
class DatagramSocket {
然后呢?详细点,行不?
* Server.java
*
* Created on 2006年8月28日, 下午2:47
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package testSocket;/**
*
* @author 千里冰封
*/
import java.net.*;
import java.io.*;
public class Server implements Runnable{
DatagramSocket ds;
/** Creates a new instance of Server */
public Server() {
initSocket();
new Thread(this).start();
}
private void initSocket(){
try{
ds=new DatagramSocket(10000);
}
catch(Exception exe){
exe.printStackTrace();
}
}
private void read(){
try{
DatagramPacket dp=new DatagramPacket(new byte[1024],1024);
System.out.println("马上服务器收到东西了");
ds.receive(dp);
System.out.println("已经收到了");
String st=new String(dp.getData());
System.out.println("东西是"+st);
System.out.println("对方发来的端口是"+dp.getPort());
send(st,dp.getAddress(),dp.getPort()); }
catch(Exception exe){
exe.printStackTrace();
}
}
private void send(String s,InetAddress net,int port){
try{
System.out.println("马上要发东西给客户端了");
byte[] data=s.getBytes();
DatagramPacket dp=new DatagramPacket(data,data.length);
dp.setAddress(net);
dp.setPort(port);
ds.send(dp);
System.out.println("东西已经发给客户端了");
}
catch(Exception exe){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
Thread.sleep(1000);
read();
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public static void main(String args[]){
new Server();
}
}这是服务器端
* Client.java
*
* Created on 2006年8月28日, 下午2:57
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package testSocket;/**
*
* @author 千里冰封
*/
import java.net.*;
import java.io.*;
public class Client implements Runnable{
private DatagramSocket ds;
/** Creates a new instance of Client */
public Client() {
initSocket();
send("abcd");
new Thread(this).start();
}
private void initSocket(){
try{
ds=new DatagramSocket(20000);
ds.connect(InetAddress.getByName("192.168.1.58"),10000);
System.out.println("已经收到了");
}
catch(Exception exe){
exe.printStackTrace();;
}
}
private String read(){
try{
System.out.println("马上要收服务器的东西了");
DatagramPacket dp=new DatagramPacket(new byte[1024],1024);
ds.receive(dp);
System.out.println("已经收到了服务器的东西了");
return new String(dp.getData());
}
catch(Exception exe){
exe.printStackTrace();
return null;
}
}
private void send(String s){
try{
byte[] data=s.getBytes();
DatagramPacket dp=new DatagramPacket(data,data.length);
dp.setAddress(ds.getInetAddress());
dp.setPort(ds.getPort());
ds.send(dp);
System.out.println("已经把东西发给服务器了");
}
catch(Exception exe){
exe.printStackTrace();
}
}
public void run(){
while(true){
try{
Thread.sleep(100);
String s= read();
send(s);
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public static void main(String args[]){
new Client();
}
}
这是客户端
先运行服务器端,然后再运行客户端就可以互发了
这是典型的UDP连接
UDP连接的好处就是快,耗的资源少
坏处就是不稳定,可能会丢数据包
import java.net.*;class RecieveThread extends Thread {
static final int MAX_PACKET_SIZE = 65507;
DatagramSocket socket; public RecieveThread(DatagramSocket socket) {
this.socket = socket;
} public void run() {
byte[] buffer = new byte[MAX_PACKET_SIZE]; DatagramPacket data = new DatagramPacket(buffer, buffer.length); while (true) {
try {
socket.receive(data);
} catch (Exception e) {
e.printStackTrace();
}
byte[] r=new byte[data.getLength()];
System.arraycopy(data.getData(),0,r,0,r.length);
String recv = new String(r);
System.out.println("收到数据" + data.getLength()+"字节:"+recv);
if (recv.equals("exit")) {
socket.close();
break;
} }
}
}
public class UDPServer {
static final int PORT = 9090; public UDPServer() {
} public static void main(String[] args) {
try {
DatagramSocket dsocket = new DatagramSocket(PORT);
RecieveThread r = new RecieveThread(dsocket);
r.start();
System.out.println("监听服务已经启动!");
} catch (Exception e) {
e.printStackTrace();
}
}
}