我正在尝试在处理中创建客户端-服务器通信。这是 server.pde 的剥离版本:
cThread thread;
ServerSocket socket1;
int main_sid = 0;
int main_port = 5204;
void setup() {
size(300, 400);
try {
ServerSocket socket1 = new ServerSocket(main_port);
} catch (Exception g) { }
}
void draw() {
try{
Socket main_cnn = socket1.accept();
thread = new cThread(main_cnn,main_sid,20);
thread.start();
println("New client: " + main_cnn.getRemoteSocketAddress() + " Assigned sid: " + main_sid);
main_sid++;
} catch (Exception g) { }
}
class cThread extends Thread { ...
设置循环应该初始化ServerSocket
并且绘制循环应该尝试不断地接受客户端。
问题是ServerSocket socket1 = new ServerSocket(main_port);
它应该只初始化一次,但是当把它放在这样的设置中时不起作用。
我应该怎么办?