2

我正在尝试开发一个 Android 应用程序,但在尝试让我的手机连接到我的服务器时遇到了一些问题。最初在尝试连接到我的服务器时,我得到了一个 IOException,我最终通过在清单中添加权限来解决。现在我得到一个套接字异常:“连接被拒绝”,我完全确定服务器正在侦听,因为我可以在我的计算机上运行另一个程序,只用连接到服务器的普通 java,它工作正常。我已经在模拟器和我的实际手机(在我的 WiFi 网络上)上运行了客户端应用程序,同时使用了我的计算机的 IP 地址和“localhost”。我的问题是,是否有人知道为什么会发生这种情况。这是一些代码:

客户:

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!");
            }
        }
    });
}
}

寻找连接的服务器部分:

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();
        }
    }

}

}
4

1 回答 1

1

连接被拒绝可能是防火墙引起的

于 2011-12-07T02:19:08.343 回答