ServerSocket不接受连接

时间:2016-04-28 09:06:05

标签: java android

我在JAVA中有以下代码来创建服务器套接字以侦听传入连接:

public class ConnectOracle {

 public static void main(String[] args) {

  String driver = "oracle.jdbc.driver.OracleDriver"; //

  String serverName = "10.11.201.84";
  String portNumber = "1521";
  String db = "XE";
  String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
    + db; // connectOracle is the data
  // source name
  String user = "ORAP"; // username of oracle database
  String pwd = "ORAP"; // password of oracle database
  Connection con = null;
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  DataOutputStream dataOutputStream = null;

  try {
   Class.forName(driver);// for loading the jdbc driver

   System.out.println("JDBC Driver loaded");

   con = DriverManager.getConnection(url, user, pwd);// for
                // establishing
   // connection
   // with database
   Statement stmt = (Statement) con.createStatement();

   serverSocket = new ServerSocket(8888);
   System.out.println("Listening :8888");

   while (true) {
    try {

     socket = serverSocket.accept();
     System.out.println("Connection Created");
     dataInputStream = new DataInputStream(
       socket.getInputStream());
     dataOutputStream = new DataOutputStream(
       socket.getOutputStream());
     System.out.println("ip: " + socket.getInetAddress());
     // System.out.println("message: " +
     // dataInputStream.readUTF());

     ResultSet res=stmt.executeQuery("select * from food_category");
     while(res.next()){
      System.out.println(res.getString(1));
     }

    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

    if (dataInputStream != null) {
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if (dataOutputStream != null) {
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

在我的Android程序中,我正在尝试使用此JAVA程序创建连接。代码如下:

class ConnectToOracle extends AsyncTask<String, String, String> {

         Socket socket = null;
         DataOutputStream dataOutputStream = null;
         DataInputStream dataInputStream = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ;
        }

        // Download Music File from Internet
        @Override
        protected String doInBackground(String... f_url) {
            try {
                 socket = new Socket("10.11.201.84", 8888);
                 dataOutputStream = new DataOutputStream(socket.getOutputStream());
                 dataInputStream = new DataInputStream(socket.getInputStream());
                 dataOutputStream.writeUTF(textOut.getText().toString());
                 textIn.setText(dataInputStream.readUTF());
            } catch (UnknownHostException e) {
             // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            finally{
                if (socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                 // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }

            if (dataOutputStream != null){
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

          if (dataInputStream != null){
              try {
                  dataInputStream.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
         }
            return null;

        }


        // Once Music File is downloaded
        @Override
        protected void onPostExecute(String file_url) {
            // Dismiss the dialog after the Music file was downloaded

        }
    }

但是没有建立联系。我该如何建立连接?任何建议都有很大帮助。

0 个答案:

没有答案