zoukankan      html  css  js  c++  java
  • 客户端socket调用

     1 import java.net.Socket;
     2 import java.io.*;
     3 import java.util.Scanner;
     4 import java.util.regex.Pattern;
     5 import java.util.regex.Matcher;
     6 
     7 class SocketTest
     8 {
     9     public static void main(String[] args) 
    10     {        
    11         Scanner sc=new Scanner(System.in);
    12         System.out.println("please input ipaddress:");
    13         boolean isIp=false;
    14         String strIp="";
    15         //ip正则匹配
    16         Pattern pattern=Pattern.compile("((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))");
    17         while(!isIp){
    18         //System.out.println(isIp);    
    19         strIp=sc.next();        
    20         Matcher matcher=pattern.matcher(strIp);
    21         //如果输入的数据满足ip格式,继续运行
    22         if(matcher.matches()){
    23             isIp=true;
    24             }
    25             else{
    26                 System.out.println("the ip is wrong,please input again:");
    27             }
    28         }            
    29         int port=80;
    30         boolean isNu=false;
    31         //如果输入的为正确的端口号(整数)继续运行
    32         while(!isNu){
    33         try{
    34             System.out.println("please input port");
    35             String strPort=sc.next();
    36             port=Integer.parseInt(strPort);
    37             isNu=true;
    38             }catch(Exception ex){
    39                 System.out.println("is not int ,please input agein:");
    40             }
    41         }
    42 
    43         System.out.println("the ip is :"+strIp);
    44         System.out.println("the port is :"+port);
    45         if(strIp!=null&&strIp.length()>0){
    46             try{
    47                 //通过ip和端口创建socket实例
    48                 Socket socket=new Socket(strIp,port);
    49                 //获取socket的输出流
    50                 OutputStream  outputStream=socket.getOutputStream();
    51                 //讲http请求头信息输入至socket输出流中
    52                 PrintWriter out=new PrintWriter(outputStream,true);
    53                 out.println("GET / HTTP/1.1");  //get 请求,获取目录为/ http协议为http 1.1
    54                 out.println("Host: localhost:8888");
    55                 out.println("Connection: Close");
    56                 out.println();
    57                 //接收服务端返回的数据流
    58                 BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    59                 boolean loop=true;
    60                 StringBuffer sb=new StringBuffer(8096);
    61                 while(loop){
    62                     if ( in.ready() ) {
    63                         int i=0;
    64                         while (i!=-1) {
    65                             i = in.read();
    66                             sb.append((char) i);
    67                         }
    68                     loop = false;
    69                     Thread.currentThread().sleep(50);
    70                     }
    71                 }
    72                 System.out.println(sb.toString());
    73                 socket.close();
    74             }
    75                 catch (Exception ex) {
    76                     ex.printStackTrace();
    77                     System.out.println(ex.toString());
    78             }
    79         }
    80     }
    81 }
    View Code
  • 相关阅读:
    爬虫助手spider_tool-JUN
    frida 保存打印日志到本地
    frida get_frontmost_application报错
    adb shell安装证书/修改证书到系统级/
    利用celery进行分布式爬虫
    vscode Go插件安装失败解决方法,亲测2020.10.15
    Frida hook map集合遍历和修改
    frida_rpc dou音、饿le么 header加密
    Frida入门
    adb连接模拟器
  • 原文地址:https://www.cnblogs.com/kurt/p/4269840.html
Copyright © 2011-2022 走看看