📄 simpleclient.java
字号:
package boco.webtrade;import java.io.*;import java.net.*;/** * 这个类建立一个socket连接到服务器,产生(spawns) * 一个ReadThread对象读从服务器返回的数据. * * */public class SimpleClient extends Object implements Runnable, ReadCallback{ protected Socket serverSock; protected DataOutputStream outStream; protected Thread clientThread; protected ReadThread reader; private int timeout=15000; byte sendbytes[]=new byte[1000]; //初始化中文转换为ASCII码和ASCII码转换为中文的函数 protected ChineseTransfer atoc = new ChineseTransfer(); public SimpleClient(String hostName, int portNumber) throws IOException { try{ Socket serverSock = new Socket(hostName, portNumber); /*try{ serverSock.setSoTimeout(timeout); }catch(SocketException se){ se.printStackTrace(); }*/// The DataOutputStream has methods for sending different data types// in a machine-independent format. It is very useful for sending data// over a socket connection. outStream = new DataOutputStream(serverSock.getOutputStream());// Create a reader thread reader = new ReadThread(this, serverSock); //置读线程为超级线程 //reader.setDaemon(true);// Start the reader thread reader.start(); }catch(IOException ioe){ //System.out.println(ioe.getMessage()); //return; } }// These are generic start and stop methods for a Runnable public void start() { clientThread = new Thread(this); //置用户线程为超级线程 //clientThread.setDaemon(true); clientThread.start(); } public void stop() { clientThread.interrupt(); clientThread = null; }// sendString sends a string to the server using write public synchronized void sendString(String str) throws IOException { //System.out.println("Sending string: "+str); String str2=atoc.ChineseStringToAscii(str); int len=str2.length(); String str1=""; for(int i=0;i<(154-len);i++) str1+="0"; str+=str1; sendbytes=str.getBytes(); //outStream.writeUTF(str); try{ outStream.write(sendbytes,0,len); }catch(NullPointerException npe){ //System.out.println(npe.getMessage()); } }// The run method for this object just sends a bytes array to the server public void run() { try { //sendString("1234567890abcdefghijk爱lmnopqrstuvwxyz"); //Thread.sleep(2000); //disconnect(); //stop(); } catch (Exception oops) {// If there was an error, print info and disconnect oops.printStackTrace(); disconnect(); stop(); } }// The disconnect method closes down the connection to the server public void disconnect() { try { reader.closeConnection(); } catch (Exception badClose) { // should be able to ignore } }// dataReady is the callback from the read thread. It is called// whenever a string is received from the server. public synchronized void dataReady(String str) { System.out.println("Got incoming string: "+atoc.ChineseStringToAscii(str)); } public static void main(String[] args) { //while(true){ try {/* Change localhost to the host you are running the server on. If it is on the same machine, you can leave it as localhost. */ //SimpleClient client = new SimpleClient("196.168.1.132",6666); //client.start(); //Thread.sleep(2000); //client.disconnect(); } catch (Exception cantStart) { System.out.println("Got error"); cantStart.printStackTrace(); } } //}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -