开发者社区> 问答> 正文

服务端报java.net.BindException: bind failed:?报错

此服务端使用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);
		}
	};
}




展开
收起
爱吃鱼的程序员 2020-06-22 15:22:14 1069 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    重复绑定了,程序没有完全退出,在应用程序里杀死请教一下要怎么做呢?服务当掉后没有停止,容器还是运行的?<divclass="ref">

    引用来自“情天大圣”的答案

    <divclass=ref_body>重复绑定了,程序没有完全退出,在应用程序里杀死<divclass="ref">

    引用来自“roywang”的答案

    <divclass=ref_body>服务当掉后没有停止,容器还是运行的?你服务端是部署在哪里?tomcat?weblogic?还是什么,把容器停掉啊你不说服务端么,服务端也装android里?那就双击HOME,关闭所有应用啊晕,android,看没看啊你?有人吗?请问楼主问题解决没?我也遇到这个问题了,求解。

    2020-06-22 15:22:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载