我正在尝试开发一个Android应用程序但是我遇到了一些问题,试图让我的手机连接到我的服务器。最初在尝试连接到我的服务器时,我得到了一个IOException,我最终通过在清单中放置权限来解决这个问题。现在我得到一个Socket Exception:“Connection Refused”,我完全确定服务器正在监听,因为我可以在我的计算机上运行另一个程序,只是连接到服务器的普通java,它工作正常。我已经在我的计算机的IP地址和“localhost”上的模拟器和我的实际手机(在我的WiFi网络上)上运行客户端应用程序。我的问题是,是否有人知道为什么会发生这种情况。这是一些代码:
客户端:
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();
}
}
}
}
答案 0 :(得分:1)
拒绝连接可能是由防火墙引起的,如果我是你,我会尝试禁用服务器上的防火墙再试一次,我有同样的问题,直到我在开放的ipadress上运行我的服务器