java面试题之----UDP聊天程序

</pre><h2>通过键盘录入获取要发送的信息。将发送和接收分别封装到两个线程中。</h2><pre name="code" class="java">
package cn.hncu.UDP;
<span style="font-size:32px;color:#ff0000;">//发送方</span>
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SendDemo {

	public static void main(String[] args) {
		try {
			DatagramSocket ds = new DatagramSocket(9999);
			String info="SOS,Socket通信信息!";
			byte buf[] = info.getBytes();//用默认码表
			//DatagramPacket类中,有ip地址的构造方法是用来创建发送数据包的
			DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.5"),10000 );//注意,IP和端口都是接收方的
			ds.send(dp);
			ds.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
<span style="font-size:32px;color:#ff0000;">//接收方</span>
<pre name="code" class="java"><span style="font-size:10px;"><span style="color:#ff0000;">package cn.hncu.UDP;

</span>import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveDemo {

	public static void main(String[] args) {
		try {
			DatagramSocket ds = new DatagramSocket(10000);
			
			//把数据接收到dp中
			byte buf[] = new byte[1024];
			//DatagramPacket类中,没有ip地址的构造方法是用来创建接收数据包的
			DatagramPacket dp = new DatagramPacket(buf, buf.length);
			ds.receive(dp);//接收之后,所有的数据都在dp里面
			
			//从dp中解析出我们想要的信息
			//获取ip
			String ip = dp.getAddress().getHostAddress();
			int port = dp.getPort();//端口
			byte data[] = dp.getData();//对方发送的数据
			String info = new String(data);
			System.out.println(ip+":"+port+"----"+info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}</span><span style="font-size:32px;">
</span>
<span style="font-size:32px;color: rgb(255, 0, 0);">//运行放1</span>
<span style="font-size:32px;color: rgb(255, 0, 0);">
</span>
<span style="font-size:10px;">package cn.hncu.UDP;

//11111
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 UDPChat {
	public static void main(String[] args) {
		try {
			DatagramSocket send = new DatagramSocket(10001);
			DatagramSocket receive = new DatagramSocket(10002);

			new Thread(new Send(send)).start();
			new Thread(new Receive(receive)).start();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

class Send implements Runnable {
	private DatagramSocket ds;

	public Send(DatagramSocket send) {
		this.ds = send;
	}

	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			String line = null;
			while ((line = br.readLine()) != null) {
				byte buf[] = line.getBytes();
				// 要用接收方的ip和接收端口
				DatagramPacket dp = new DatagramPacket(buf, buf.length,
						InetAddress.getByName("192.168.1.5"), 10004);
				ds.send(dp);
				if ("over".equals(line)) {
					break;
				}
			}
			ds.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class Receive implements Runnable {
	private DatagramSocket ds;

	public Receive(DatagramSocket receive) {
		this.ds = receive;
	}

	@Override
	public void run() {
		try {
			byte buf[] = new byte[1024];// 大小够存储一行就可以
			while (true) {
				// 接收数据
				DatagramPacket dp = new DatagramPacket(buf, buf.length);
				ds.receive(dp);
				// 解析数据
				String ip = dp.getAddress().getHostAddress();
				String info = new String(dp.getData(), 0, dp.getLength());
				System.out.println(ip + "说:" + info);
				if ("over".equals(info)) {
					System.out.println(ip + "离开聊天室....");
					 break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
</span>

//运行方2
 
<pre name="code" class="java"><span style="font-size:10px;">package cn.hncu.UDP.chat2;
//22222
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 UdpChat {
	public static void main(String[] args) {
		try {
			DatagramSocket send = new DatagramSocket(10003);
			DatagramSocket receive = new DatagramSocket(10004);
			
			new Thread(new Send(send) ).start();
			new Thread(new Receive(receive)).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

class Send implements Runnable{
	private DatagramSocket ds;
	public Send(DatagramSocket send) {
		this.ds = send;
	}

	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while( (line=br.readLine())!=null ){
				byte buf[] = line.getBytes();
				//要用接收方的ip和接收端口
				DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.5"), 10002);
				ds.send(dp);
				if("over".equals(line)){
					break;
				}
			}
			ds.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class Receive implements Runnable{
	private DatagramSocket ds;
	public Receive(DatagramSocket receive) {
		this.ds = receive;
	}

	@Override
	public void run() {
		try {
			byte buf[] = new byte[1024];//大小够存储一行就可以
			while(true){
				//接收数据
				DatagramPacket dp = new DatagramPacket(buf, buf.length);
				ds.receive(dp);
				//解析数据
				String ip = dp.getAddress().getHostAddress();
				String info= new String(dp.getData(),0,dp.getLength());
				System.out.println(ip+"说:"+info);
				if("over".equals(info)){
					System.out.println(ip+"离开聊天室....");
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
</span>



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值