Sockets 服务器和Java小程序:连接套接字
我有一个java小程序最近在服务器更新后停止工作,更具体地说: 1.服务器从Sun更新,运行Solaris 9,32位。(2005年安装)到64位的CentOS 5(linux)。 2.小程序有两个主要类1)collect.class:从画布收集数据2)server.class:通过端口侦听collect.class并相应地执行操作 但是小程序被卡住了,我检查了start_server.sh(它生成一个报告nohup.out)有一行Sockets 服务器和Java小程序:连接套接字,sockets,Sockets,我有一个java小程序最近在服务器更新后停止工作,更具体地说: 1.服务器从Sun更新,运行Solaris 9,32位。(2005年安装)到64位的CentOS 5(linux)。 2.小程序有两个主要类1)collect.class:从画布收集数据2)server.class:通过端口侦听collect.class并相应地执行操作 但是小程序被卡住了,我检查了start_server.sh(它生成一个报告nohup.out)有一行 Exception creating server socke
Exception creating server socket: java.net.BindException: Address already in use
这很奇怪,因为collect.class使用的端口=9999没有问题。为什么问题只发生在server.class(谁听collet.class)中
请帮忙
其他信息:
I.IN COLLECT.JAVA:
有一个带有网格的画布,用户在网格上绘制一些区域,然后单击“提交”。
->触发MineCanvas.submit()->该区域的值由MineCanvas.ComputeGridValue()计算->然后Collect.cleintSend(卡在此处)
创建服务器套接字时出现异常:java.net.BindException:地址
已在使用
此异常表示套接字尝试绑定到的端口号(套接字尝试在连接的本地端使用的端口号)已被其他程序使用。要修复它,您需要找出其他哪些软件正在使用该端口并查看是否可以安全地更改它,或者更改程序正在使用的端口
编辑:可能值得尝试寻找很少使用的端口,以减少使用另一个已知由某些常用软件使用的端口的机会,Wikipedias列出了常用程序和服务使用的典型TCP和UDP端口。谢谢和链接-非常感谢!我刚刚更新了我的问题。有一件事我还不确定。请帮忙!谢谢为此,我需要一些额外的信息:collect类和server类是否都使用相同的端口打开ServerSocket?如果是,它们不能同时使用同一端口,一次只能有一个套接字使用同一端口(不管套接字是由相同或不同的程序打开的)。在使用同一端口打开新套接字之前,需要先关闭先前的套接字。如果您能提供一个类如何工作的简单示例(仅限于套接字使用部分),可能会有所帮助!是的,那太棒了!我们已经为此工作了一段时间。现在让我通过添加更多信息来更新问题。谢谢嘿,esaj,我已经更新了附加信息。请看这是否足够,我尽可能多地提取了不相关的信息。谢谢clientSend方法是如何“卡住”的?执行停止,软件冻结?你有可用的调试器吗?或者你知道它卡在哪一行吗?
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class Collect extends Applet {
...
public static final int PORT = 8888;
...
public boolean action(Event e, Object arg) {
...
if (arg.equals("Submit")) {
if (action(null, "Update Grid")) {
minecanvas.Submit();
} else {
return true;
}
}
return true;
}
...
public void clientSend(){
s = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(s.getInputStream());}
out = new DataOutputStream(s.getOutputStream());
listener = new SolutionListener(in, minecanvas);}
minecanvas.mode = MineCanvas.SUBMITTING;
minecanvas.repaint();
int n = 1;
out.writeBytes(minecanvas.gridh + "\n" + minecanvas.gridw + "\n");
for (int h = 0; h < minecanvas.gridh; h++) {
for (int w = 0; w < minecanvas.gridw; w++) {
out.writeBytes(n + " " + minecanvas.AllCells[w][h].net + "\n");
n++;
}
}
out.writeBytes("done\n");
s = null;
in = null;
out = null;
}
}
class MineCanvas extends Canvas {
...
public int gridw = 0; // number of grid squares width-ly
public int gridh = 0; // number of grid squares height-ly
public GridCell[][] AllCells; // array of grid cells comprising the grid
...
// compute values for minecanvas
public void ComputeGridValue() {...}
public void Submit() {
ComputeGridValue();
parent.clientSend();
}
...
}
...
}
import java.io.*;
import java.net.*;
public class Server extends Thread {
private OPM_Server opm; // this is the corresponding server for collect
...
public Server() {
...
opm = new OPM_Server();
}
public static void main(String[] args) {
new Server();
}
}
...
// OPM: correspond to Collect
class OPM_Server extends Thread {
public final static int DEFAULT_PORT = 8888;
protected int port;
protected ServerSocket listen_socket;
public static void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
}
public OPM_Server() {
this.port = DEFAULT_PORT;
try { listen_socket = new ServerSocket(port); }
catch (IOException e){ fail(e, "Exception creating server socket");}
System.out.println("Server: listening on port " + port);
this.start();
}
public void run() {
try {
while(true) {
System.out.println("I got to before ServerSocket");
Socket client_socket = listen_socket.accept();
OPM_Connection c = new OPM_Connection(client_socket);
}
}
catch (IOException e) {fail(e, "Exception while listening for connections");}
}
}
...
class OPM_Connection extends Thread {
protected Socket client;
protected BufferedReader in;
protected DataOutputStream out;
File mine_data = new File("mine_data"); // output file data
FileOutputStream file_stream;
DataOutputStream file_out;
public OPM_Connection(Socket client_socket) {
client = client_socket;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
try {
client.close();
} catch (IOException e2) {
}
;
System.err.println("Exception while getting socket stream: "
+ e.toString());
return;
}
this.start();
}
public void run() {
...
file_stream = new FileOutputStream(mine_data);
file_out = new DataOutputStream(file_stream);
...// write to mine data
file_out = null;
if (inputGood == true) {
System.out.println(pid + "> ---Got all info from client");
Runtime r = Runtime.getRuntime();
Process Aproc = null;
Process Bproc = null;
int returnVal = -1;
try {
Aproc = r.exec("runOPM");
} catch (IOException e) {
inputGood = false;
System.out.println(pid + "> runOPM didn't exec");
}
try {
returnVal = Aproc.waitFor();
} catch (InterruptedException e) {
inputGood = false;
System.out.println(pid + "> runOPM didn't return");
}
System.out.println(pid + "> ---All execing done");
File report = new File("mine_report");
FileInputStream report_stream = null;
...
// create a mine report
System.out.println(pid + "> ---Done sending data back to client");
}
try {
client.close();
} catch (IOException e2) {
}
;
System.out.println(pid + "> EXITING THREAD");
}
}