黑马程序员–java 网络处理

URL的全名叫做对统一资源定位符(Uniform Resource Locator)的抽象,其实就好像现在中的身份证号一样的,可以通过这一串字符找到自己想要去找的地方;

URL url=new URL(www.baidu.com);创建一个百度的URL

Internet是用于获取主机信息

有两个实例方法

getHostName()用来获取InetAddress对象所含的域名。

gethostAddress()获取InetAddress对象的IP地址

而Socket是提供网络的交流,当服务器执行accept来进行等待直到收到客户端的请求,然后请求收到两个就建立一条通信的“线路”。

getInputStream:获得一个输入流

getOutputStream获得一个输出流

accept:进入等待状态直到客户端发出请求

下面的程序是实现一个简单的交互过程,有两个类一个是server类另一个是client类,首先服务端等待,然后客户端发送一个数,服务端返回这个数*2的值

clip_image002

Server类:

import java.io.*;

import java.net.*;

public class Server

{

public static void main(String args[])

{

ServerSocket server=null;

Socket you=null;

String s=null;

DataOutputStream out=null;

DataInputStream in=null;

try{server=new ServerSocket(4331);}

catch(IOException e1)

{

System.out.println(e1);

}

try{

System.out.println("等待客户呼叫");

you=server.accept();

out=new DataOutputStream(you.getOutputStream());

in=new DataInputStream(you.getInputStream());

while(true)

{

s=in.readUTF();

int m=Integer.parseInt(s);

out.writeUTF("您好:我是服务器");

out.writeUTF("您说的数乘2后是:"+2*m);

System.out.println("服务器收到:"+s);

Thread.sleep(500);

}

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Client类:

import java.io.*;

import java.net.*;

public class Client

{

public static void main(String args[])

{

String s=null;

Socket mysocket;

DataInputStream in=null;

DataOutputStream out=null;

try{

mysocket=new Socket("127.0.0.1",4331);

in=new DataInputStream(mysocket.getInputStream());

out=new DataOutputStream(mysocket.getOutputStream());

for(int k=1;k<100;k=k+2)

{

out.writeUTF(""+k);

s=in.readUTF();

System.out.println("客户收到:"+s);

Thread.sleep(500);

}

}

catch(Exception e)

{

System.out.println("服务器已断开"+e);

}

}

}

//显示网络的文档内容

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

import javax.swing.JEditorPane;

public class Example11_2

{

public static void main(String args[])

{

new Win();

}

}

class Win extends Frame implements ActionListener,Runnable

{

Button button;

URL url;

TextField text;

JEditorPane editpane;

byte b[]=new byte[118];

Thread thread;

public Win()

{

text=new TextField(20);

editpane=new JEditorPane();

editpane.setEditable(false);

button=new Button("确定");

button.addActionListener(this);

thread=new Thread(this);

Panel p=new Panel();

p.add(new Label("输入网址:"));

p.add(text);

p.add(button);

ScrollPane scroll=new ScrollPane();

scroll.add(editpane);

add(scroll,BorderLayout.CENTER);

add(p,BorderLayout.NORTH);

setBounds(160,60,360,300);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)

{

if(!(thread.isAlive()))

{

thread=new Thread(this);

try{thread.start();}

catch(Exception ee)

{text.setText("我正在读取"+url);

}

}

}

public void run()

{

try{

editpane.setText(null);

url=new URL(text.getText().trim());

editpane.setPage(url);

}

catch(Exception e1)

{

text.setText(""+e1);

return;

}

}

}

史上最差的模拟网页浏览器代码

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

import javax.swing.event.*;

import javax.swing.*;

public class Example11_3

{

public static void main(String args[])

{

new LinkWin();

}

}

class LinkWin extends JFrame implements ActionListener,Runnable

{

Button button;

URL url;

TextField text;

JEditorPane editpane;

byte b[]=new byte[118];

Thread thread;

public LinkWin()

{

text=new TextField(20);

editpane=new JEditorPane();

editpane.setEditable(false);

button=new Button("确定");

button.addActionListener(this);

thread=new Thread(this);

Panel p=new Panel();

p.add(new Label("输入网址"));

p.add(text);

p.add(button);

ScrollPane scroll=new ScrollPane();

scroll.add(editpane);

add(scroll,BorderLayout.CENTER);

add(p,BorderLayout.NORTH);

setBounds(60,60,360,300);

setVisible(true);

validate();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

editpane.addHyperlinkListener(new HyperlinkListener()

{

public void hyperlinkUpdate(HyperlinkEvent e)

{

if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)

{

try{

editpane.setPage(e.getURL());

}catch(IOException e1)

{

editpane.setText(""+e1);

}

}

}

});

}

public void actionPerformed(ActionEvent e)

{

if(!(thread.isAlive()))

thread=new Thread(this);

try{thread.start();}catch(Exception ee){text.setText("我正在读取"+url);}

}

public void run()

{

try{

editpane.setText(null);

url=new URL(text.getText().trim());

editpane.setPage(url);

}catch(Exception e1)

{

text.setText(""+e1);

return;

}

}

}

posted @ 2012-02-20 18:18  夜风雪  阅读(416)  评论(0编辑  收藏  举报