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

📄 tcpcommunicationthread.java

📁 一个远程控制的java测试程序源代码.转于网上
💻 JAVA
字号:
/*
 * @(#)TcpCommunicationThread.java 1.00 2005-9-1
 *
 * Copyright 2005 BeanSoft Studio. All rights reserved.
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package studio.beansoft.remotecontrol.client;

import java.awt.Toolkit;
import java.io.*;

import javax.swing.JOptionPane;

import studio.beansoft.net.HttpPostBean;
import studio.beansoft.remotecontrol.ConfigurationManager;
import studio.beansoft.remotecontrol.KeyEventCodec;
import studio.beansoft.remotecontrol.MouseEventCodec;
import sun.net.NetworkClient;

/**
 * TcpCommunicationThread is a thread used to do communication jobs with a 
 * TCP Socket host.
 * Protocol: client send all data in one line, when server
 * received a line(might be empty), then response as follows:
 * length [a number of image data length]\n
 * [binaries of image data]
 * 
 * eg:
 * Client:mouse_events=....&key_events=....\n
 * Server:length 200\n
 * Server:0x010x02....

 * Chinese documents:
 * 用来和 TCP Socket 主机进行通信工作的线程.
 * 协议: 客户端在单行中发送所有数据, 当服务器收到一行数据(可以为空),
 * 就回复下列信息:
 * length [图片二进制数据的长度数]\n
 * [图片二进制数据]
 * 例如:
 * Client:mouse_events=....&key_events=....\n
 * Server:length 200\n
 * Server:0x010x02.... * 
 * 
 * @see studio.beansoft.remotecontrol.server.TcpMonitorServer
 * @author BeanSoft
 * @version 1.00 2005-9-1
 */
public class TcpCommunicationThread extends AbstractCommunicationThread {

	/**
	 * @see AbstractCommunicationThread#run()
	 */
	public void run() {
		NetworkClient client = new NetworkClient();
		DataInputStream in = null;
		StringBuffer buffer = new StringBuffer();
		
		try {
			String tcpHostURL = ConfigurationManager.getConfiguration().getProperty("remote_url_tcp");
			int colonIndex = tcpHostURL.indexOf(':');
			String host = null;
			int port = 6666;
			
			if(colonIndex > 0) {
				host = tcpHostURL.substring(0, colonIndex);
				port = Integer.parseInt(tcpHostURL.substring(colonIndex + 1));
			} else {
				host = tcpHostURL;
			}
			
			client.openServer(host, port);
			
			in = new DataInputStream(client.serverInput);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Unable connect to server:" + e.getLocalizedMessage());
			return;
		}
		
		while (true) {
			long startTime = System.currentTimeMillis();
			// Clear command buffer
			buffer.delete(0, buffer.length());
			
			if(paused) {
				if(pane != null) {
					// Clear events that has been send 清除已发送的事件
					pane.getMouseEventQueue().clear();
					pane.getKeyEventQueue().clear();
				}
				
				sleepTime(1000);
				continue;
			} else if(pane == null) {
				sleepTime(1000);				
				continue;
			}
			
			buffer.append("mouse_events=");
			buffer.append(MouseEventCodec.mouseEventToString(pane.getMouseEventQueue()));
			buffer.append("&");
			buffer.append("key_events=");
			buffer.append(KeyEventCodec.keyEventToString(pane.getKeyEventQueue()));
			
			client.serverOutput.println(buffer.toString());
			// Clear events that has been send 清除已发送的事件
			pane.getMouseEventQueue().clear();
			pane.getKeyEventQueue().clear();
			
			try {
				String line = in.readLine();
				if(line == null) {
					line = "";
				}
				
				if(line.startsWith("length")) {
					int length = Integer.parseInt(line.substring("length".length()));
					
					byte[] data = new byte[length];
					
					for(int i = 0; i < data.length; i++) {
						data[i] = in.readByte();
					}
					
//					in.read(data);
					
					pane.setImage(Toolkit.getDefaultToolkit().createImage(
							data));
					System.out.println("image size: " + data.length + " byte(s)");
				}				
			} catch (Exception e) {

						setPaused(true);
						JOptionPane.showMessageDialog(getPane(), "Thread paused, because errors occurs:" +  e);
						getPane().repaint();

				e.printStackTrace();
			}

//			Make sure sleep at least 100ms
			startTime += 100;
			sleepTime(Math.max(0, startTime - System.currentTimeMillis()));
			
			System.out.println("执行时间:" + (System.currentTimeMillis() - startTime));
		}
	}


}

⌨️ 快捷键说明

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