处理某一个客户端通信的函数代码:
       ServerSocket conSs = new ServerSocket(conPort);
        Socket Sc = conSs.accept();
        conSs.close();
        try {
          String inputStr = null;
          DataInputStream conDis = new DataInputStream(Sc.getInputStream());
          inputStr = conDis.readUTF();
          System.out.println("Get From Client Input Data:" + inputStr.trim());          DataOutputStream conDos = new DataOutputStream(Sc.getOutputStream());
          conDos.writeInt(1);
          conDis.close();
          conDos.close();
          Sc.close();
          System.out.println("port "+conPort+" is closed!");
          portManage.putSparePort(conPort);
        }
        catch (IOException e) {
          System.out.println("read port " + conPort + " error:" + e);
        }

解决方案 »

  1.   

    你使用的是什么服务器,可能你装了超过一个的WEB服务器,其中一个服务器强占了JVM。
    好象WebLogic6.0有这个毛病,其他的就不太清楚了。
    Tomcat5.0没这个问题
      

  2.   

    监听客户端连接请求的代码:
            Socket sc = ss.accept();
            System.out.println("\naccept ");
            DataOutputStream dos = new DataOutputStream(sc.getOutputStream());        conPort=portManage.getSparePort();
            dos.writeInt(conPort); //The client recieve this port number,
            //and try to connect at it        GetClientInput clientInput = new GetClientInput(conPort,portManage);
            new Thread(clientInput).start();
      

  3.   

    to handsomeghost(方鸿渐) :
      我没有在WebLogic或者tomcat上运行这个程序,只是把服务器程序写在一个class里头运行的。
      原来不关闭socket的时候出现过这个异常,但是现在关闭了socket,还是有这种异常,不知道有没有其他原因。
      

  4.   

    全部代码:
    package com.utic.standard.socketServer;import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.Vector;
    import java.util.Vector;public class SocketServer
        implements Runnable {
      public void run() {
        try {
          String line = null;      try {
            WaitConnect wait = new WaitConnect();
            Thread tw = new Thread(wait);
            tw.start();
            System.out.println("tw.start();");
          }
          catch (Exception ee) {
            System.out.println("[DebugLog]" + ee);
            ee.printStackTrace();
            System.out.println("[DebugLog]ignore this exception and continue");
          }
        }
        catch (Exception e) {
          System.out.println("[DebugLog]" + e);
        }
      }
    }class WaitConnect
        implements Runnable {
      public static int port = 9414; //协商端口,客户使用这个端口获得连接端口
      public static PortManage portManage=null;
      public static int conPort = 0;//连接端口
      public WaitConnect() {
        System.out.println("new WaitConnect()");
        portManage=new PortManage();
      }  public void run() {
        ServerSocket ss = null;
        try {
          ss = new ServerSocket(port);
        }
        catch (Exception sse) {
          sse.printStackTrace();
        }
        while (true) {
          try {        System.out.println("Lieson in port :"+port);
            Thread.sleep(500);
            Socket sc = ss.accept();
            System.out.println("\naccept ");
            DataOutputStream dos = new DataOutputStream(sc.getOutputStream());        conPort=portManage.getSparePort();
            dos.writeInt(conPort); //The client recieve this port number,
            //and try to connect at it        GetClientInput clientInput = new GetClientInput(conPort,portManage);
            new Thread(clientInput).start();        System.out.println("[DebugLog:Connection establish at " + conPort +
                               "]");        dos.close();
            sc.close();
          }
          catch (Exception e) {
            System.out.print("[DebugLog:run]");
            e.printStackTrace();
          }
        }
      }
    }
    //提供管理端口分配的功能
    class PortManage{
      private static int port=10000;
      private static Vector busyPort=new Vector();
      private static Vector sparePort=new Vector();  public PortManage(){
        for (int i = 0; i < 10; i++) {
          this.sparePort.add(new Integer(port + i));
        }
        System.out.println("new PortManage()");
      }
      public int getSparePort(){
        if(sparePort.isEmpty()){
          System.out.println("the port manager is empty!");
          return -1;
        }
        else{
          int returnPort = ((Integer)sparePort.firstElement()).intValue();
          sparePort.remove(new Integer(returnPort));
          busyPort.add(new Integer(returnPort));
          return returnPort;
        }
      }  public void putSparePort(int newPort){
        busyPort.remove(new Integer(newPort));
        sparePort.add(new Integer(newPort));
      }
    }class GetClientInput
        implements Runnable {
      int conPort = 0;
      PortManage portManage=null;
      GetClientInput(int conPort,PortManage portmanage) {
        this.conPort = conPort;
        this.portManage = portmanage;
      }  public void run() {
        try {
          while (true) {
            ServerSocket conSs = new ServerSocket(conPort);
            Socket Sc = conSs.accept();
            conSs.close();
            try {
              String inputStr = null;
              DataInputStream conDis = new DataInputStream(Sc.getInputStream());
              inputStr = conDis.readUTF();
              System.out.println("Get From Client Input Data:" + inputStr.trim());          DataOutputStream conDos = new DataOutputStream(Sc.getOutputStream());
              conDos.writeInt(1);
              conDis.close();
              conDos.close();
              Sc.close();
              System.out.println("port "+conPort+" is closed!");
              portManage.putSparePort(conPort);
            }
            catch (IOException e) {
              System.out.println("read port " + conPort + " error:" + e);
            }
          }
        }
        catch (IOException e) {
          System.out.println("socket error in port: " + conPort);
          System.out.println(e);
        }
      }
    }