2012-03-08 18 views
-2

我想在android上編寫一個應用程序,其中有一個多線程服務器在計算機上運行,​​而在手機上的應用程序中有一個按鈕,一旦用戶單擊該按鈕,服務器和客戶端之間將打開一個套接字。我的問題是,一旦用戶點擊那個按鈕,我的日誌貓就會遇到很多運行時錯誤。其中之一是SocketException socket失敗(拒絕)如何從android手機打開套接字而不會發生SocketException運行時錯誤?

這是該活動的代碼有打開的插座與服務器

package guc.edu.eg; 

import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.PrintStream; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Activity1 extends Activity { 
    Button start; 
    Button help; 
    Button credentials; 
    Socket socket; 
    //public DataInputStream in=null; 
    public PrintStream out=null; 
    InetAddress IP; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); 
     //try { 
      //IP = InetAddress.getLocalHost(); 
     //} catch (UnknownHostException e) { 
      // //TODO Auto-generated catch block 
      //e.printStackTrace(); 
     //} 
     start = (Button) findViewById(R.id.start); 
     start.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       try { 
        socket= new Socket("127.0.0.1",4444); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       //try { 
        //out = new PrintStream(socket.getOutputStream()); 
        //out.println ("The socket is open at the phone!!"); 
       //} catch (IOException e) { 
        // TODO Auto-generated catch block 
        //e.printStackTrace(); 
       //} 
       //try { 
        // in = new 
         //DataInputStream(socket.getInputStream()); 
      // } catch (IOException e) { 
        // TODO Auto-generated catch block 
       // e.printStackTrace(); 
       //} 
       Intent intent = new Intent(Activity1.this,Activity2.class); 
       startActivity(intent); 
      } 
     }); 
     help = (Button) findViewById(R.id.help); 
     help.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(Activity1.this,Activity3.class); 
       startActivity(intent); 
       finish(); 

      } 
     }); 
     credentials = (Button) findViewById(R.id.credentials); 
     credentials.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(Activity1.this,Activity4.class); 
       startActivity(intent); 
       finish(); 

      } 
     }); 
    } 
} 

下面是服務器類的代碼按鈕,即實際上被放置在具有應用程序代碼的項目之外的其他項目中。

import java.io.*; 
import java.net.*; 
import java.util.LinkedList; 


// Java extension packages 


public class Server implements Runnable { 


    ServerSocket serverSocket; 
    Socket clientSocket; 
    int portNo; 

    LinkedList<ServerThread> allClients = new LinkedList<ServerThread>(); 

    public Server(int port) { 
     portNo = port; 


    } 

    public void run() { 

     try { 

      serverSocket = new ServerSocket(portNo); 

      while (true) { 


       clientSocket = serverSocket.accept(); 


       ServerThread x = new ServerThread(clientSocket, this); 
       allClients.add(x); 

       x.start(); 


      } 
     } catch (EOFException eofException) { 
      System.out.println("Client terminated connection"); 
     } catch (IOException ioException) { 
      ioException.printStackTrace(); 
     } 
    } 

    public void sendMessageToClient(String message) { 
     String[] x = message.split(","); 
     //for (int i = 0; i < allClients.size(); i++) { 
      //if (allClients.get(i).clientName.equals(x[2])) { 
       //allClients.get(i).sendMessage(x[1] + ": " + x[0]); 
       //return; 
      //} 
     //}  
     // if you didn't return then you couldn't reach your destination in your local server 
     // send the message to the network server to search for it 
    } 

    public String getLocalClientsNames() { 
     String names = ""; 
     for (int i = 0; i < allClients.size(); i++) { 
      names += allClients.get(i).clientName + ","; 
     } 
     return names; 
    } 

    public void sendClientNamesToAll() { 
     //net.sendAllClientNames(); 
    } 
    public static void main(String [] args) { 
     Server server = new Server(4444); 
     new Thread(server).start(); 
    } 

} 

這裏是放置在同一個包與服務器類

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.net.Socket; 


public class ServerThread extends Thread { 

    private Socket clientSocket; 
    private Server server; 


    String clientName; 

    private ObjectOutputStream output; 
    private ObjectInputStream input; 

    public ServerThread(Socket client, Server s) { 
     this.clientSocket = client; 
     this.server = s; 
    } 

    @Override 
    public void run() { 
     try { 
      output = new ObjectOutputStream(clientSocket.getOutputStream()); 
      output.flush(); 
      input = new ObjectInputStream(clientSocket.getInputStream()); 

      processConnection(); 
      closeConnection(); 

     } catch (IOException e) { 
      System.out.println("in or out failed"); 
     } 
    } 

    private void processConnection() throws IOException { 

     String message = "!!"; 

     System.out.println("mada5alsh el while"); 

     while (!message.equalsIgnoreCase("Client>> end")) { 

      try { 
       System.out.println("da5al el while"); 
       message = (String) input.readObject(); 
       System.out.print(message); 
       if (message.equalsIgnoreCase("Name")) { 
        clientName = (String) input.readObject(); 
        server.sendClientNamesToAll(); 
       } else { 



        server.sendMessageToClient(message); 
       } 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void closeConnection() throws IOException { 

     output.close(); 
     input.close(); 
     clientSocket.close(); 
    } 

    public void sendMessage(String message) { 
     try { 
      output.writeObject(message); 
      output.flush(); 
     } catch (IOException ioException) { 
      //do 
     } 
    } 

} 
  1. 我認爲這可能是某種與清單文件做,所以我說這行ServerThread類的代碼,它

     `<uses-permission android:name="android.permission.INTERNET"></uses-permission>` 
    

回答

0

向你添加<uses-permission android:name="android.permission.INTERNET"></uses-permission> AndroidManifest.xml應該可以做到。

1

您在不允許的主線程中創建客戶端套接字。

使用單獨的線程或使用AsyncTask。

+0

非常感謝您的建議,但我該如何做到這一點!? – 2012-03-08 09:55:58

+0

在這個html頁面的右邊緣,你會看到許多-blue-相關主題。全部閱讀。其中一個顯示如何爲客戶端創建線程的是:http://stackoverflow.com/questions/7323100/exception-in-connecting-android-tcp-client-to-java-tcp-server – Helper 2012-03-08 10:10:26

相關問題