[英]Java Socket Exception on Android app, Need Help?
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. 我正在尝试开发一个Android应用程序,但是在尝试使手机连接到服务器时遇到了一些问题。 Originally when trying to connect to my Server I got an IOException which I finally resolved by putting permissions in the manifest.
最初,当尝试连接到我的服务器时,我得到了IOException,我终于通过在清单中放置权限来解决了。 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.
现在,我收到一个套接字异常:“连接被拒绝”,我完全可以确定服务器正在侦听,因为我可以用连接服务器的普通Java在计算机上运行另一个程序,并且可以正常工作。 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".
我已经在模拟器和我的实际电话(在我的WiFi网络上)上使用计算机的IP地址和“本地主机”运行了客户端应用程序。 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();
}
}
}
}
连接被拒绝可能是由防火墙引起的,如果我是您,我将尝试在服务器上禁用防火墙,然后再试一次,直到我在开放的ipadress上运行服务器之前,我都遇到了同样的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.