Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import static java.lang.System.out;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- /**
- *
- * @author Naik
- */
- public class Server {
- public static void main(String... params) {
- checkServer();
- }
- public static void checkServer() {
- int timeout = 300;
- int port = 21;
- InetSocketAddress addr = new InetSocketAddress("193.189.127.107", port);
- Socket sock = new Socket();
- try {
- sock.connect(addr, timeout);
- //out.println(readFromSocket(sock));
- //out.println("OK");
- out.println(readSocket(sock));
- out.println("OK");
- sock.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- private static String readFromSocket(Socket sock) throws Exception {
- InputStream in = sock.getInputStream();
- StringBuilder result = new StringBuilder(" ");
- int timeout = 0;
- int a;
- while (true) {
- a = in.available();
- if (a > 0) {
- int r = in.read();
- if (r == -1) {
- break;
- }
- result.append((char) r);
- timeout = 0;
- } else {
- Thread.sleep(10);
- a = in.available();
- if (a == 0) {
- timeout++;
- }
- if (timeout > 40) {
- break; // timeout
- }
- }
- }
- return result.toString();
- }
- private static String readSocket(Socket sock) throws Exception {
- InputStream in = sock.getInputStream();
- StringBuilder result = new StringBuilder(" ");
- int r;
- while ((r = in.read()) != -1) {
- result.append((char) r);
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement