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()+'\n';
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+'\n');
      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..,\n please enter the message:");
     DataOutputStream outToServer=new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    sentence=inFromUser.readLine();
    outToServer.writeBytes(sentence+'\n');
    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.

posted @ 2013-08-15 00:10  丸子No1  阅读(320)  评论(0编辑  收藏  举报