1. Ip地址,域名和端口号
JDK的java.net包中定义了与IP地址/域名有关的类
1.java.net.InetAddress
32位或128位无符号数字表示的IP地址。
2.java.net.Inet4Address
继承了InetAddress类,以32位无符号数字表示的IPv4地址。
3.java.net.Inet6Address
集成了InetAddress类,以128为无符号数字表示的IPv6地址。
public class TestInetAddress { public static void main(String args[]){ try { InetAddress ia = InetAddress.getLocalHost(); showInfo(ia); ia = InetAddress.getByName("www.sina.com"); showInfo(ia); } catch (UnknownHostException e) { e.printStackTrace(); } } public static void showInfo(InetAddress ia){ String name = ia.getHostName(); String address = ia.getHostAddress(); print("name:" + name); print("address:" + address); } public static void print(Object o){ System.out.println(o); } }
运行结果:
name:QQ-PC address:60.166.219.14 name:www.sina.com address:61.172.201.195
2. URL
URL(Uniform Resource Locator,统一资源定位器)用于表示Internet上资源的地址。
URL格式:<协议名><资源所在主机名>[:<端口号>]<资源名>
http://home.netscape.com/home/welcome.html
java.net包定义了对应的URL类,常用方法如下:
public URL(String spec)
public final InputStream openStream() throws IOException
public class URLReader { public static void main(String args[]){ URL url; try { url = new URL("http://www.baidu.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String s = reader.readLine(); while(null != s){ System.out.println(s); s = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } } }
运行结果:
<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>百度一下,你就知道
3 Socket编程
两个进程间可以通过一个双向的网络通信连接实现数据交换,这种通信链路的端点被称为“套接字”(Socket) 。
Socket通常用来实现Client-Server连接。
建立连接时所需的寻址信息
1.远程计算机的机器名或IP地址
2.试图连接的端口号(Port number)
java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接的client和server端。在Socket和ServerSocket类常用的构造方法如下:
Socket(InetAddress address,int port) throws IOException;
Socket(String host,int port) throws IOException
ServerSocket(int port) throws IOException
ServerSocket(int port,int backlog) throws IOException
Socket网络编程可分为四个基本步骤:
1. 建立网络连接。
2. 打开连接到Socket
3. 通过打开的I/O流进行数据读/写操作
4. 关闭已打开的I/O流和Socket
Server:
public class TestServer { public static void main(String args[]){ ServerSocket server; try { server = new ServerSocket(8888); System.out.println("Server is running!"); while(true){ Socket s1 = server.accept();//如果没有客户端进来,将会处于阻塞状态 OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("Hi,Client Address is:" + s1.getInetAddress() + "\tClient port:" + s1.getPort()); dos.writeUTF("byte!"); dos.close(); s1.close(); } } catch (IOException e) { e.printStackTrace(); } } }
Client:
public class TestClient { public static void main(String args[]) { Socket client; try { client = new Socket("127.0.0.1", 8888); InputStream is = client.getInputStream(); DataInputStream dis = new DataInputStream(is); print(dis.readUTF()); print(dis.readUTF()); dis.close(); client.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void print(Object o) { System.out.println(o); } }
应该先运行Server,然后在运行Client。
简单的聊天程序 :
Server:
public class TestServerChat { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(8888); Socket s1 = server.accept(); OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String info; while(true){ info = dis.readUTF(); print("Client:" + info); if(info.equals("byte")){ break; } info = reader.readLine(); dos.writeUTF(info); if(info.equals("byte")){ break; } } dis.close(); reader.close(); server.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } public static void print(Object o){ System.out.println(o); } }
Client:
public class TestClientChat { public static void main(String args[]) { Socket client; try { client = new Socket("127.0.0.1", 8888); InputStream is = client.getInputStream(); DataInputStream dis = new DataInputStream(is); OutputStream os = client.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); String info; while (true) { info = reader.readLine(); dos.writeUTF(info); if (info.equals("byte")) { break; } info = dis.readUTF(); print("Server:" + info); if (info.equals("byte")) { break; } } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void print(Object o) { System.out.println(o); } }
此简易聊天程序有很大的局限性,仅可以一人一句。