服务器和Java Applet:连接套接字

时间:2012-01-15 07:58:23

标签: sockets

我有一个Java applet最近在服务器更新后停止工作,更具体地说: 1.服务器从Sun更新,运行Solaris 9,32位。 (安装在2005年)到CentOS 5,(linux)64位。 2. applet有两个主要的类1)collect.class:从画布收集数据2)server.class:通过PORT监听collect.class并采取相应的行动;

但applet卡住了,我检查了start_server.sh(产生报告nohup.out)有一行

Exception creating server socket: java.net.BindException: Address already in use

这很奇怪,因为PORT = 9999,collect.class使用没有问题。为什么问题只发生在server.class(谁监听collet.class)。

请帮忙!

其他信息:

I.IN COLLECT.JAVA: 网格上有一个带网格的画布,用户在网格上绘制一些区域并单击“提交”。 - > MineCanvas.submit()被触发 - >该区域的值由MineCanvas.ComputeGridValue() - >计算得出。然后Collect.cleintSend(卡在这里)

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();
}

    ...

}
...

}

II。 SERVER.JAVA

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");
}
} 

1 个答案:

答案 0 :(得分:0)

  

创建服务器套接字的异常:java.net.BindException:Address   已经在使用

此异常表示套接字尝试绑定的端口号(套接字尝试在连接的本地端使用的端口号)已被其他程序使用。要修复它,您需要找出使用该端口的其他软件,看看您是否可以安全地更改它,或者更改程序正在使用的端口。

编辑:可能值得尝试寻找很少使用的端口,以减少使用另一个已知由某些常见软件使用的端口的机会,here's维基百科的典型TCP列表和普通程序和服务使用的UDP端口。