package server;
/**类ReadRhread */
import java.io.*;
import java.net.*;
/**
* 服务器端的ReadRhread
* @see clientRequest
*/
public class ReadThread extends Thread{
/**用户连接的通信套接字*/
private Socket clientRequest;
/**输入流*/
private BufferedReader input;
/**读进来的数*/
private String m_sReadIn="";
/**服务器*/
private MyServer m_myServer;
/**线程结束的标志*/
private boolean done=false;
/**
* 服务器端的ReadRhread的构造器
* @param s 在9090端口上监听读的套接字
* @param ms 服务器MyServer类
* @see
*/
public ReadThread(Socket s,MyServer ms)
{
/**接收客户端传来的套接字*/
this.clientRequest=s;
/**服务器*/
this.m_myServer=ms;
/**字符流*/
InputStreamReader reader;
try{
/**初始化输入、输出流*/
reader=new InputStreamReader(clientRequest.getInputStream());
input=new BufferedReader(reader);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
/**
* 线程的执行方法读取客户机指令
* @see input.readLine()
* @see m_myServer.readStr(m_sReadIn)
*/
public void run()
{
while(!done && m_myServer!=null)
{
try{
/**接收客户机指令*/
m_sReadIn=input.readLine();
m_myServer.readStr(m_sReadIn);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
try
{
input.close() ;
clientRequest.close();
}
catch(IOException e){ }
}
/**
* 结束线程
*/
public void close()
{
done = false;
}
}
/**类ReadRhread */
import java.io.*;
import java.net.*;
/**
* 服务器端的ReadRhread
* @see clientRequest
*/
public class ReadThread extends Thread{
/**用户连接的通信套接字*/
private Socket clientRequest;
/**输入流*/
private BufferedReader input;
/**读进来的数*/
private String m_sReadIn="";
/**服务器*/
private MyServer m_myServer;
/**线程结束的标志*/
private boolean done=false;
/**
* 服务器端的ReadRhread的构造器
* @param s 在9090端口上监听读的套接字
* @param ms 服务器MyServer类
* @see
*/
public ReadThread(Socket s,MyServer ms)
{
/**接收客户端传来的套接字*/
this.clientRequest=s;
/**服务器*/
this.m_myServer=ms;
/**字符流*/
InputStreamReader reader;
try{
/**初始化输入、输出流*/
reader=new InputStreamReader(clientRequest.getInputStream());
input=new BufferedReader(reader);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
/**
* 线程的执行方法读取客户机指令
* @see input.readLine()
* @see m_myServer.readStr(m_sReadIn)
*/
public void run()
{
while(!done && m_myServer!=null)
{
try{
/**接收客户机指令*/
m_sReadIn=input.readLine();
m_myServer.readStr(m_sReadIn);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
try
{
input.close() ;
clientRequest.close();
}
catch(IOException e){ }
}
/**
* 结束线程
*/
public void close()
{
done = false;
}
}
package server;
import java.io.*;
import java.util.*;
import java.net.*;
import net.*;
/**
* 类MyServer扩充Thread
* @see <a href="../net/Net.html">Net</a>
* @see <a href="ReadThread.html">ReadThread</a>
*/
public class MyServer extends Thread{
/**接收数据的端口*/
private final int RECEIVE_IN_PORT=9090;
/**发送数据的端口*/
private final int RECEIVE_OUT_PORT=9091;
/**向总控台写数据的流*/
private PrintWriter m_printWriter;
/**服务器端监听接收数据的套接字*/
private ServerSocket m_rInServer;
/**服务器端监听发送数据的套接字*/
private ServerSocket m_rOutServer;
/**依赖的网络接口*/
private Net m_Owner;
/**读数据流的线程*/
private ReadThread readThread=null;
/**连接客户机成功失败有标志*/
private boolean bConnDone=false;
/**创建成功标志*/
private boolean bServerDone=true;
/**
* MyServer的构造器
* @param netRead 网络接口NetRead
*/
public MyServer(Net netRead)
{
m_Owner=netRead;
try {
m_rInServer = new ServerSocket(RECEIVE_IN_PORT);
m_rOutServer = new ServerSocket(RECEIVE_OUT_PORT);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
/**
* 重写Thread的run()方法,MyServer线程的入口点
* @see readThread#start()
*/
public void run()
{
// try{
while(!bConnDone){
try{
Socket socketIn = m_rInServer.accept();
Socket socketOut = m_rOutServer.accept();
readThread = new ReadThread(socketIn, this);
readThread.start();
/**文本流——>字符流*/
OutputStreamWriter writer = new OutputStreamWriter(socketOut.getOutputStream());
m_printWriter = new PrintWriter(writer, true);
m_Owner.showMessage("对手:"+socketIn.getInetAddress()+"加入游戏");
bConnDone=true;
}catch(Exception e)
{
bConnDone=true;
bServerDone=false;
m_Owner.setNetStatus(Net.NOCONNECT);
m_Owner.showMessage("创建服务器失败!");
}
}
//}catch(IOException e){
//System.out.println(e.getMessage());
//}
}
/**
* 当数据来的时候调用,接口上的读数据,
* @param str 读进来的数据
*/
protected void readStr(String str)
{
m_Owner.readStr(str);
}
/**
* 向对方发送数据
* @param str 要发送东西
* @return 若写成功,则返回真
*/
public boolean writeStr(String str)
{
if(m_printWriter==null)return false;
try
{
m_printWriter.println(str);
return true;
}
catch(Exception e)
{
m_Owner.showMessage(str+"发送失败!");
return false;
}
}
/**
* 关闭套接字和输入流,终止主机对象MyServer线程
*/
public void close()
{
try{
readThread.stop();
readThread=null;
m_printWriter.close();
m_rOutServer.close();
m_rInServer.close();
}catch(Exception e){}
}
/**
* 返回有没有机器连接成功
* @return 已经有机器连接上服务器 返回true
*/
public boolean getbConnDone()
{
return bConnDone;
}
/**
* 设置有没有机器连接
* @param bool true就时有连接
*/
public void setConnDone(boolean bool)
{
bConnDone=bool;
}
/**
* 返回创建服务器成功标志
* @return
*/
public boolean getServerDone()
{
return this.bServerDone;
}
}
import java.io.*;
import java.util.*;
import java.net.*;
import net.*;
/**
* 类MyServer扩充Thread
* @see <a href="../net/Net.html">Net</a>
* @see <a href="ReadThread.html">ReadThread</a>
*/
public class MyServer extends Thread{
/**接收数据的端口*/
private final int RECEIVE_IN_PORT=9090;
/**发送数据的端口*/
private final int RECEIVE_OUT_PORT=9091;
/**向总控台写数据的流*/
private PrintWriter m_printWriter;
/**服务器端监听接收数据的套接字*/
private ServerSocket m_rInServer;
/**服务器端监听发送数据的套接字*/
private ServerSocket m_rOutServer;
/**依赖的网络接口*/
private Net m_Owner;
/**读数据流的线程*/
private ReadThread readThread=null;
/**连接客户机成功失败有标志*/
private boolean bConnDone=false;
/**创建成功标志*/
private boolean bServerDone=true;
/**
* MyServer的构造器
* @param netRead 网络接口NetRead
*/
public MyServer(Net netRead)
{
m_Owner=netRead;
try {
m_rInServer = new ServerSocket(RECEIVE_IN_PORT);
m_rOutServer = new ServerSocket(RECEIVE_OUT_PORT);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
/**
* 重写Thread的run()方法,MyServer线程的入口点
* @see readThread#start()
*/
public void run()
{
// try{
while(!bConnDone){
try{
Socket socketIn = m_rInServer.accept();
Socket socketOut = m_rOutServer.accept();
readThread = new ReadThread(socketIn, this);
readThread.start();
/**文本流——>字符流*/
OutputStreamWriter writer = new OutputStreamWriter(socketOut.getOutputStream());
m_printWriter = new PrintWriter(writer, true);
m_Owner.showMessage("对手:"+socketIn.getInetAddress()+"加入游戏");
bConnDone=true;
}catch(Exception e)
{
bConnDone=true;
bServerDone=false;
m_Owner.setNetStatus(Net.NOCONNECT);
m_Owner.showMessage("创建服务器失败!");
}
}
//}catch(IOException e){
//System.out.println(e.getMessage());
//}
}
/**
* 当数据来的时候调用,接口上的读数据,
* @param str 读进来的数据
*/
protected void readStr(String str)
{
m_Owner.readStr(str);
}
/**
* 向对方发送数据
* @param str 要发送东西
* @return 若写成功,则返回真
*/
public boolean writeStr(String str)
{
if(m_printWriter==null)return false;
try
{
m_printWriter.println(str);
return true;
}
catch(Exception e)
{
m_Owner.showMessage(str+"发送失败!");
return false;
}
}
/**
* 关闭套接字和输入流,终止主机对象MyServer线程
*/
public void close()
{
try{
readThread.stop();
readThread=null;
m_printWriter.close();
m_rOutServer.close();
m_rInServer.close();
}catch(Exception e){}
}
/**
* 返回有没有机器连接成功
* @return 已经有机器连接上服务器 返回true
*/
public boolean getbConnDone()
{
return bConnDone;
}
/**
* 设置有没有机器连接
* @param bool true就时有连接
*/
public void setConnDone(boolean bool)
{
bConnDone=bool;
}
/**
* 返回创建服务器成功标志
* @return
*/
public boolean getServerDone()
{
return this.bServerDone;
}
}