public static void main(String[] args) { try { Socket s1 = new Socket("127.0.0.1", 57643); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF()); OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("Oh my gosh..."); dis.close(); is.close(); dos.close(); os.close(); s1.close(); } catch (ConnectException connExc) { connExc.printStackTrace(); System.out.println("Server connection failed"); } catch (IOException ioExc) { ioExc.printStackTrace(); } }
/** * Initialize a server on the given port. This server will not listen until it is launched with * the start() method. * * @param port * @throws IOException */ public Server(int port, int botPort, Retriever ret, Map<String, Double> traffic) throws IOException { if (port <= 1024 || botPort <= 1024) { throw new IllegalArgumentException("Ports under 1024 are reserved!"); } _port = port; _clients = new ClientPool(); _socket = new ServerSocket(port); try { _botSocket = new Socket("localhost", botPort); _botConnectionSuccess = true; _trafficInput = new BufferedReader(new InputStreamReader(_botSocket.getInputStream())); _trafficData = traffic; } catch (ConnectException e) { System.out.println("unable to connect"); System.out.println(e.getMessage()); _botConnectionSuccess = false; } r = ret; }
public static void main(String[] args) { int retry = 2; int port = 123; int timeout = 3000; // get the address and NTP address request InetAddress ipv4Addr = null; try { ipv4Addr = InetAddress.getByName("133.100.11.8"); } catch (UnknownHostException e1) { e1.printStackTrace(); return; } int serviceStatus = -1; DatagramSocket socket = null; long responseTime = -1; try { socket = new DatagramSocket(); socket.setSoTimeout(timeout); // will force the InterruptedIOException for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) { try { // Send NTP request byte[] data = new NtpMessage().toByteArray(); DatagramPacket outgoing = new DatagramPacket(data, data.length, ipv4Addr, port); long sentTime = System.currentTimeMillis(); socket.send(outgoing); // Get NTP Response // byte[] buffer = new byte[512]; DatagramPacket incoming = new DatagramPacket(data, data.length); socket.receive(incoming); responseTime = System.currentTimeMillis() - sentTime; double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0; // 这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差 // Validate NTP Response // IOException thrown if packet does not decode as expected. NtpMessage msg = new NtpMessage(incoming.getData()); double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - destinationTimestamp)) / 2; System.out.println( "poll: valid NTP request received the local clock offset is " + localClockOffset + ", responseTime= " + responseTime + "ms"); System.out.println("poll: NTP message : " + msg.toString()); serviceStatus = 1; } catch (InterruptedIOException ex) { // Ignore, no response received. System.out.println("InterruptedIOException: " + ex.getMessage()); } } } catch (NoRouteToHostException e) { System.out.println("No route to host exception for address: " + ipv4Addr); } catch (ConnectException e) { // Connection refused. Continue to retry. e.fillInStackTrace(); System.out.println("Connection exception for address: " + ipv4Addr); } catch (IOException ex) { ex.fillInStackTrace(); System.out.println("IOException while polling address: " + ipv4Addr); } finally { if (socket != null) socket.close(); } // Store response time if available if (serviceStatus == 1) { System.out.println("responsetime==" + responseTime); } }