zoukankan      html  css  js  c++  java
  • hacker client and normal client in java

        Write two servers, one uses 1034 port, another server uses 1035, write a normal client which establish connection with 1034 server and receive a message. Another hacker client program which tells which port is open.(port scanner)

     1 import java.io.*;
     2 import java.net.*;
     3 class tcpserver{
     4   public static void main(String args[])throws Exception
     5   {
     6     System.out.println("hello");
     7     String clientSentence;
     8     String capitalizedSentence;
     9     int port = Integer.parseInt(args[0]);
    10     ServerSocket welcomeSocket=new ServerSocket(port);
    11 
    12     while(true){
    13        Socket connectionSocket=welcomeSocket.accept();
    14        BufferedReader inFromClient=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
    15        DataOutputStream outToClient=new DataOutputStream(connectionSocket.getOutputStream());
    16        clientSentence=inFromClient.readLine();
    17        System.out.println("Received: "+clientSentence);
    18        capitalizedSentence=clientSentence.toUpperCase()+'
    ';
    19        outToClient.writeBytes(capitalizedSentence); 
    20     }
    21 
    22   }
    23 }
    import java.io.*;
    import java.net.*;
    public class normalclient{
      public static void main(String args[]){
        int port=Integer.parseInt(args[0]);
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket;
        try{
          clientSocket=new Socket("localhost",port);
          DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          sentence=inFromUser.readLine();
          outToServer.writeBytes(sentence+'
    ');
          modifiedSentence=inFromServer.readLine();
           System.out.println("FROM SERVER:"+modifiedSentence);
            clientSocket.close();
           }catch(Exception e){
              System.out.println("port "+port+" is closed...");
           }
      }
    }
    normalclient
    import java.io.*;
    import java.net.*;
    
    class hackclient{
      public static void main(String args[])throws Exception
      {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket;
        for(int i=1034;i<1039;i++){
           try{
             clientSocket=new Socket("localhost",i);
             System.out.println("port "+i+" is open..,
     please enter the message:");
         DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence=inFromUser.readLine();
        outToServer.writeBytes(sentence+'
    ');
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER:"+modifiedSentence);
            clientSocket.close();
           }catch(Exception e){
              System.out.println("port "+i+" is closed...");
           }
        
        } 
      }
    }
    hackerclient

    Using only one server for more than one ports, we provide the port number for the server program and thus we can open several termals.As for the normal client, after sending a message, it returns the capitalized form  of the message. The hacker client will check the port ranging from 1034 till 1038, If the port is open , it will return the same as in the normal client, otherwise, it displays the message, the port is closed.

  • 相关阅读:
    CentOS Linux下VNC Server远程桌面配置详解
    Java 中的悲观锁和乐观锁的实现
    spring @configuration使用
    MySQL 汉字拼音
    chmod用数字来表示权限的方法
    C语言创建删不掉的目录
    Android小经验
    系统清理——查找大文件
    最全Pycharm教程(42)——Pycharm扩展功能之Emacs外部编辑器
    怎样学习程序
  • 原文地址:https://www.cnblogs.com/songwanzi/p/3258745.html
Copyright © 2011-2022 走看看