package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyBrowser {

/**
  * @param args
  * @throws IOException
  * @throws UnknownHostException
  */
 public static void main(String[] args) throws UnknownHostException, IOException {

Socket s = new Socket("192.168.1.100",8080);
  
  //模拟浏览器,给tomcat服务端发送符合http协议的请求消息。
  PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  out.println("GET /myweb/1.html HTTP/1.1");
  out.println("Accept: */*");
  out.println("Host: 192.168.1.100:8080");
  out.println("Connection: close");
  out.println();
  out.println();
  
  
  InputStream in = s.getInputStream();
  
  byte[] buf = new byte[1024];
  int len = in.read(buf);
  
  String str =new String(buf,0,len);
  System.out.println(str);
  
  s.close();
  
  //http://192.168.1.100:8080/myweb/1.html
 }

}

package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyTomcat {

/**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(9090);
  
  Socket s = ss.accept();
  System.out.println(s.getInetAddress().getHostAddress()+".....connected");
  
  InputStream in = s.getInputStream();
  
  byte[] buf = new byte[1024];
  
  int len = in.read(buf);
  String text = new String(buf,0,len);
  System.out.println(text);
  
  
  //给客户端一个反馈信息。
  PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  
  out.println("<font color='red' size='7'>欢迎光临</font>");
  
  s.close();
  ss.close();
 }

}

package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {

/**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {

String str_url = "http://192.168.1.100:8080/myweb/1.html";
  
  URL url = new URL(str_url);
  
//  System.out.println("getProtocol:"+url.getProtocol());
//  System.out.println("getHost:"+url.getHost());
//  System.out.println("getPort:"+url.getPort());
//  System.out.println("getFile:"+url.getFile());
//  System.out.println("getPath:"+url.getPath());
//  System.out.println("getQuery:"+url.getQuery());
  
//  InputStream in = url.openStream();
  
  //获取url对象的Url连接器对象。将连接封装成了对象:java中内置的可以解析的具体协议的对象+socket.
  URLConnection conn = url.openConnection();
  
//  String value = conn.getHeaderField("Content-Type");
//  System.out.println(value);
  
//  System.out.println(conn);
  //sun.net.www.protocol.http.HttpURLConnection:http://192.168.1.100:8080/myweb/1.html
  
  InputStream in = conn.getInputStream();
  
  byte[] buf = new byte[1024];
  int len = in.read(buf);
  
  String text = new String(buf,0,len);
  
  System.out.println(text);
  
  in.close();
  
 }

}

package cn.itcast.net.p3.iegui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MyBrowseGUI extends javax.swing.JFrame {
 private JTextField url_text;
 private JButton goto_but;
 private JScrollPane jScrollPane1;
 private JTextArea page_content;

/**
 * Auto-generated main method to display this JFrame
 */
 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    MyBrowseGUI inst = new MyBrowseGUI();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
   }
  });
 }
 
 public MyBrowseGUI() {
  super();
  initGUI();
 }
 
 private void initGUI() {
  try {
   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   getContentPane().setLayout(null);
   {
    url_text = new JTextField();
    getContentPane().add(url_text);
    url_text.setBounds(12, 36, 531, 44);
    url_text.addKeyListener(new KeyAdapter() {
     public void keyPressed(KeyEvent evt) {
      url_textKeyPressed(evt);
     }
    });
   }
   {
    goto_but = new JButton();
    getContentPane().add(goto_but);
    goto_but.setText("\u8f6c \u5230");
    goto_but.setBounds(555, 36, 134, 44);
    goto_but.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
      goto_butActionPerformed(evt);
     }
    });
   }
   {
    jScrollPane1 = new JScrollPane();
    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(12, 92, 676, 414);
    {
     page_content = new JTextArea();
     jScrollPane1.setViewportView(page_content);
    }
   }
   pack();
   this.setSize(708, 545);
  } catch (Exception e) {
      //add your error handling code here
   e.printStackTrace();
  }
 }
 
 private void goto_butActionPerformed(ActionEvent evt) {
  showPage();
 }
 
 private void url_textKeyPressed(KeyEvent evt) {
  if(evt.getKeyCode()==KeyEvent.VK_ENTER)
   showPage();
  
 }

private void showPage() {
  try {
   
   String url_str = url_text.getText();
   URL url = new URL(url_str);
   
   InputStream in = url.openConnection().getInputStream();//url.openStream();
   
   page_content.setText("");
   
   byte[] buf = new byte[1024];
   int len = in.read(buf);
   String text = new String(buf,0,len,"utf-8");
   
   page_content.setText(text);
   
   in.close();
   
   
  } catch (Exception e) {
   // TODO: handle exception
  }
 }

}

java Socket Tcp 浏览器和服务器(二)的更多相关文章

  1. java Socket Tcp 浏览器和服务器(一)

    自定义服务端,使用已有的客户端IE,了解一下客户端给服务端发了什么请求? 发送的请求是: GET / HTTP/1.1  请求行  请求方式  /myweb/1.html  请求的资源路径   htt ...

  2. JAVA Socket 编程学习笔记(二)

    在上一篇中,使用了 java Socket+Tcp/IP  协议来实现应用程序或客户端--服务器间的实时双向通信,本篇中,将使用 UDP 协议来实现 Socket 的通信. 1. 关于UDP UDP协 ...

  3. 【JAVA网络流之浏览器与服务器模拟】

    一.模拟服务器获取浏览器请求http信息 代码: package p06.TCPTransferImitateServer.p01.ImitateServer; import java.io.IOEx ...

  4. java socket tcp(服务器循环检测)

    刚才看了下以前写了的代码,tcp通信,发现太简单了,直接又摘抄了一个,运行 博客:http://blog.csdn.net/zhy_cheng/article/details/7819659 优点是服 ...

  5. java Socket Tcp示例三则(服务端处理数据、上传文件)

    示例一: package cn.itcast.net.p5.tcptest; import java.io.BufferedReader;import java.io.IOException;impo ...

  6. java Socket(TCP)编程小项目

    package 服务器端相关操作; import java.io.Serializable; /* * 创建存储需要传输信息的对象,方便客户端向服务器端传送数据 */ public class Cli ...

  7. Socket TCP客户端和服务器的实现

    import java.io.*; import java.net.Inet4Address; import java.net.InetSocketAddress; import java.net.S ...

  8. Java Socket TCP编程(Server端多线程处理)

    package com; import java.io.*; import java.net.Socket; /** * Socket Client * <p> * Created by ...

  9. Java Socket TCP编程

    package com; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Socket Se ...

随机推荐

  1. 浅谈Android RecyclerView

    Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...

  2. python文本 字符串逐字符反转以及逐单词反转

    python文本 字符串逐字符反转以及逐单词反转 场景: 字符串逐字符反转以及逐单词反转 首先来看字符串逐字符反转,由于python提供了非常有用的切片,所以只需要一句就可以搞定了 >>& ...

  3. .NET:如何实现 “热插拔”?

    背景 如果某个“功能”需要动态更新?这种动态更新,可能是需求驱动的,也可能是为了修改 BUG,面对这种场景,如何实现“热插拔”呢?先解释一下“热插拔”:在系统运行过程动态替换某些功能,不用重启系统进程 ...

  4. Myeclipse反编译插件(jad)的安装和使用

    在开发过程中我们肯定会遇到这样的问题,当我们调试程序的时候,走到一个地方发现引用了一个第三方的东西,点进去一看,会出现一下的画面,没有源代码!!!! 这让人很头疼,今天给大家介绍一个Myeclipse ...

  5. D - I Think I Need a Houseboat(1.3.1)

    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  6. dlib landmark+人面识别

    #include "stdafx.h" #include <dlib/image_processing/frontal_face_detector.h> #includ ...

  7. Linux 系统分析命令图

    sar命令: 编辑 /etc/default/sysstat 设置为true sudo /etc/init.d/sysstat restart

  8. [Git] 谷歌的代码管理

    copy from : http://www.ruanyifeng.com/blog/2016/07/google-monolithic-source-repository.html https:// ...

  9. mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2

    3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...

  10. Spring 远程服务

    稍微看了一下Spring的远程服务章节,讲到了RMI,Hessian,Burlap,Http invoker以及JAX-WS 1.RMI 原理: 1)在Spring服务端使用RmiServiceExp ...