📄 network.java
字号:
/*This is the network class of the PollClient. It handles all the network stuffCopyright (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 PollClient;import java.net.*;import java.io.*;public class Network { private final static int BUFFER_SIZE = 4096*4; public final static int PORT_NUMBER = 6969; public final static byte CHECK_BYTE = 0; public final static byte CLIENT_SENDING_ANSWER = 100; public final static byte SERVER_SENDING_QUESTION = 1; public final static byte SERVER_DISCONNECTED = 10; public final static byte QUESTION_CLOSED = 11; public final static byte CONNECTION_TIMED_OUT = 2; private OutputStream out; private InputStream in; private Socket clntSock; public Network(String addr, int port) throws IOException { try { clntSock = new Socket(addr, port); clntSock.setSoTimeout(750); in = clntSock.getInputStream(); out = clntSock.getOutputStream(); } catch (UnknownHostException e) { System.out.println("Don't know about that host"); throw e; } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to the server"); throw e; } } public String arrayToString(byte[] b, int length) { return new String(b, 0, length); } public Question recieveQuestion() { byte[] bufA = new byte[BUFFER_SIZE];// byte[] bufB; try { int bytesRecieved = in.read(bufA); if (bytesRecieved < BUFFER_SIZE) return new Question(bufA);/* int qns = bytesToInt(bufA, 0); int qs = bytesToInt(bufA, 4); int as = bytesToInt(bufA, 8); int bs = bytesToInt(bufA, 12); int cs = bytesToInt(bufA, 16); int ds = bytesToInt(bufA, 20); int totalSizeOfQuestion = qns + qs + as + bs + cs + ds + 28; bufB = new byte[totalSizeOfQuestion]; bytesRecieved = in.read(bufB, BUFFER_SIZE, totalSizeOfQuestion - BUFFER_SIZE); System.arraycopy(bufA, 0, bufB, 0, BUFFER_SIZE); */ } catch (IOException e) { return null; } return new Question(bufA); } public void sendAnswer(int i) { try { out.write((byte) i); } catch (IOException e) { } } public void close() { try { clntSock.close(); } catch (IOException e) {} } public byte checkForQuestionStatus() { byte[] b = new byte[1]; b[0] = -1; int BytesRead = 0; try { BytesRead = in.read(b); if (BytesRead > 0) { switch (b[0]) { case CHECK_BYTE: out.write(CHECK_BYTE); return CHECK_BYTE; default: return b[0]; } } } catch (IOException e) { return CONNECTION_TIMED_OUT; } return CONNECTION_TIMED_OUT; } private static int bytesToInt(byte[] b, int offset) { int value = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; value += (b[i + offset] & 0x000000FF) << shift; } return value; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -