java socket 浏览器_java Socket Tcp 浏览器和服务器(二)

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("欢迎光临");

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值