我给你一个我写的多线程接收的例子吧!
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import java.util.Date;public class AcceptFeeMsg extends Thread{
Queue acceptMsgQueue = null;
Queue sortMsgQueue = null;
ServerSocket serSocket = null;
Socket sSocket = null;
int port = 9110;
DealSocketThread dealSocketThread=null;
        LogToFile myLog = new LogToFile("LogFileName");
        LogToFile accLog = new LogToFile("收费消息");
public AcceptFeeMsg(Queue acceptFeeQueue,Queue waitSortQueue){ acceptMsgQueue = acceptFeeQueue;
sortMsgQueue = waitSortQueue;
myLog.DoLog("AcceptFeeMsg线程启动成功!");
}public void run(){
try{ serSocket = new ServerSocket(port); while (true)
{
   sSocket = serSocket.accept();//socket 的 accept
dealSocketThread= new DealSocketThread(sSocket,acceptMsgQueue,sortMsgQueue,myLog,accLog);//这里两行将accept到的SOCKET交给线程去处理
     dealSocketThread.start(); }//end of while }catch(Exception ee){
myLog.DoLog("AcceptFeeMsg线程异常 "+ee);  }//end of try and catch exception ee
finally 
{
       if (serSocket!= null){
     try {
     serSocket.close();
     }catch(Exception eaa){
     eaa.printStackTrace();
     }//end of try and catch exception eaa

       }
       }
System.out.println("ListenDaemon退出,系统。");
}//end of run
}//end of class ListenDaemon

class DealSocketThread extends Thread{
Socket clientRequest = null;
        ObjectInputStream dis = null;
Queue acceptFeeQueue = null;
Queue waitSortQueue = null;
acceptFee  acceptFeeMsg = new acceptFee();
acceptFee  acceptFeeSaveMsg = new acceptFee();
privateFee feeMsg = new privateFee();
int month;
LogToFile myLog = null;
LogToFile accLog = null;

public DealSocketThread(Socket s,Queue acceptMsgQueue,Queue sortMsgQueue,LogToFile inmyLog,LogToFile inaccLog){
clientRequest = s;
acceptFeeQueue = acceptMsgQueue;
waitSortQueue = sortMsgQueue;
myLog = inmyLog;
accLog = inaccLog;
try {
InputStream s1out=clientRequest.getInputStream();
dis=new ObjectInputStream (s1out);
System.out.println("启动一个DealSocketThread成功");
}catch (Exception eda){
System.out.println("启动一个DealSocketThread失败");
}
}//end of public DealSocketThread
public void run(){ try{ acceptFeeMsg = (acceptFee)dis.readObject();
acceptFeeSaveMsg = acceptFeeMsg;
                        acceptFeeQueue.enq(acceptFeeSaveMsg); 
if (acceptFeeMsg!=null)
{
                               //  接收到的消息处理 }//end of if (virtalMsg!=null) }catch (Exception edd){
                edd.printStackTrace();
myLog.DoLog("DealSocketThread线程异常 "+edd); 
}//end of try_catch
finally
{
try{

if(dis!=null){
   dis.close();
}
     if (clientRequest!= null)
     {
         clientRequest.close();
     }
     }
     catch(Exception e){
     }
         } }//end of run
}//end of class DealSocketThread

解决方案 »

  1.   

    以下是最简单的支持多用户的服务器程序,测试时可用telnet 127.0.0.1 10000import java.net.*;
    import java.io.*;public class CSServer{
    public CSServer(int port) throws IOException{
    ServerSocket serverSocket=new ServerSocket(port);
    System.out.println("Server started!");

    try{
    while(true){
    Socket socket=serverSocket.accept();
    try{
    new MyDealProcess(socket).start();
    }catch(Exception e){
    socket.close();
    }
    }
    }finally{
    serverSocket.close();
    }
    } public static void main(String[] args) throws IOException{
    CSServer server=new CSServer(10000);
    }
    }
    class MyDealProcess extends Thread{
    Socket socket;
    PrintWriter pw;
    BufferedReader br;
    public MyDealProcess(Socket socket) throws IOException{
    this.socket=socket;
    this.socket.setSoTimeout(1000*60);
    pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
    br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    public void run(){
    try{
    while(true){
    String sss=br.readLine();
    if(sss==null){
    break;
    }else{
    pw.println("input:");
    }
    }
    }catch(IOException e){
    }finally{
    try {
    pw.close();
    br.close();
    socket.close();
    } catch(IOException e) {}
    }
    }
    }