Исключение Java Socket для Android-приложения, Нужна помощь? - PullRequest
2 голосов
/ 07 декабря 2011

Я пытаюсь разработать приложение для Android, но у меня возникают проблемы с подключением телефона к серверу.Первоначально при попытке подключиться к моему Серверу я получил исключение IOException, которое я окончательно решил, добавив разрешения в манифест.Теперь я получаю Исключение Socket: «Отказано в соединении», я полностью уверен, что Сервер слушает, так как я могу запустить другую программу на моем компьютере, используя обычную 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();
        }
    }

}

}

1 Ответ

1 голос
/ 07 декабря 2011

Отказ в соединении может быть вызван брандмауэром. Если бы я был вами, я бы попытался отключить брандмауэр на вашем сервере и повторить попытку, у меня была такая же проблема, пока я не запустил свой сервер на открытом ipadress

...