我想在我的Flash应用程序可以听到的JAVA中构建一个非常简单的套接字服务器。 I am using this tutorial。一切似乎都在起作用 - JAVA代码被编译并且服务器正在运行。如何运行允许传入连接的简单套接字服务器?
我的问题是:外部应用程序如何使用IP地址和端口号将消息发送到此服务器?我的目标是Flash可以侦听由外部应用程序发送的套接字消息。
的Java代码:
import java.io.*;
import java.net.*;
class SimpleServer {
private static SimpleServer server;
ServerSocket socket;
Socket incoming;
BufferedReader readerIn;
PrintStream printOut;
public static void main(String[] args) {
int port = 8080;
try {
port = Integer.parseInt(args[0]);
} catch (ArrayIndexOutOfBoundsException e) {
// Catch exception and keep going.
}
server = new SimpleServer(port);
}
private SimpleServer(int port) {
System.out.println(">> Starting SimpleServer");
try {
socket = new ServerSocket(port);
incoming = socket.accept();
readerIn = new BufferedReader(
new InputStreamReader(
incoming.getInputStream()));
printOut = new PrintStream(incoming.getOutputStream());
printOut.println("Enter EXIT to exit.\r");
out("Enter EXIT to exit.\r");
boolean done = false;
while (!done) {
String str = readerIn.readLine();
if (str == null) {
done = true;
} else {
out("Echo: " + str + "\r");
if(str.trim().equals("EXIT"))
done = true;
}
incoming.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
private void out(String str) {
printOut.println(str);
System.out.println(str);
}
}
你收到4个不同的答案给你的问题。这其中的一个问题是否是你的问题? –
我非常感谢答案,但我还没有时间来构建新的服务器测试! – Kokodoko