2011-12-07 11 views
2

私はAndroidアプリケーションを開発しようとしていますが、電話機をサーバーに接続しようとするといくつかの問題が発生しています。もともと私のサーバーに接続しようとすると、マニフェストにパーミッションを入れて最終的に解決したIOExceptionが発生しました。今私はソケット例外を取得しています: "接続が拒否されました"、私は完全にサーバーに接続して正常に動作する単純なJavaで自分のコンピュータ上の別のプログラムを実行できるようにサーバーがリッスンしていると確信しています。エミュレータと私の実際の電話(私のWiFiネットワーク上)で、クライアントアプリケーションをコンピュータのIPアドレスと "localhost"の両方で実行しました。私の質問は、誰かがこれがなぜ起こっているのかという考えがあるかどうかです。これは、コードの一部です:AndroidアプリのJavaソケット例外、ヘルプが必要ですか?

クライアント:

package com.patyo.money4free; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Tutorial_Accountname extends Activity{ 
    Button bSubmit; 
    EditText Account,ConfirmAccount; 
    TextView ErrorText; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tutorial_accountname); 

    bSubmit = (Button) findViewById (R.id.AccountNameSubmitButton); 
    Account = (EditText) findViewById (R.id.AccountName); 
    ConfirmAccount = (EditText) findViewById (R.id.ConfirmAccountName); 
    ErrorText = (TextView) findViewById (R.id.AccountNameErrorText); 

    if(!TutorialGolbals.Username.equals("")) 
    { 
     Account.setText(TutorialGolbals.Username); 
     ConfirmAccount.setText(TutorialGolbals.Username); 
    } 


    bSubmit.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      String username = Account.getText().toString(); 
      String confusername = ConfirmAccount.getText().toString(); 

      if(username.equals(confusername)){ 
       if(username.equals("")) 
       { 
        ErrorText.setTextColor(Color.RED); 
        ErrorText.setText("Username Field is Empty!"); 
       }else{ 
        ErrorText.setText("Testing Account..."); 
        BufferedReader in = null; 
        PrintWriter out = null; 
        Socket connection = null; 
        try { 
         //This is where it throws exception 
         connection = new Socket(Server_Globals.address,Server_Globals.port_create); 
         in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
         out = new PrintWriter(connection.getOutputStream(), true); 
        } catch (UnknownHostException e) { 
         ErrorText.setTextColor(Color.RED); 
         ErrorText.setText("Sorry, Cannot Connect to Server"); 
         return; 
        } catch (IOException e) { 
         ErrorText.setTextColor(Color.RED); 
         ErrorText.setText("Sorry, Cannot Connect to Server"); 
         return; 
        } 
        String s = ""; 
        s+="Try Account\r\n"; 
        s+=username+"\r\n"; 
        out.write(s); 
        out.flush(); 
        boolean reading = true; 
        String response = null; 
        try { 
         while(reading){ 
           if(in.ready()) 
           { 
            response = in.readLine(); 
            reading = false; 
           } 
         } 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         reading = false; 
         ErrorText.setTextColor(Color.RED); 
         ErrorText.setText("Sorry, Cannot Connect to Server"); 
        } 

        if(response.equals("TRUE")){ 
         Intent nextArea = new Intent("com.patyo.money4free.TUTORIALEMAIL"); 
         TutorialGolbals.Username = username; 
         startActivity(nextArea); 
        } 
        else if(response.equals("FALSE")){ 
         ErrorText.setTextColor(Color.RED); 
         ErrorText.setText("Someone Already Has That Username!"); 
        } 
       } 
      }else{ 
       ErrorText.setTextColor(Color.RED); 
       ErrorText.setText("Usernames are Not the Same!"); 
      } 
     } 
    }); 
} 
} 

接続を探しServerの一部:私だったら

package com.patyo.money4free.server; 

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Lookout_CreateAccount { 

private static final int port = 5222; 
public static void main(String[] args) { 
    ServerSocket server = null; 
    Socket buffer = null; 
    try { 
     server = new ServerSocket(port); 
     System.out.println("Server Started..."); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     System.exit(-1); 
    } 
    while(true) 
    { 
     try { 
      buffer = server.accept(); 
      System.out.println("Server Accepted Client"); 
      Thread buff = new Thread(new CreateAccountHandler(buffer)); 
      buff.start(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

} 

答えて

1

拒否した接続は、ファイアウォールが原因で発生することができあなたは私のサーバー上のファイアウォールを無効にしようとすると、私は開いているipadressで私のサーバーを走らせるまで同じ問題を抱えていた

+0

良いawnserが、それはまだ接続拒否されたエラー – user1036756

関連する問題