美文网首页
1.2.1 Socket类

1.2.1 Socket类

作者: 七迁屋的馒头 | 来源:发表于2017-05-05 15:22 被阅读0次

    <code>套接字</code>使应用程序可以从网络中读取数据,可以向网络中写入数据。
    不同计算机上的两个应用程序可以通过<code>套接字</code>发送或接收字节流,以此达到相互通信的目的。
    为了从一个应用程序向另一个应用程序发送消息,需要知道另一个应用程序中<code>套接字</code>的IP地址和端口号。
    在Java中,<code>套接字</code>由<code>java.net.Socket</code>表示。

    1. 创建一个<code>套接字</code>即创建<code>Socket</code>类实例
      创建一个<code>套接字</code>,可以使用<code>Socket</code>类中众多构造函数中的一个。其中一个构造函数接收两个参数:主机名和端口号。
      <code>public Socket(java.lang.String host, int port)</code>
      参数host是远程主机的名称或IP地址,参数port是连接远程应用程序的端口号。
    2. 使用<code>Socket</code>实例发送数据
      可以使用<code>Socket</code>实例发送字节流。
      要发送字节流,需要调用<code>Scoket</code>类的<code>getOutputStream()</code>方法获取一个<code>java.io.OutputStream</code>对象。
      要发送文本到远程应用程序,通常需要使用返回<code>OutputStream</code>对象创建一个<code>java.io.PrintWriter</code>对象。
    3. 使用<code>Socket</code>实例接收数据
      要从连接的另一端接收字节流,需要调用<code>Socket</code>类的<code>getInputStream()</code>方法,该方法返回一个<code>java.io.InputStream</code>对象。
    4. 实例
      下面的代码段创建了一个<code>Socket</code>实例,用于与本地HTTP服务器进行通信,发送HTTP请求,接收服务器的响应信息。一下代码创建了一个StringBuffer对象来保存响应信息,并将其输出到控制台上。
      <code>
      public static void main(String[] args) {
      try {
      Socket socket = new Socket("192.168.130.156", 8080);
      OutputStream os = socket.getOutputStream();
      boolean autoflush = true;
      PrintWriter out = new PrintWriter(os, autoflush);
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      //发送http请求到web server
      out.println("GET /XSR/pub/login/login.jsp HTTP/1.1");
      out.println("Host: 192.168.130.156:8080");
      out.println("Connection: Close");
      out.println();
      //接收http响应
      boolean loop = true;
      StringBuilder stb = new StringBuilder();
      while(loop) {
      if(in.ready()) {
      int i = 0;
      while(i!=-1) {
      i = in.read();
      stb.append((char)i);
      }
      loop = false;
      }
      try {
      Thread.currentThread().sleep(50);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      //将服务器响应打印到控制台
      System.out.println(stb.toString());
      socket.close();
      } catch (UnknownHostException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      </code>
      运行结果:
      HTTP/1.1 200 OK
      Server: Apache-Coyote/1.1
      Set-Cookie: JSESSIONID=78EF643F15E2600F710AD471ACE7E4FF; Path=/XSR
      Content-Type: text/html;charset=utf-8
      Date: Fri, 05 May 2017 07:29:32 GMT
      Connection: close

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="/XSR/pub/login/images/favicon.ico" type="image/x-icon" />
    <title>登录</title>
    <link href="css.css" rel="stylesheet" type="text/css" />
    .............
    <code>响应实体是整个页面的html文件</code>

    相关文章

      网友评论

          本文标题:1.2.1 Socket类

          本文链接:https://www.haomeiwen.com/subject/roxstxtx.html