MyClient类和ReadThread类
package client;
import java.net.*;
import java.io.*;
import net.*;
/**
* 用于连接MyServer
* @see <a href="../net/Net.html">Net</a>
* @see <a ref="ReadThread.html">ReadThread</a>
*/
public class MyClient {
/**接收数据的端口*/
private final int RECEIVE_OUT_PORT=9090;
/**发送数据的端口*/
private final int RECEIVE_IN_PORT=9091;
/**向总控台写数据的流*/
private PrintWriter m_printWriter;
/**依赖的网络接口*/
private Net m_Owner;
/**读数据流的线程*/
private ReadThread readThread=null;
/**用于通信的套接字*/
private Socket socketIn,socketOut;
/**连接成功失败的标志*/
private boolean m_connectFlag=false;
/**
* MyClient的构造器
* @param str 要连接的MyServer的IP地址
* @param netRead 依赖的网络接口
*/
public MyClient(String str, Net netRead)
{
m_Owner = netRead;
try
{
socketOut = new Socket(str, RECEIVE_OUT_PORT);
socketIn = new Socket(str, RECEIVE_IN_PORT);
readThread = new ReadThread(socketIn, this); //实例readThread
readThread.start(); //启动readThread线程
OutputStreamWriter writer = new OutputStreamWriter(socketOut.
getOutputStream());
m_printWriter = new PrintWriter(writer, true);
m_connectFlag = true;
}
catch (Exception e)
{
m_Owner.showMessage("连接" + str + "失败");
m_connectFlag = false;
}
}
/**
* 当数据来的时候调用
* @param str 读进来的数据
*/
protected void readStr(String str)
{
System.out.println(str);
m_Owner.readStr(str);
}
/**
* 往对方写东西
* @param str 要写的东西
* @return 若写成功,则返回真
* @see m_printWriter
*/
public boolean writeStr(String str)
{
if (m_printWriter == null)
return false;
try
{
m_printWriter.println(str);
}
catch (Exception e)
{
m_Owner.showMessage(str + "发送失败!");
return false;
}
return true;
}
/**
* 关闭MyClient
* @see readThread#stop()
*/
public void close()
{
try
{
readThread.stop();
readThread = null;
m_printWriter.close();
socketOut.close();
socketIn.close();
}
catch (Exception e) {}
}
/**获取连接成功失败的标志*/
public boolean getConnectFlag()
{
return m_connectFlag;
}
}
import java.net.*;
import java.io.*;
import net.*;
/**
* 用于连接MyServer
* @see <a href="../net/Net.html">Net</a>
* @see <a ref="ReadThread.html">ReadThread</a>
*/
public class MyClient {
/**接收数据的端口*/
private final int RECEIVE_OUT_PORT=9090;
/**发送数据的端口*/
private final int RECEIVE_IN_PORT=9091;
/**向总控台写数据的流*/
private PrintWriter m_printWriter;
/**依赖的网络接口*/
private Net m_Owner;
/**读数据流的线程*/
private ReadThread readThread=null;
/**用于通信的套接字*/
private Socket socketIn,socketOut;
/**连接成功失败的标志*/
private boolean m_connectFlag=false;
/**
* MyClient的构造器
* @param str 要连接的MyServer的IP地址
* @param netRead 依赖的网络接口
*/
public MyClient(String str, Net netRead)
{
m_Owner = netRead;
try
{
socketOut = new Socket(str, RECEIVE_OUT_PORT);
socketIn = new Socket(str, RECEIVE_IN_PORT);
readThread = new ReadThread(socketIn, this); //实例readThread
readThread.start(); //启动readThread线程
OutputStreamWriter writer = new OutputStreamWriter(socketOut.
getOutputStream());
m_printWriter = new PrintWriter(writer, true);
m_connectFlag = true;
}
catch (Exception e)
{
m_Owner.showMessage("连接" + str + "失败");
m_connectFlag = false;
}
}
/**
* 当数据来的时候调用
* @param str 读进来的数据
*/
protected void readStr(String str)
{
System.out.println(str);
m_Owner.readStr(str);
}
/**
* 往对方写东西
* @param str 要写的东西
* @return 若写成功,则返回真
* @see m_printWriter
*/
public boolean writeStr(String str)
{
if (m_printWriter == null)
return false;
try
{
m_printWriter.println(str);
}
catch (Exception e)
{
m_Owner.showMessage(str + "发送失败!");
return false;
}
return true;
}
/**
* 关闭MyClient
* @see readThread#stop()
*/
public void close()
{
try
{
readThread.stop();
readThread = null;
m_printWriter.close();
socketOut.close();
socketIn.close();
}
catch (Exception e) {}
}
/**获取连接成功失败的标志*/
public boolean getConnectFlag()
{
return m_connectFlag;
}
}
package client;
import java.io.*;
import java.net.*;
/**
*客户机端从套接字读数据
*/
public class ReadThread extends Thread
{
/**用户连接的通信套接字*/
private Socket clientRequest;
/**输入流*/
private BufferedReader input;
/**读进来的数*/
private String m_sReadIn = "";
/**客户机*/
private MyClient m_myClient;
/**线程结束标志*/
private boolean done = false;
/**
* 客户机端的ReadThread的构造器
* @param s 套接字
* @param mc 客户机
*/
public ReadThread(Socket s, MyClient mc) {
/**接收receiveServer传来的套接字*/
this.clientRequest = s;
this.m_myClient = mc;
/**字符流*/
InputStreamReader reader;
try {
/**初始化输入、输出流*/
reader = new InputStreamReader(clientRequest.getInputStream());
input = new BufferedReader(reader);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 线程的执行方法
*/
public void run()
{
while (!done)
{
try
{
m_sReadIn = input.readLine();
m_myClient.readStr(m_sReadIn);
//System.out.println(m_sReadIn); //接收客户机指令
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
try
{
input.close() ;
clientRequest.close(); //关闭套接字
}
catch (IOException e) { }
} //end of run
/**
* 结束线程
*/
public void close()
{
done = false;
}
}
import java.io.*;
import java.net.*;
/**
*客户机端从套接字读数据
*/
public class ReadThread extends Thread
{
/**用户连接的通信套接字*/
private Socket clientRequest;
/**输入流*/
private BufferedReader input;
/**读进来的数*/
private String m_sReadIn = "";
/**客户机*/
private MyClient m_myClient;
/**线程结束标志*/
private boolean done = false;
/**
* 客户机端的ReadThread的构造器
* @param s 套接字
* @param mc 客户机
*/
public ReadThread(Socket s, MyClient mc) {
/**接收receiveServer传来的套接字*/
this.clientRequest = s;
this.m_myClient = mc;
/**字符流*/
InputStreamReader reader;
try {
/**初始化输入、输出流*/
reader = new InputStreamReader(clientRequest.getInputStream());
input = new BufferedReader(reader);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 线程的执行方法
*/
public void run()
{
while (!done)
{
try
{
m_sReadIn = input.readLine();
m_myClient.readStr(m_sReadIn);
//System.out.println(m_sReadIn); //接收客户机指令
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
try
{
input.close() ;
clientRequest.close(); //关闭套接字
}
catch (IOException e) { }
} //end of run
/**
* 结束线程
*/
public void close()
{
done = false;
}
}