该课内容: Java网络编程. 基于TCP的套接字编程. 基于UDP的套接字编程. URL和URI.一个实用的下载程序.
—计算机网络.
计算机网络是相互连接的独立自主的计算机的集合. 最简单的网络形式由两台计算机组成.
—IP地址.
—协议.
—ISO/OSI七层参考模型.
—OSI各层所使用的协议.
a) TCP:面向连接的可靠的传输协议。
b) UDP:是无连接的,不可靠的传输协议。
—数据封装.
—TCP/IP模型.
TCP/IP包括: 应用层. 传输层. 网络层. 网络接口.
—端口.
—基于TCP的socket编程.
①调用ServerSocket(int port)创建一个服务器端套接字,并绑定到指定端口上;②调用accept(),监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字。③调用Socket类的getOutputStream()和getInputStream获取输出流和输入流,开始网络数据的发送和接收。④最后关闭通信套接字。
①调用Socket()创建一个流套接字,并连接到服务器端; ②调用Socket类的getOutputStream()和getInputStream获取输出流和输入流,开始网络数据的发送和接收。 ③最后关闭通信套接字。
EX. 基于TCP的Socket编程
package test; import java.net.*; import java.io.*; public class Test extends Thread{ private Socket s; public Test (Socket s){ this.s = s; } public void run(){ try{ OutputStream os = s.getOutputStream(); InputStream is = s.getInputStream(); os.write("hello.".getBytes()); byte[] buf = new byte[100]; int len = is.read(buf); System.out.println(new String(buf, 0, len)); os.close(); is.close(); s.close(); } catch (Exception ex){ ex.printStackTrace(); } } public static void main(String[] args){ if (args.length > 0) server(); else client(); } public static void server(){ try{ // 创建套接字.绑定到端口号为6000的端口上. ServerSocket ss = new ServerSocket(6000); // 一旦有请求就创建一个线程.进行处理 while (true){ Socket s = ss.accept(); new Test(s).start(); } //ss.close(); } catch(Exception ex){ ex.printStackTrace(); } } public static void client(){ try{ // 客户端的端口需要与服务器端口号相同. // 注意这里ip地址使用的是本机地址. Socket s = new Socket(InetAddress.getByName(null), 6000); OutputStream os = s.getOutputStream(); InputStream is = s.getInputStream(); os.write("hello too.".getBytes()); byte[] buf = new byte[100]; int len = is.read(buf); System.out.println(new String(buf, 0, len)); os.close(); is.close(); s.close(); } catch(Exception ex){ ex.printStackTrace(); } } }
—基于UDP的socket编程.
①调用DatagramSocket(int port)创建一个数据报套接字,并绑定到指定端口上;②调用DatagramPacket(byte[] buf, int length),建立一个字节数组以接收UDP包 。③调用DatagramSocket类的receive(),接收UDP包。④最后关闭数据报套接字。
①用DatagramSocket()创建一个数据报套接字; ②调用DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port),建立要发送的UDP包。 ③调用DatagramSocket类的send(),发送UDP包。④最后关闭数据报套接字。
EX. 基于UDP的Socket编程
package test; import java.net.*; import java.io.*; public class Test extends Thread{ public static void main(String[] args){ } public static void recv(){ try{ DatagramSocket ds = new DatagramSocket(6000); byte[] buf = new byte[100]; DatagramPacket dp = new DatagramPacket(buf, 100); ds.receive(dp); System.out.println(new String(buf, 0, dp.getLength())); String str = "hello too"; DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str.length(), dp.getAddress(), dp.getPort()); ds.send(dpSend); ds.close(); } catch (Exception ex){ ex.printStackTrace(); } } public static void send(){ try{ DatagramSocket ds = new DatagramSocket(); String str = "Hello"; DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName(null), 6000); ds.send(dp); byte[] buf = new byte[100]; DatagramPacket dpRecv = new DatagramPacket(buf, 100); ds.receive(dpRecv); System.out.println(new String(buf, 0, dpRecv.getLength())); ds.close(); } catch (Exception ex){ ex.printStackTrace(); } } }
—URL与URI.
EX. 文件下载例子.
package test; import java.net.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class Test extends Thread{ public static void main(String[] args){ JFrame jf = new JFrame("download"); jf.setSize(600, 400); jf.setLocation(100, 100); JPanel p = new JPanel(); JLabel l = new JLabel("input URL:"); final JTextField tf = new JTextField(30); p.add(l); p.add(tf); jf.getContentPane().add(p, "North"); final JTextArea ta = new JTextArea(); jf.getContentPane().add(ta, "Center"); JButton btn = new JButton("download"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String str = tf.getText(); try{ URL url1 = new URL(str); URLConnection urlconn = url1.openConnection(); String line = System.getProperty("line.separator"); ta.append("Host " + url1.getHost() + line); ta.append("Port " + url1.getDefaultPort() + line); ta.append("ContentType " + urlconn.getContentType() + line); ta.append("ContextLength " + urlconn.getContentLength()); InputStream is = urlconn.getInputStream(); FileOutputStream fos = new FileOutputStream("1.html"); int data; while ((data = is.read()) != -1){ fos.write(data); } fos.close(); is.close(); } catch (Exception ex){ ex.printStackTrace(); } } }); jf.getContentPane().add(btn, "South"); jf.addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent e){ System.exit(0); } }); jf.show(); } }
Java无难事(笔记)-Lesson10-网络编程,布布扣,bubuko.com
原文:http://www.cnblogs.com/breakingbad/p/3614825.html