//-------------------------------------------------------
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);
}


}

解决方案 »

  1.   

    楼上的过奖了,以前一直搞c++,公司让用java做一个金融通信系统,只是初学java,遇到了不少问题,希望大家能多帮忙,
    我打算把它全部开源,小弟来个抛砖引玉,希望有更好的东东和大家共响
      

  2.   

    简单看了一下,想看看你的结构设计来着,呵呵。现在的程序情况好像简单了点。不过可能你要做的就是简单的通信这样就够用了吧。提两个建议第一,public static boolean executeCmd(String sCmd)
    这里用if... else if应该会好一些第二,clientsocket用线程组管理应该比用数组好一些。
      

  3.   

    To  bluesmile979(笑着):
    你说的很对,的确很简单,我也得具体加些东西啊,比如如何设置网络超时设置,那个clientsocket还要再向一个服务器转发数据报,然后等待服务器答复再发回给客户端
    你有没有一些资料比如 设置数据报超时,关闭socket连接前的超做 等
      

  4.   

    socket和ServerSocket都有一个setSoTimeout的方法
    可以考虑使用一下线程池 
    JDK1.5的util已经有PoolExecutor那个东西了
    要是你想着为了并发好一点 可能可以使用java.nio这个异步的包里面的selector和channel都好像还好 集合线程池的话
      

  5.   

    to zealVampire(蚊子)  :
     thank you !
    你说的那个PoolExecutor,selector和channel,有没有实例看??
      

  6.   

    讲讲你的构架啊!!
    我自己学JAVA也半年多了,也写了个网络通讯的东东是我第一次写完一个完整的JAVA应用,本想拿出来让大家帮我指点指点的,可自我感觉太差了,特别是构架.我都不好意思拿出来,能具体说说你的想法吗也让我参考参考!!