📄 pollserverthread.java
字号:
/*PollServerThread is the class that listens for the clients connectingCopyright (C) 2005-2006 Igor Partola, Michael J. Krikonis, Clark UniversityThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/package PollServer;import java.net.*;import java.io.*; class PollServerThread implements Runnable { private PollServer parent; private Thread t; private int clientCount = 0; private ServerSocket servSock; private boolean endOfLife = false; private static final int SERVER_PORT = 6969; PollServerThread(PollServer p) { parent = p; } public void start() { t = new Thread(this); t.start(); } public void run() { Thread thisThread = Thread.currentThread(); Socket clntSock; try { servSock = new ServerSocket(SERVER_PORT); servSock.setSoTimeout(50); while (true) { if (endOfLife) break; parent.setServerStatus(true); try { clntSock = servSock.accept(); if (clntSock != null) { clntSock.setSoTimeout(750); new TalkThread(clntSock, this).start(); clntSock = null; } } catch (IOException e) { } try { t.sleep(50); } catch(InterruptedException e) { } } } catch (IOException e) { System.out.println("Cannot serve because of a network problem"); parent.setServerStatus(false); } try { if (servSock != null) servSock.close(); parent.setServerStatus(false); } catch(IOException e) { } } public void stop() { endOfLife = true; } public PollServer getParent() { return parent; } public boolean isDead() { return endOfLife; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -