你是用SQL Server么,好象是和数据库建立连接会出现的问题。改变一下SQL Server 的身份认证模式也许会好。

解决方案 »

  1.   

    提示是:建立socket有错误。一般是程序的问题。
      

  2.   

    通讯的指定服务端口最好用来监听,不要直接使用,我不知道你是不使用的ServerSocket,他负责监听,然后把实际的连接转到别的端口上
    访问端的Socket我记得有个不用指定端口号的构造器,他会使用空闲的Socket----------------------------------------
    以上只是我的理解和想法,如有不对欢迎指教
      

  3.   

    监听的类:
    public SocketServer(int port, MsgRecvInterface msgRecv)
      {
        t = new Thread(this, "数据接收线程");
        this.port = port;
        msgRecvInterface = msgRecv;
        t.start();
      }  public void run()
      {
        Logs.println("正在启动" + t.getName() + "...");
        Socket client = null;
        String clientList= BaseConfig.getInstance().getParamterValue("网关验证","合法IP"," ");
        String flag = BaseConfig.getInstance().getParamterValue("网关验证","连接需要验证","否");    try
        {
          socket = new ServerSocket(port);
          Logs.println("成功启动" + t.getName() + "![" + port + "]");
          Logs.println("当前接口程序的版本号:" + com.bestleader.comm.Version.getVersion());
        }
        catch (IOException ex)
        {
          Logs.printError(ex,"无法启动" + t.getName());
        }    while (!isClose)
        {
          try
          {
            client = socket.accept();        if (client != null)
            {
              if (flag.equals("是") && clientList.indexOf(client.getInetAddress().getHostAddress()) < 0)
              {
                Logs.printError("非法的IP["+client.getInetAddress().getHostAddress()+"]请求连接,已被系统断开");
                client.close();
                continue;
              }
              new Client(client, msgRecvInterface);
            }
          }
          catch (SocketTimeoutException ex) //超时
          {
            continue;
          }
          catch (Exception ex)
          {
            break;
          }
        }
        try
        {
          socket.close();
        }
        catch (IOException ex)
        {}
        Logs.println(t.getName() + "已关闭.");
      }
    }发送的方法:
    public static int sendTo(String host,int port,byte[] buffer)
      {
        int result=0;
        try
        {
          Socket socket = socketConnect(host,port);
          if (socket == null) return -255;      DataOutputStream out = new DataOutputStream(socket.getOutputStream());
          out.write(buffer);
          out.flush();      if (Tools.byteToInt(buffer,8) == 0) //同步传送
          {
            DataInputStream in = new DataInputStream(socket.getInputStream());
            byte[] head = new byte[12];
            if (in.read(head, 0, 12) == 12)
            {
              int size = Tools.byteToInt(head);
              byte[] body = new byte[size];
              ShortMessage msg = null;          if (in.read(body, 0, size) == size)
              {
                try
                {
                  msg = new ShortMessage(body);
                  String errorCode = msg.getNodeItem("Body", "ErrorCode");
                  if (errorCode == null)
                  {
                    result = 999;
                  }
                  else
                  {
                    result = Integer.parseInt(errorCode, 10);
                  }
                  if (result != 0)
                  {
                    errorCode = msg.getNodeItem("Body", "ErrorMsg");
                    Logs.printError("数据发送失败:TxId=" + msg.GetTxId() + ",ErrorCode="+result+",ErrorMsg=" +
                                    errorCode);
                  }
                }
                catch (Exception ex)
                {
                  Logs.printError(ex,"创建XML数据包失败");
                  result = -999;
                }
              }
            }
          }
          closeSocket(socket);
          socket.close();
          return result;
        }
        catch(IOException ex)
        {
          Logs.printError(ex,"发送数据到 " + host + ":" + port + "失败!");
        }
        return -999;
      }
    /**
       * 建立Socket连接
       * @param host String IP地址
       * @param port int  端口号  
       * @return Socket
       */
      public  static Socket socketConnect(String host,int port)
      {
        try
        {
          return new Socket(host, port);
        }catch(UnknownHostException ex)
        {
          Logs.printError("非法目标地址或端口号 " + host + ":" + port);
          return null;
        }catch(IOException ex)
        {
          Logs.printError("无法连接 " + host + ":" + port);
        }
        return null;
      }对于  fpwang(胖子) 说的:"通讯的指定服务端口最好用来监听,不要直接使用"这句话有点不太明白是什么意思?
      

  4.   

    看来fpwang(胖子)猜的尽管不中,也差得不远..
    楼主监听和数据传输的SOCKET倒是分开了的,只是它们却都弄在同一线程...