⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendudpthread.java

📁 用swt作的一个类似飞鸽一样的局域网通信工具
💻 JAVA
字号:
/**
 * @author Tony DongWei Xia
 *
 */

package thread.sender;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
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;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import main.NetTransfer;
import extend.Configuration;

public class SendUDPThread extends Thread {

	public byte[] buf = new byte[100];

	public byte[] sendBuf = null;

	private DatagramSocket UDP = null;

	private DatagramPacket packet = null;

	private BufferedReader setupbr = null;
	
	private File setupfile;
	
	private String UTPcmd;
	
	public SendUDPThread() {
		
		super();
		
	}

	public SendUDPThread(String param) {
		super();
		if (param.equals("recreate")){
			reCreate();
		}else{
			UTPcmd = param;
			this.start();
		}
	}
	
	private void reCreate(){
		
		try {
			UDP = new DatagramSocket();
			buf = ("recreate"+"|").getBytes();
			packet = new DatagramPacket(buf, buf.length, InetAddress
					.getByName("127.0.0.1"), main.NetReceiver.UDPport);
			UDP.send(packet);
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	
	public void run() {
		String localAddress = null;
		String localHost = null;
		try {
			UDP = new DatagramSocket();
			localAddress = InetAddress.getLocalHost().getHostAddress();
			Configuration rc = new Configuration(NetTransfer.SetupFile);//相对路径
			localHost = rc.getValue("HostName");//以下读取properties文件的值
			if (localHost.equals("")) 
				localHost = new String(InetAddress.getLocalHost().getHostName().getBytes());
			buf = (UTPcmd+"|" + localAddress+ " " +localHost + "|").getBytes();
		} catch (UnknownHostException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
				
		try {
			packet = new DatagramPacket(buf, buf.length, InetAddress
						.getByName("255.255.255.255"), main.NetReceiver.UDPport);
			UDP.send(packet);
			
			setupfile = new File("./","CastSetup");
			setupbr = new BufferedReader(new InputStreamReader(new FileInputStream(setupfile)));
			
			while (true){
				String str = setupbr.readLine();
				if (str==null) break;
				else{
					str = (String)splitToken(str," ").get(0);
					packet = new DatagramPacket(buf, buf.length, InetAddress
							.getByName(str), main.NetReceiver.UDPport);
					UDP.send(packet);
				}
			}
		} catch (UnknownHostException e) {
							
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				setupbr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			UDP.close();
		}
	}

	public List splitToken(String fs,String acter) {
		StringTokenizer pt = new StringTokenizer(fs, acter);
		List l = new ArrayList();
		while (pt.hasMoreTokens()) {
			l.add(pt.nextToken());
		}
		return l;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -