我的一个例子:(主要是一个连接开启一个线程,不过开启过得话也会内存不够的,呵呵)
package seu.yuch.server;/**
* The server to monitor all tasks which comes from all client-side in LAN.
*
* @version $version 1.0
* @author <a href="mailto:[email protected]" ><b>[email protected]</b></a>
*/import java.util.ArrayList;
import java.util.StringTokenizer;import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;import java.net.InetAddress;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;import seu.yuch.sql.PoolManager;
import seu.yuch.Constants;public class TaskMonitorServer extends Thread
implements Constants
{
private int port = DEFAULT_PORT; //initialize port
private int timeout = DEFAULT_TIMEOUT; //timeout private PoolManager pools = null; /**
* Constructor.
*
*/
public TaskMonitorServer() {
this( DEFAULT_PORT,DEFAULT_TIMEOUT );
} /**
* Constructor.
*
* @param port int.
* @param timeout long.
*/
public TaskMonitorServer( int port,int timeout ) {
this.port = port;
this.timeout = timeout;
} /**
* Sets the interval.
*
* @param interval long.
*/
public void setTimeOut( int timeout ) {
this.timeout = timeout;
} /**
* Sets the port.
*
* @param port int.
*/
public void setPort( int port ) {
this.port = port;
}
/**
* Set connection pool manager
*
* @param PoolManager
*/
public void setPoolManager( PoolManager pools ) {
this.pools = pools;
} /**
* Get connection pool manager.
*
* @return PoolManager.
*/
public PoolManager getPoolManager() {
return this.pools;
}
/**
* Close server and dispose all resources
*
*/
public void release() {
if( pools != null ) {
pools.release();
}
} /**
* Starts to run server.
*
*/
public void run() {
try
{
ServerSocket server = new ServerSocket( port );
server.setSoTimeout( timeout );
while( true ) {
Socket listen = null;
try
{
listen = server.accept();
ProcessConnectionThread cc =
new ProcessConnectionThread( listen );
}
catch ( IOException e )
{
System.out.println( "无法监听到来的客户连接!" );
}
}
}
catch ( SocketTimeoutException e )
{
System.out.println( "服务器Socket超时!" );
if( pools != null ) {//close all connection in pool
pools.release();
}
return;
}
catch ( IOException e ) {
System.out.println( "Starting server thread error!!" );
if( pools != null ) {//close all connection in pool
pools.release();
}
return;
}
} /**
* Thread to process the activity information coming from client sides.
*
*/
class ProcessConnectionThread extends Thread {
private Socket client = null;
private SafeBufferedReader is = null;
public ProcessConnectionThread( Socket s ) { // constructor
client = s;
try {
is = new SafeBufferedReader( new BufferedReader(
new InputStreamReader( client.getInputStream() ) ) );
} catch ( IOException e ) {
System.out.println( "读取客户端资料异常: " + e.getMessage() );
}
this.start(); // Thread starts here...this start() will call run()
}
public void run() {
Connection conn = null;
Statement st = null;
try {
conn = pools.getConnection( "sqlserver" );
st = conn.createStatement(); InetAddress inet = client.getInetAddress();
String ip = inet.toString().substring(1); //clinet side's IP String activity = is.readLine(); //a activity item
while( activity != null ) {
//write a item about client's activity information into database
StringTokenizer items = new StringTokenizer( activity,"***" );
ArrayList infos = new ArrayList();
while( items.hasMoreTokens() ) {
infos.add( items.nextToken() );
}
StringBuffer buffer = new StringBuffer( "insert into" );
if( ((String)infos.get(1)).equalsIgnoreCase("CPU") ) {
String usage = (String)infos.get(2);
buffer.append( " client_cpu values(\'" + ip + "\'"
+ ",\'" + (String)infos.get(0) + "\'"
+ ",\'" + ( Integer.valueOf(usage.trim()).intValue() ) + "\')" );
}
else if( ((String)infos.get(1)).equalsIgnoreCase("NET") ) {
buffer.append(
" client_net values(\'" + ip + "\'"
+ ",\'" + (String)infos.get(0) + "\'" );
String[] infoitems = ((String)infos.get(2)).split( "###" );
buffer.append( ",\'" + Integer.valueOf(infoitems[0].trim()).intValue() + "\'" );
buffer.append( ",\'" + infoitems[1] + "\'" );
buffer.append( ",\'" + infoitems[2] + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[3].trim()).intValue() + "\'" );
buffer.append( ",\'" + infoitems[4] + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[5].trim()).intValue() + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[6].trim()).intValue() + "\')" );
}
else {
buffer.append( " client_activity values(\'" + ip + "\'" );
buffer.append( ",\'" + (String)infos.get(0) + "\'" );
buffer.append( ",\'" + (String)infos.get(1) + "\'" );
buffer.append( ",\'" + (String)infos.get(2) + "\')" );
}
System.out.println( buffer.toString() );
st.execute( buffer.toString() );
//read next item
activity = is.readLine();
}
if( st != null ) {//closse statement
st.close();
}
if( conn != null ) {//return connection into pools
pools.freeConnection( "sqlserver",conn );
}
}
catch ( SQLException e ) {
System.out.println( "数据库操作异常: " + e.getMessage() );
}
catch ( IOException e ) {
System.out.println( client + " 断开连接!" + e.getMessage() );
}
}
}
/**
* Test this application server.
*
*/
public static void main( String args[] ) {
TaskMonitorServer server = new TaskMonitorServer();
server.setTimeOut( 0 );//infinite wait
PoolManager pools = PoolManager.getInstance();
server.setPoolManager( pools );
server.run();
}
}
package seu.yuch.server;/**
* The server to monitor all tasks which comes from all client-side in LAN.
*
* @version $version 1.0
* @author <a href="mailto:[email protected]" ><b>[email protected]</b></a>
*/import java.util.ArrayList;
import java.util.StringTokenizer;import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;import java.net.InetAddress;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;import seu.yuch.sql.PoolManager;
import seu.yuch.Constants;public class TaskMonitorServer extends Thread
implements Constants
{
private int port = DEFAULT_PORT; //initialize port
private int timeout = DEFAULT_TIMEOUT; //timeout private PoolManager pools = null; /**
* Constructor.
*
*/
public TaskMonitorServer() {
this( DEFAULT_PORT,DEFAULT_TIMEOUT );
} /**
* Constructor.
*
* @param port int.
* @param timeout long.
*/
public TaskMonitorServer( int port,int timeout ) {
this.port = port;
this.timeout = timeout;
} /**
* Sets the interval.
*
* @param interval long.
*/
public void setTimeOut( int timeout ) {
this.timeout = timeout;
} /**
* Sets the port.
*
* @param port int.
*/
public void setPort( int port ) {
this.port = port;
}
/**
* Set connection pool manager
*
* @param PoolManager
*/
public void setPoolManager( PoolManager pools ) {
this.pools = pools;
} /**
* Get connection pool manager.
*
* @return PoolManager.
*/
public PoolManager getPoolManager() {
return this.pools;
}
/**
* Close server and dispose all resources
*
*/
public void release() {
if( pools != null ) {
pools.release();
}
} /**
* Starts to run server.
*
*/
public void run() {
try
{
ServerSocket server = new ServerSocket( port );
server.setSoTimeout( timeout );
while( true ) {
Socket listen = null;
try
{
listen = server.accept();
ProcessConnectionThread cc =
new ProcessConnectionThread( listen );
}
catch ( IOException e )
{
System.out.println( "无法监听到来的客户连接!" );
}
}
}
catch ( SocketTimeoutException e )
{
System.out.println( "服务器Socket超时!" );
if( pools != null ) {//close all connection in pool
pools.release();
}
return;
}
catch ( IOException e ) {
System.out.println( "Starting server thread error!!" );
if( pools != null ) {//close all connection in pool
pools.release();
}
return;
}
} /**
* Thread to process the activity information coming from client sides.
*
*/
class ProcessConnectionThread extends Thread {
private Socket client = null;
private SafeBufferedReader is = null;
public ProcessConnectionThread( Socket s ) { // constructor
client = s;
try {
is = new SafeBufferedReader( new BufferedReader(
new InputStreamReader( client.getInputStream() ) ) );
} catch ( IOException e ) {
System.out.println( "读取客户端资料异常: " + e.getMessage() );
}
this.start(); // Thread starts here...this start() will call run()
}
public void run() {
Connection conn = null;
Statement st = null;
try {
conn = pools.getConnection( "sqlserver" );
st = conn.createStatement(); InetAddress inet = client.getInetAddress();
String ip = inet.toString().substring(1); //clinet side's IP String activity = is.readLine(); //a activity item
while( activity != null ) {
//write a item about client's activity information into database
StringTokenizer items = new StringTokenizer( activity,"***" );
ArrayList infos = new ArrayList();
while( items.hasMoreTokens() ) {
infos.add( items.nextToken() );
}
StringBuffer buffer = new StringBuffer( "insert into" );
if( ((String)infos.get(1)).equalsIgnoreCase("CPU") ) {
String usage = (String)infos.get(2);
buffer.append( " client_cpu values(\'" + ip + "\'"
+ ",\'" + (String)infos.get(0) + "\'"
+ ",\'" + ( Integer.valueOf(usage.trim()).intValue() ) + "\')" );
}
else if( ((String)infos.get(1)).equalsIgnoreCase("NET") ) {
buffer.append(
" client_net values(\'" + ip + "\'"
+ ",\'" + (String)infos.get(0) + "\'" );
String[] infoitems = ((String)infos.get(2)).split( "###" );
buffer.append( ",\'" + Integer.valueOf(infoitems[0].trim()).intValue() + "\'" );
buffer.append( ",\'" + infoitems[1] + "\'" );
buffer.append( ",\'" + infoitems[2] + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[3].trim()).intValue() + "\'" );
buffer.append( ",\'" + infoitems[4] + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[5].trim()).intValue() + "\'" );
buffer.append( ",\'" + Integer.valueOf(infoitems[6].trim()).intValue() + "\')" );
}
else {
buffer.append( " client_activity values(\'" + ip + "\'" );
buffer.append( ",\'" + (String)infos.get(0) + "\'" );
buffer.append( ",\'" + (String)infos.get(1) + "\'" );
buffer.append( ",\'" + (String)infos.get(2) + "\')" );
}
System.out.println( buffer.toString() );
st.execute( buffer.toString() );
//read next item
activity = is.readLine();
}
if( st != null ) {//closse statement
st.close();
}
if( conn != null ) {//return connection into pools
pools.freeConnection( "sqlserver",conn );
}
}
catch ( SQLException e ) {
System.out.println( "数据库操作异常: " + e.getMessage() );
}
catch ( IOException e ) {
System.out.println( client + " 断开连接!" + e.getMessage() );
}
}
}
/**
* Test this application server.
*
*/
public static void main( String args[] ) {
TaskMonitorServer server = new TaskMonitorServer();
server.setTimeOut( 0 );//infinite wait
PoolManager pools = PoolManager.getInstance();
server.setPoolManager( pools );
server.run();
}
}
或者用java.nio包
FormFile uploadFile = fileUploadForm.getUploadFile();
try
{
// 获得文件流
InputStream uf = uploadFile.getInputStream();
Socket s = new Socket("192.168.100.25", 5678);这里的uf是不是在socket建立后就能传送了,就象DataInputStream一样啊??