?? sample.java
字號:
這是一個TCP服務器端的實現代碼,監聽客戶端的請求,在子線程中處理各個客戶端發來的數據包,再將處理后的結果送回客戶端。這里提供的代碼很完整,包括一個mainclass,一個監聽class和它的interface,一個包處理class,一個發送class,基本上可以直接使用。其中用到的一些工具類,例如Debug、GenProfile、Terminator等,它們代碼也會出現在隨后的系列文章中。
package org.kyle.net.svr.sample;
import java.io.*;
import java.util.*;
import java.net.*;
public class Sample
{
// GenProfile是一個配置文件工具類,從配置文件中取得運行參數
protected GenProfile m_env = null;
protected IListener m_Listener = null;
public Sample()
{
String cfgFile = System.getProperty("MainConfigFile","server.cfg");
m_env = new GenProfile( cfgFile );
}
public boolean startService()
{
try
{
// IntegrationFactory是一個工廠類,創建一個Listener實例
m_Listener=IntegrationFactory.createListener(m_env);
m_Listener.setProfile(m_env);
m_Listener.startListener();
Debug.info("Server started.");
return true;
}
catch( Exception e)
{
Debug.warning(e);
}
return false;
}
public boolean stopService()
{
try
{
m_ Listener.stopListener();
Debug.info("Server service stopped.");
return true;
}
catch( Exception e )
{
Debug.warning(e);
}
return false;
}
public static void main( String [] argv )
{
try
{
Sample main = new Sample();
main.startService();
// Terminator用來接收鍵盤操作,按下特定鍵后使程序退出。
Terminator terminator = null;
terminator = new Terminator(System.in, main);
terminator.start();
synchronized (main) {
main.wait(); //將主進程懸掛,直到在Terminator里激活。
main.stopService();
}
System.exit(0);
}
catch( Exception e )
{
Debug.warning(e);
}
}
}
package org.kyle.net.svr.sample;
import java.net.*;
public interface IListener
{
public void setProfile( GenProfile env );
public void listenOn(int port);
public void setTimeout( int timeout );
public void startListener();
public void stopListener();
public RawPkt accept();
public void close();
}
package org.kyle.net.svr.sample;
import java.net.*;
import java.io.*;
import java.util.*;
import java.math.*;
public class SampleListenerImpl extends Thread implements IListener
{
private boolean m_isRunning = false;
private boolean m_innerCall = false;
private int m_listenAt = -1;
private int m_timeout = -1;
private Socket m_skt = null;
private ServerSocket m_svrSkt = null;
public SampleListenerImpl ()
{
setName("SampleListener.");
}
public SampleListenerImpl ( GenProfile env )
throws SocketException, SecurityException, IOException
{
setName("SampleListener.");
if ( env == null )
throw new SecurityException("No Environment provided!");
m_env = env;
invokeSocket();
}
public void setProfile( GenProfile env )
{
m_env = env;
}
public void run()
{
try
{
invokeSocket();
Debug.info("Listening at " + m_svrSkt.getLocalPort() + "...");
while( m_isRunning )
{
try
{
m_innerCall = true;
accept();
m_innerCall = false;
}
catch( Exception e)
{
Debug.info(e);
}
}
}
catch(Exception e)
{
Debug.info(e);
}
}
public void startListener()
{
if ( !m_isRunning )
{
m_isRunning = true;
start();
}
}
public void stopListener()
{
if ( m_isRunning )
{
m_isRunning = false;
interrupt();
close();
}
}
public RawPkt accept()
{
if ( m_isRunning )
{
if ( m_innerCall )
{ }
else
{
Debug.finest("StandAlone Listener was started, external call of accept failed.");
return null;
}
}
try
{
m_skt = m_svrSkt.accept();
m_skt.setSoTimeout( m_env.getTimeout() * 1000 );
Debug.fine("ServerSocket accepted. ");
new FreeListener( m_skt );
return null;
}
catch( InterruptedIOException iioe)
{
Debug.info("Listener Timed Out: " + iioe.getMessage() + "\n");
}
catch(IOException ioe)
{
Debug.info(ioe);
}
catch(Exception e)
{
Debug.info(e);
}
return null;
}
public void listenOn(int port)
{
if ( port < 0 || port > 65535 ) port = 0;
m_listenAt = port;
}
public void close()
{
if ( m_skt != null )
{
if ( !m_isRunning )
{
try{
m_skt.close();
m_skt = null;
}
catch( IOException ioe)
{
Debug.warning(ioe);
}
}
}
}
public void setTimeout( int timeout )
{
if ( timeout < 0 ) timeout = 300;
m_timeout = timeout;
}
/////////////Private methods section.///////////////////////////////
private void invokeSocket()
throws SocketException, SecurityException, IOException
{
if ( m_skt == null )
{
m_svrSkt = new ServerSocket( m_listenAt != -1 ? m_listenAt : m_env.getListenAt() );
m_svrSkt.setSoTimeout( m_timeout != -1 ? m_timeout*1000: m_env.getTimeout()*1000 );
}
}
class FreeListener extends Thread
{
Socket m_skt;
public FreeListener( Socket skt )
{
m_skt = skt;
start();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -