English 中文(简体)
服务器和 Java果:连接Socket
原标题:Server and Java Applet: Connecting Socket
  • 时间:2012-01-15 07:58:22
  •  标签:
  • sockets

I have a java applet recently stopped working after the server is updated, more specifically: 1. The server is updated from Sun, running Solaris 9, 32 bit. (installed in 2005) to CentOS 5, (linux) on 64 bit. 2. The applet has two major classes 1) collect.class: collects data from a canvas 2) server.class: listens to collect.class through a PORT and acts accordingly;

但是, app子 st着,我检查开端——服务器。

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

这一点是多余的,因为收集的机载=99999。 班级没有问题。 问题只出现在服务器上。 班次(听取会)。

请帮助!

参考:

I.IN COLLECT.JAVA: There is a canvas with grid on it, the user draw some area on the grid and click "Submit". -> The MineCanvas.submit() is triggered -> The value of the area is computed by MineCanvas.ComputeGridValue() -> then Collect.cleintSend (stuck here)

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 + "
" + minecanvas.gridw + "
");

    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++;
        }
    }
    out.writeBytes("done
");

        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");
}
} 
问题回答

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

这一例外意味着,一些其他方案已经使用港口号,试图对港口进行约束(贵国的港口号正试图在连接的地方端使用)。 为了确定该港口,你要么需要发现其他软件正在使用该港口,看看你是否能够安全地改变该港口,要么改变贵国方案正在使用的港口。

Edit: 或许值得尝试寻找很少使用的港口,以减少使用另一个港口的机会,而这一港口被一些通用软件





相关问题
Max Outgoing Socket Connections in .NET/Windows Server

I have a slightly unusual situation where I m needing to maintain CLIENT tcp connections to another server for thousands of mobile users on my servers (basically the mobile devices connect to my ...

Determine the remaining TTL of a multicast packet in Java

If I have a Java program that listens to multicast data, how can I tell what the remaining TTL of the incoming datagram is? I know how to set the TTL on the sending multicast socket, but I want to ...

Python socket not receiving anything

I m trying to receive a variable length stream from a camera with python, but get weird behaviour. This is Python 2.6.4 (r264:75706) on linux(Ubuntu 9.10) The message is supposed to come with a ...

Connecting to a Source game server in VB.NET

I m developing an application that detects Source-based games running on the LAN. Following up on the specifications provided by Valve, I have it narrowed down to exactly what I want: making a UDP ...

What can cause select to block in Python?

Here s a snippet of code I m using in a loop: while True: print loop rlist, wlist, xlist = select.select(readers, [], [], TIMEOUT) print selected # do stuff At a certain point, ...

Socket.UseOnlyOverlappedIO?

Does anyone know what Socket.UseOnlyOverlappedIO does, and if so, does it affect performance? There seems to be no real explanation on MSDN. Thanks in advance.

热门标签