2

I am trying to develop an Android application but I am running into some problems trying to get my phone to connect to my server. Originally when trying to connect to my Server I got an IOException which I finally resolved by putting permissions in the manifest. Now I am getting an Socket Exception: "Connection Refused", I am completely sure that the Server is listening as I can run another program on my computer in just plain java that connects to the server and it works fine. I have run the client application on both the emulator and my actual phone (on my WiFi network) with both the IP address of my computer and "localhost". My question is if anyone has an idea as of why this is happening. This is some of the code:

Client:

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

The Part of the Server that looks for connections:

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 Answer 1

1

The connection refused can be caused by a firewall, if i was you i would try disabling the firewall on your server and try it again, i had the same problem untill i ran my server on an open ipadress

Sign up to request clarification or add additional context in comments.

1 Comment

Good awnser but it didn't work it is still throwing the Connection Refused Error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.