本帖最后由 doyxy 于 2014-4-5 08:27 编辑
在UDP聊天的课程里,毕老师把DatagramSocket定义在了外面,并且用构造函数传入,而我直接在里面设置运行起来也是一样可以,请问为什么要设置在外面呢?谢谢
- package NetTest;
- 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;
- //创建一个UDP发送和接收程序.
- // step2 将两个控制台合并,在一个控制台上显示收发内容.用多线程实现.
- public class UDPDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new Thread(new UDPSend()).start();
- new Thread(new UDPRece()).start();
- }
- }
- /* 毕老师的写法:
- //发送端
- class UDPSend implements Runnable{
- private DatagramSocket ds;
-
- public UDPSend(DatagramSocket ds){
- this.ds = ds;
- }
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- DatagramPacket dp;
- while (true){
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while ((line=bufr.readLine())!=null){
- if ("over".equals(line))
- return;
- dp = new DatagramPacket(line.getBytes(), line.getBytes().length,InetAddress.getByName("192.168.1.101"),5555);
- ds.send(dp);
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
- class UDPRece implements Runnable {
- private DatagramSocket ds = null;
-
- public UDPRece(DatagramSocket ds){
- this.ds = ds;
- }
- public void run() {
- while (true) {// 一直准备接收数据
- try {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf, buf.length);// 接收数据后的存储位置
- ds.receive(dp);// 接收数据
- String ip = dp.getAddress().getHostAddress();// 获取数据中的ip
- String data = new String(buf, 0, dp.getLength());// 获取数据内容
- System.out.println("ip:(" + ip + "):" + data);
- // ds.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- */
- // 我的写法:
- // 发送端,1,建立UDPsocket,2,封装数据,3,发送,4,关闭
- class UDPSend implements Runnable {
- public void run() {
- DatagramSocket ds = null;
- try {
- ds = new DatagramSocket();
- } catch (SocketException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- while (true) {// 一直准备发送数据,通过控制台输入
- try {
- // 录入数据
- BufferedReader bufr = new BufferedReader(new InputStreamReader(
- System.in));
- String line = null;
- while ((line = bufr.readLine()) != null) {
- if ("over".equals(line))
- return;
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket(buf, buf.length,
- InetAddress.getByName("192.168.1.101"), 5561);// 将数据写入包中,
- //ip192.168.1.255为广播,发送往该网段所有的主机
- ds.send(dp);
- }
- // ds.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- class UDPRece implements Runnable {
- public void run() {
- DatagramSocket ds = null;
- try {
- ds = new DatagramSocket(5561);// 需要在while(true)外面设置,否则发生端口绑定异常
- } catch (SocketException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- while (true) {// 一直准备接收数据
- try {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf, buf.length);// 接收数据后的存储位置
- ds.receive(dp);// 接收数据
- String ip = dp.getAddress().getHostAddress();// 获取数据中的ip
- String data = new String(buf, 0, dp.getLength());// 获取数据内容
- int port = dp.getPort();
- System.out.println("ip:(" + ip + "),data:(" + data + "),port:("
- + port + ").");
- // ds.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
复制代码
|
|