import java.io.*;
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供天寧網(wǎng)站建設(shè)、天寧做網(wǎng)站、天寧網(wǎng)站設(shè)計(jì)、天寧網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、天寧企業(yè)網(wǎng)站模板建站服務(wù),十多年天寧做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
import java.lang.*;
import java點(diǎn)虐 .*;
public class uclient
{
private DatagramSocket cli;
private DatagramPacket pac;
private byte sb[];
private String sen;
public uclient()
{
Init();
}
public void Init()
{
try
{
//指定端口號(hào),避免與其他應(yīng)用程序發(fā)生沖突
cli=new DatagramSocket(10002);
sb=new byte[1024];
sen="UDP方式發(fā)送數(shù)據(jù)";
sb=sen.getBytes();
pac=new DatagramPacket(sb,sb.length,InetAddress.getByName("localhost"),10005);
cli.send(pac);
}
catch(SocketException se)
{
se.printStackTrace();
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
public static void main(String args[])
{
new uclient();
}
}
接收端數(shù)據(jù):
//加以改進(jìn)代碼,可以改變接收方式
import java.io.*;
import java.lang.*;
import java點(diǎn)虐 .*;
public class userve
{
private DatagramSocket ser;
private DatagramPacket pac;
private byte rb[];
private String rev;
public userve()
{
Init();
}
public void Init()
{
try
{
ser=new DatagramSocket(10005);
rb=new byte[1024];
pac=new DatagramPacket(rb,rb.length);
rev="";
int i=0;
while(i==0)//無(wú)數(shù)據(jù),則循環(huán)
{
ser.receive(pac);
i=pac.getLength();
//接收數(shù)據(jù)
if(i0)
{
//指定接收到數(shù)據(jù)的長(zhǎng)度,可使接收數(shù)據(jù)正常顯示,開始時(shí)很容易忽略這一點(diǎn)
rev=new String(rb,0,pac.getLength());
System.out.println(rev);
i=0;//循環(huán)接收
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new userve();
}
}
我自己寫的代碼:
import?java.io.IOException;
import?java點(diǎn)虐 .DatagramPacket;
import?java點(diǎn)虐 .DatagramSocket;
import?java點(diǎn)虐 .InetAddress;
import?java點(diǎn)虐 .SocketException;
import?java點(diǎn)虐 .UnknownHostException;
public?class?UdpRecvDemo
{
public?static?void?main(String[]?args)?throws?IOException
{
Recv();
}
public?static?void?Recv()?throws?IOException
{
System.out.println("接受開始......");
DatagramSocket?ds?=?new?DatagramSocket(3388);
while(true)
{
byte[]buf?=?new?byte[1024];
DatagramPacket?dp?=?new?DatagramPacket(buf,buf.length);
ds.receive(dp);
System.out.println(ds);
String?ip?=?dp.getAddress().getHostAddress();
int?port?=?dp.getPort();
String?text?=?new?String(dp.getData(),0,dp.getLength());
if(text.equals("exit"))
{
System.out.println(ip+"退出會(huì)話......");
break;
}
System.out.println(ip+":"+port+"===me?"+text);
}
ds.close();
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java點(diǎn)虐 .ServerSocket;
import java點(diǎn)虐 .Socket;
import java.io.IOException;
import java.util.Date;
class Server
{
public Server()
{
try
{
ServerSocket s=new ServerSocket(8888);
Socket ss=s.accept();
OutputStream out=ss.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
InputStream in=ss.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已經(jīng)連接到服務(wù)器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"
+"你的鏈接端口:"+ss.getLocalPort()+"\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
//接受客戶端信息
class ReadMessage extends Thread
{
private DataInputStream din;
public ReadMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while (true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"客戶端說(shuō):"+str);
if (str.equals("bye"))
{
System.out.println("客戶端下線!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// 發(fā)出服務(wù)器信息
class SendMessage extends Thread
{
private DataOutputStream dout;
public SendMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
InputStreamReader inr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inr);
String str;
try
{
while(true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("服務(wù)器退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java點(diǎn)虐 .Socket;
import java.io.IOException;
import java.util.Date;
class Client
{
public Client()
{
try
{
Socket s=new Socket("192.168.1.2",8888);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("服務(wù)器你好!我是客戶端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
class ReaderMessage implements Runnable
{
private DataInputStream din;
public ReaderMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while(true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"服務(wù)器說(shuō):"+str);
if (str.equals("bye"))
{
System.out.println("服務(wù)器已經(jīng)關(guān)閉,此程序自動(dòng)退出!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable
{
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
String str;
InputStreamReader inf=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inf);
try
{
while (true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("客戶端自己退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
發(fā)送步驟:
使用 DatagramSocket(int port) 建立socket(套間字)服務(wù)。
將數(shù)據(jù)打包到DatagramPacket中去
通過(guò)socket服務(wù)發(fā)送 (send()方法)
關(guān)閉資源
import?java.io.IOException;??
import?java點(diǎn)虐 .*;??
public?class?Send?{??
public?static?void?main(String[]?args)??{??
DatagramSocket?ds?=?null;??//建立套間字udpsocket服務(wù)??
try?{??
ds?=?new?DatagramSocket(8999);??//實(shí)例化套間字,指定自己的port??
}?catch?(SocketException?e)?{??
System.out.println("Cannot?open?port!");??
System.exit(1);???
}??
byte[]?buf=?"Hello,?I?am?sender!".getBytes();??//數(shù)據(jù)??
InetAddress?destination?=?null?;??
try?{??
destination?=?InetAddress.getByName("192.168.1.5");??//需要發(fā)送的地址??
}?catch?(UnknownHostException?e)?{??
System.out.println("Cannot?open?findhost!");??
System.exit(1);???
}??
DatagramPacket?dp?=???
new?DatagramPacket(buf,?buf.length,?destination?,?10000);????
//打包到DatagramPacket類型中(DatagramSocket的send()方法接受此類,注意10000是接受地址的端口,不同于自己的端口?。??
try?{??
ds.send(dp);??//發(fā)送數(shù)據(jù)??
}?catch?(IOException?e)?{??
}??
ds.close();??
}??
}
接收步驟:
使用 DatagramSocket(int port) 建立socket(套間字)服務(wù)。(我們注意到此服務(wù)即可以接收,又可以發(fā)送),port指定監(jiān)視接受端口。
定義一個(gè)數(shù)據(jù)包(DatagramPacket),儲(chǔ)存接收到的數(shù)據(jù),使用其中的方法提取傳送的內(nèi)容
通過(guò)DatagramSocket 的receive方法將接受到的數(shù)據(jù)存入上面定義的包中
使用DatagramPacket的方法,提取數(shù)據(jù)。
關(guān)閉資源。
import?java點(diǎn)虐 .*;??
public?class?Rec?{??
public?static?void?main(String[]?args)?throws?Exception?{??
DatagramSocket?ds?=?new?DatagramSocket(10000);??//定義服務(wù),監(jiān)視端口上面的發(fā)送端口,注意不是send本身端口??
byte[]?buf?=?new?byte[1024];//接受內(nèi)容的大小,注意不要溢出??
DatagramPacket?dp?=?new?DatagramPacket(buf,0,buf.length);//定義一個(gè)接收的包??
ds.receive(dp);//將接受內(nèi)容封裝到包中??
String?data?=?new?String(dp.getData(),?0,?dp.getLength());//利用getData()方法取出內(nèi)容??
System.out.println(data);//打印內(nèi)容??
ds.close();//關(guān)閉資源?????
}??
}