Java Android套接字到服务器,服务器报告“;“连接重置”;错误
我正在为电子邮件系统开发一个Android应用程序,该应用程序需要连接服务器(由我的合作伙伴编写)。 我的手机使用PC生成的wifi,当我尝试登录时,服务器在10秒内只报告了几次“连接重置”错误Java Android套接字到服务器,服务器报告“;“连接重置”;错误,java,android,sockets,Java,Android,Sockets,我正在为电子邮件系统开发一个Android应用程序,该应用程序需要连接服务器(由我的合作伙伴编写)。 我的手机使用PC生成的wifi,当我尝试登录时,服务器在10秒内只报告了几次“连接重置”错误 Client Login IP:59.71.68.237 Server host:Iris Time:2015.06.13 at 09:04:36 java.net.SocketException:Connection reset at java.net.SocketInputStream.
Client Login IP:59.71.68.237 Server host:Iris Time:2015.06.13 at 09:04:36
java.net.SocketException:Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputSteamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.mailService.emsp.tempEMSP.run(tempEMSP.java:42)
at java.lang.Thread.run(Thread.java:745)
我在Android中调试,发现代码在执行后停止
client = new Socket(ServerInfo.sockethost, ServerInfo.socketport);
服务器会在这之后报告错误
这是客户端代码
public class Login extends Activity {
private Intent intent = null;
private EditText account = null; //Account
private EditText passwpord = null; //password
private Button login = null; // login
private TextView register = null; // register
private String useraccount = null;
private String userpassword = null;
private Dialog m_Dialog = null; // dialog
private Socket client; //client socket
private BufferedReader reader;
private PrintWriter writer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
intent = this.getIntent();
account = (EditText) this.findViewById(R.id.login_account);
passwpord = (EditText) this.findViewById(R.id.login_password);
login = (Button) this.findViewById(R.id.login_button);
register = (TextView) this.findViewById(R.id.login_register);
register.setClickable(true);
//login button event
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
useraccount = account.getText().toString();
userpassword = account.getText().toString();
//judge whether the account is null
if (Regex.isEmpty(useraccount)) {
Toast.makeText(getApplicationContext(), "Please fill in account!",
Toast.LENGTH_SHORT).show();
}
// judge whether the account meets email form
else if (Regex.isEmail(useraccount)) {
Toast.makeText(getApplicationContext(), "account form is wrong!",
Toast.LENGTH_SHORT).show();
}
// judge whether password is null
else if (Regex.isEmpty(userpassword)) {
Toast.makeText(getApplicationContext(), "Please fill in the password!",
Toast.LENGTH_SHORT).show();
} else {
System.out.println("Client:Connecting");
m_Dialog = ProgressDialog.show(Login.this, "Hint", "Login...",true);
// create a new thread
new Thread(new Runnable() {
int flag = 0;
int mark = 0;
// based on the custom protocol
@Override
public void run() {
try {
// create a new socket
System.out.println("new Socket('', 6666);");
//This is where the code stops
client = new Socket(ServerInfo.sockethost, ServerInfo.socketport);
System.out.println("Socket 'client' created.");
String command = null;
System.out.println("new reader.");
// IO reader and writer
reader = new BufferedReader(
new InputStreamReader(client.getInputStream()));
System.out.println("new writer.");
writer = new PrintWriter(client.getOutputStream(), true);
// Client:login
writer.println("login");
while ((command = reader.readLine()) != null) {
StringTokenizer stk = new StringTokenizer(command);
String cmd = stk.nextToken();
System.out.println("cmd:" + cmd);
if (cmd.equals("user")) {
writer.println(useraccount);
System.out
.println("writer.println('useraccount');");
} else if (cmd.equals("pass")) {
writer.println(userpassword);
System.out
.println("writer.println('userpassword');");
} else if (cmd.equals("inexist")) {
writer.println(useraccount);
System.out
.println("inexist, writer.println('username');");
} else if (cmd.equals("error")) {
writer.println(useraccount);
System.out
.println("error, writer.println('username');");
} else if (cmd.equals("loginok")) {
System.out.println("case 'loginok'");
mark = 1;
System.out.println("========mark = "
+ mark);
break;
}
}
System.out.println("Login succeed!,flag=0");
} catch (Exception e) {
flag = 1; // Login fail
e.printStackTrace();
} finally {
try {
if (client != null)
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
m_Handler.post(new Runnable() {
@Override
public void run() {
if (flag == 0) {
ServerInfo.username = useraccount;
ServerInfo.password = userpassword;
intent.setClass(Login.this,
MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("username",
useraccount);
bundle.putString("password",
userpassword);
intent.putExtras(bundle);
startActivity(intent);
Login.this.finish();
System.out
.println("Client: Login succeed!");
Toast.makeText(getApplicationContext(),
"Login succeed!!", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(),
"Login fail!", Toast.LENGTH_SHORT)
.show();
}
}
});
m_Dialog.dismiss();
}
}).start();
}
}
});
}
}
当我独立于应用程序执行客户机代码时,它工作得很好
有什么问题?您有什么建议吗?我看不出有什么问题,但如果服务器多次报告相同的错误,肯定会出现严重问题。问题就在那一头。显然,在流结束后,他仍在继续使用套接字。您是否设置了访问网络的正确权限?日志中是否有任何异常?是的。我有。登录“logcat”显示编码在新套接字()之后停止。在长时间尝试与服务器建立连接后,它会报告异常。【连接超时】由于连接超时,您无法对从未存在的连接进行“连接重置”。下定决心吧。你的问题是什么?