前言
UDP是一种无连接的通信方式,要使用UDP通信需要知道目的站的端口号以及IP地址
1: 类 DatagramSocket 和 DatagramPacket 实现了基于 UDP 协议网络程序。 2: UDP数据报通过数据报套接字 DatagramSocket 发送和接收,系统不保证 UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达。 3: DatagramPacket 对象封装了UDP数据报,在数据报中包含了发送端的IP 地址和端口号以及接收端的IP地址和端口号。 4: UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送方和 接收方的连接。如同发快递包裹一样。 注: 本文将以一台计算机进行相互间通信,及模拟一个聊天室,如果需要与其他人通信,那么只需要修改目的端IP地址即可
接收端代码
由于是相互通信,因此需要实现接收以及发送操作,为了能模拟电脑上那种发送资源的情况.我设定了一个判定标志,如果有一方发送了video,那么发送方将进入发送资源状态,而接收端将接收发送端发送来的资源. Receiver端用于接收数据,因此只需要设定自己的端口即可,因为自身的IP地址其实是一个已知量.其余部分的注释再代码中.
package IntnetProgram_ch14.ChatRoom.Finally;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
public class Receiver implements Runnable{
String ReceiveData;
byte[]buffer=new byte[1024];
DatagramPacket repacket;
OutputStreamWriter osw;
private DatagramSocket socket=null;
private String fromWho;
private int port;
public Receiver(String fromWho, int port) {
this.fromWho = fromWho;
this.port = port;
try {
socket=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
StringBuilder sb=new StringBuilder();
while (true) {
try {
osw=new OutputStreamWriter(
new FileOutputStream("hell.txt",true),"UTF-8");
repacket = new DatagramPacket(buffer,0,buffer.length);
socket.receive(repacket);
ReceiveData=new String(repacket.getData(),0, repacket.getLength());
if(!ReceiveData.equals("bye"))
sb.append(ReceiveData);
System.out.println(fromWho+":"+ReceiveData);
if(ReceiveData.equals("bye"))
{
osw.write(sb.toString());
osw.close();
socket.close();
System.exit(0);
}
if(ReceiveData.equals("video"))
{
socket.receive(repacket);
String name=new String(repacket.getData(),0,repacket.getLength());
FileOutputStream fos=new FileOutputStream(new File("D:\\图片\\"+name));
repacket = new DatagramPacket(buffer,0,buffer.length);
do{
socket.receive(repacket);
fos.write(repacket.getData(),0,repacket.getLength());
}while(!(new String(repacket.getData(),0, repacket.getLength()).equals("over")));
System.out.println("图片传输完成!!我收到了");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
发送端代码
package IntnetProgram_ch14.ChatRoom.Finally;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;
public class Sender implements Runnable{
DatagramSocket socket=null;
DatagramPacket packet=null;
String SendData=null;
BufferedReader br;
private int toPort;
private int fromPort;
private String toIP;
private InetSocketAddress inet;
public Sender(int fromPort,int toPort,String toIP) {
this.toPort = toPort;
this.fromPort=fromPort;
this.toIP = toIP;
try {
socket=new DatagramSocket(fromPort);
inet=new InetSocketAddress(toIP,toPort);
br=new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
SendData=br.readLine();
DatagramPacket packet = new DatagramPacket(SendData.getBytes(),0,SendData.getBytes().length,this.inet);
socket.send(packet);
if(SendData.equals("bye"))
{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
System.exit(0);
}
if(SendData.equals("video"))
{
System.out.println("输入要传输的文件的路径");
String path= new Scanner(System.in).nextLine();
System.out.println(".....等待对方接收中");
String houzhui=path.substring(path.lastIndexOf("\\\\"));
packet = new DatagramPacket(houzhui.getBytes(),0,houzhui.length(),this.inet);
socket.send(packet);
FileInputStream fis=new FileInputStream(new File(path));
byte[]readbuffer=new byte[1024];
int len;
while((len=fis.read(readbuffer))!=-1)
{
socket.send(new DatagramPacket(readbuffer,0,readbuffer.length,this.inet));
}
System.out.println("图片发送结束");
packet = new DatagramPacket("over".getBytes(),0,"over".length(),this.inet);
socket.send(packet);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
聊天对象1
用于模拟聊天室中的两个人
package IntnetProgram_ch14.ChatRoom.Finally;
public class TestWife {
public static void main(String[] args) {
new Thread(new Receiver("同学1",7777)).start();
new Thread(new Sender(2828,4444,"localhost")).start();
}
}
聊天对象2
package IntnetProgram_ch14.ChatRoom.Finally;
public class TestWife {
public static void main(String[] args) {
new Thread(new Receiver("同学2",7777)).start();
new Thread(new Sender(2828,4444,"localhost")).start();
}
}
源码下载:
源文件夹
文件夹格式
聊天记录
文件下载结果
|