//-------------------------------------------------------
package Server;
/**
* <p>Title:<p/>
* <p>Description:
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:</p>
* @author
* @version 1.0
*/
import java.io.*;
import java.net.*;public class ClientSocket extends Thread
{
public static final int buff_count = 100;
private Socket cliSocket;
private ServerListen srvListen;
private int Index;
private boolean bStop =false ;
// private boolean bRead = false; //**是否处于等待读数据状态
DataInputStream in;
DataOutputStream out;
public ClientSocket(ServerListen srvListen,Socket cliSocket,int Index) throws Exception
{
this.cliSocket = cliSocket;
this.srvListen = srvListen;
this.Index = Index;
synchronized(srvListen)
{
srvListen.userCount++;
}
in = new DataInputStream(this.cliSocket.getInputStream());
out = new DataOutputStream(this.cliSocket.getOutputStream());
super.start();
}
public void run()
{
byte[] inByte = new byte[buff_count];
int count = 0;
try
{
while(!bStop)
{
count = in.read(inByte);
String response = new String(inByte,0,inByte.length);
System.out.println("Receive "+count+" bytes from Client"+Index+"\n"+
response);
/**
*1.接收客户端发送来的数据包,分解数据包;
*2.对该数据包进行加密...
*3.将处理后的包转发给服务器端。
*4.等待服务器端反馈数据,进行 密钥管理
*5.PIN转换 MAC转换
*/
} }
catch(IOException e)
{
System.out.println("Read error:"+e);
}
finally
{
closeSocket();
}
closeIOStream();
synchronized(srvListen) //**当一个客户端线程结束时,
{ //**清理服务器线程
srvListen.userCount--;
if(srvListen.isWait()) //如果服务器线程处于等待状态,则唤醒其
{
srvListen.notifyConnect();
}
srvListen.nullSpecify(Index); //**此线程退出,句柄清空,能否正常被释放?
}
}
public void exitThread()
{
bStop= true;
closeSocket();
}
public void closeIOStream()
{
try
{
in.close();
out.close();
}
catch(IOException e)
{
System.out.println("Close IOStream error:"+e);
}
}
public synchronized void closeSocket()
{
try
{
if((cliSocket != null)&&(!cliSocket.isClosed()))
{
cliSocket.close();
}
}
catch(Exception e)
{
System.out.println("Close error:"+e);
}
}
}//---------------------------------------------------
package Server;
/**
* <p>Title: 输出\读写INI文件 <p/>
* <p>Description:
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:</p>
* @author
* @version 1.0
*/
import java.io.*;public class App
{
public static boolean bQuit = false;
public static void printCmdMsg()
{
String sCmd = new String("");
sCmd = "[input the command list]: \r\n" +
"quit --- exit\r\n" +
"cls --- clear screen\r\n" +
"ver --- get current version\r\n" +
"client --- get client information\r\n" +
"server --- get server status" ;
System.out.println(sCmd);
}
}
package Server;
/**
* <p>Title:<p/>
* <p>Description:
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:</p>
* @author
* @version 1.0
*/
import java.io.*;
import java.net.*;public class ClientSocket extends Thread
{
public static final int buff_count = 100;
private Socket cliSocket;
private ServerListen srvListen;
private int Index;
private boolean bStop =false ;
// private boolean bRead = false; //**是否处于等待读数据状态
DataInputStream in;
DataOutputStream out;
public ClientSocket(ServerListen srvListen,Socket cliSocket,int Index) throws Exception
{
this.cliSocket = cliSocket;
this.srvListen = srvListen;
this.Index = Index;
synchronized(srvListen)
{
srvListen.userCount++;
}
in = new DataInputStream(this.cliSocket.getInputStream());
out = new DataOutputStream(this.cliSocket.getOutputStream());
super.start();
}
public void run()
{
byte[] inByte = new byte[buff_count];
int count = 0;
try
{
while(!bStop)
{
count = in.read(inByte);
String response = new String(inByte,0,inByte.length);
System.out.println("Receive "+count+" bytes from Client"+Index+"\n"+
response);
/**
*1.接收客户端发送来的数据包,分解数据包;
*2.对该数据包进行加密...
*3.将处理后的包转发给服务器端。
*4.等待服务器端反馈数据,进行 密钥管理
*5.PIN转换 MAC转换
*/
} }
catch(IOException e)
{
System.out.println("Read error:"+e);
}
finally
{
closeSocket();
}
closeIOStream();
synchronized(srvListen) //**当一个客户端线程结束时,
{ //**清理服务器线程
srvListen.userCount--;
if(srvListen.isWait()) //如果服务器线程处于等待状态,则唤醒其
{
srvListen.notifyConnect();
}
srvListen.nullSpecify(Index); //**此线程退出,句柄清空,能否正常被释放?
}
}
public void exitThread()
{
bStop= true;
closeSocket();
}
public void closeIOStream()
{
try
{
in.close();
out.close();
}
catch(IOException e)
{
System.out.println("Close IOStream error:"+e);
}
}
public synchronized void closeSocket()
{
try
{
if((cliSocket != null)&&(!cliSocket.isClosed()))
{
cliSocket.close();
}
}
catch(Exception e)
{
System.out.println("Close error:"+e);
}
}
}//---------------------------------------------------
package Server;
/**
* <p>Title: 输出\读写INI文件 <p/>
* <p>Description:
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:</p>
* @author
* @version 1.0
*/
import java.io.*;public class App
{
public static boolean bQuit = false;
public static void printCmdMsg()
{
String sCmd = new String("");
sCmd = "[input the command list]: \r\n" +
"quit --- exit\r\n" +
"cls --- clear screen\r\n" +
"ver --- get current version\r\n" +
"client --- get client information\r\n" +
"server --- get server status" ;
System.out.println(sCmd);
}
}
我打算把它全部开源,小弟来个抛砖引玉,希望有更好的东东和大家共响
这里用if... else if应该会好一些第二,clientsocket用线程组管理应该比用数组好一些。
你说的很对,的确很简单,我也得具体加些东西啊,比如如何设置网络超时设置,那个clientsocket还要再向一个服务器转发数据报,然后等待服务器答复再发回给客户端
你有没有一些资料比如 设置数据报超时,关闭socket连接前的超做 等
可以考虑使用一下线程池
JDK1.5的util已经有PoolExecutor那个东西了
要是你想着为了并发好一点 可能可以使用java.nio这个异步的包里面的selector和channel都好像还好 集合线程池的话
thank you !
你说的那个PoolExecutor,selector和channel,有没有实例看??
我自己学JAVA也半年多了,也写了个网络通讯的东东是我第一次写完一个完整的JAVA应用,本想拿出来让大家帮我指点指点的,可自我感觉太差了,特别是构架.我都不好意思拿出来,能具体说说你的想法吗也让我参考参考!!