此服务端使用socket写的。当安装这android程序到手机并且运行后,在android的浏览器中访问http://127.0.0.1:9999/printLabel?userName=中国人&orderNo=1354&areaNo=2这个网址后这个服务端会自动获取到想要的参数。但是获取之后好像自动退出了,再打开此程序的时候就报java.net.BindException: bind failed: EADDRINUSE (Address already in use)错误
,报的主程序错误的66行对应这里的26行。请大神们看看到底什么原因?要怎么解决?报错如下图:
android服务端代码如下:
public class MainActivity extends Activity {
private static EditText etUserName;
private static EditText etAreaNo;
private static EditText etOrderNo;
// private String data;
private ServerSocket ss;
private Socket s;
// private BufferedReader in;
private static Handler handler;
private static String userName;
private static String orderNo;
private static String areaNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etUserName = (EditText) findViewById(R.id.etUserName);
etAreaNo = (EditText) findViewById(R.id.etAreaNo);
etOrderNo = (EditText) findViewById(R.id.etOrderNo);
// 创建属于主线程的handler
handler = new Handler();
new Thread(new Runnable() {
public void run() {
try {
ss = new ServerSocket(9045);
while (true) {
s = ss.accept();
System.out.println(s);
invoke(s);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}).start();
}
private static void invoke(final Socket s) throws IOException {
// 创建新线程
new Thread(new Runnable() {
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(
s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
while (true) {
String data = in.readLine();
if (data == null) {
return;
} else {
try {
data = URLDecoder.decode(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("客户端传过来的数据为:" + data);
if (data.startsWith("GET")&&data.indexOf("printLabel")!=-1) {
String a = data.split("HTTP")[0];
String sid = a.split("\\?")[1];
String[] sts = sid.split("&");
userName = sts[0].split("=")[1];
orderNo = sts[1].split("=")[1];
areaNo = sts[2].split("=")[1];
System.out.println("userName=" + userName + ","+ "orderNo=" + orderNo + "," + "areaNo="+ areaNo);
handler.post(runnableUi);
}
out.println("success");
out.flush();
}
break;
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
try {
out.close();
} catch (Exception e) {
}
try {
s.close();
} catch (Exception e) {
}
}
}
}).start();
}
// 构建Runnable对象,在runnable中更新界面
static Runnable runnableUi = new Runnable() {
@Override
public void run() {
// 更新界面
etUserName.setText(userName);
etAreaNo.setText(areaNo);
etOrderNo.setText(orderNo);
}
};
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
重复绑定了,程序没有完全退出,在应用程序里杀死请教一下要怎么做呢?服务当掉后没有停止,容器还是运行的?<divclass="ref">