网络编程使物理上不在一起的主机进行互联,网络连接过程需要使用网络协议,常见的通信协议是TCP,UDP协议.
TCP:属于可靠的连接,使用三方握手的方式完成连接的确认.
UDP:属于不可靠的连接.
对于网络的开发有两种架构:C/S和B/S.
网络开发包所在的类都在java.net开发包中.此包中可以使用ServerSocket,Socket类完成服务器和客户端的开发.开发TCP程序,首先开发服务器端,使用ServerSocket进行客户端的连接接收,每个客户端在程序上都使用Socket对象表示.
Server代码:
package com.ares.demo.helloserver; import java.io.OutputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class HelloServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); // 在8888端口上开启服务 Socket client = null; // 表示连接的客户端 System.out.println("等待客户端连接... ..."); client = server.accept(); // 接受客户端的连接 OutputStream out = client.getOutputStream(); // 得到客户端的输出流 PrintStream pout = new PrintStream(out); pout.println("hello world!!!"); pout.close(); out.close(); client.close(); server.close(); } }
Client代码示例:
package com.ares.demo.helloserver; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; public class HelloClient { public static void main(String[] args) throws Exception { Socket client = new Socket("localhost", 8888);// 表示连接的主机及端口 BufferedReader buf = null; buf = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = buf.readLine();// 接收回应的内容 System.out.println("内容是:" + str); client.close(); } }
Sever代码示例:
package com.ares.demo.echoserver; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class EchoServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); // 在8888端口上开启服务 Socket client = null; // 表示连接的客户端 boolean flag = true; while (flag) { System.out.println("等待客户端连接... ..."); client = server.accept(); // 接受客户端的连接 BufferedReader buf = new BufferedReader(new InputStreamReader( client.getInputStream())); PrintStream pout = new PrintStream(client.getOutputStream()); // 得到客户端的输出流 boolean temp = true; while (temp) {// 循环接收用户输入的内容并回应 String str = buf.readLine(); if (str == null || "".equals(str)) { temp = false; break; } if ("bye".equals(str)) { temp = false; break; } pout.println("ECHO:" + str); // 回送信息 } pout.close(); client.close(); } server.close(); } }
Client代码示例:
package com.ares.demo.echoserver; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; public class EchoClient { public static void main(String[] args) throws Exception { Socket client = new Socket("localhost", 8888);// 表示连接的主机及端口 BufferedReader input = new BufferedReader(new InputStreamReader( System.in)); BufferedReader buf = null; buf = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintStream out = new PrintStream(client.getOutputStream()); boolean flag = true; while (flag) { System.out.print("请输入要发送的内容:") ; String str = input.readLine();// 接收回应的内容 if (str == null || "".equals(str)) { flag = false; break; } if ("bye".equals(str)) { flag = false; break; } out.println(str) ; System.out.println(buf.readLine()) ; } client.close(); } }
该代码只适合单线程的连接访问.可以进一步优化.
Server代码示例:
①EchoThread代码
package com.ares.demo.echothreadserver; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; public class EchoThread implements Runnable { private Socket client; public EchoThread(Socket client) { this.client = client; } public void run() { try { BufferedReader buf = new BufferedReader(new InputStreamReader( client.getInputStream())); PrintStream pout = new PrintStream(client.getOutputStream()); // 得到客户端的输出流 boolean temp = true; while (temp) {// 循环接收用户输入的内容并回应 String str = buf.readLine(); if (str == null || "".equals(str)) { temp = false; break; } if ("bye".equals(str)) { temp = false; break; } pout.println("ECHO:" + str); // 回送信息 } pout.close(); client.close(); } catch (Exception e) { } } }
② EchoServer代码
package com.ares.demo.echothreadserver; import java.net.ServerSocket; public class EchoServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(8888); // 在8888端口上开启服务 boolean flag = true; while (flag) { System.out.println("等待客户端连接... ..."); new Thread(new EchoThread(server.accept())).start() ; } server.close(); } }注 :此程序即实现了多线程的访问 .
UDP的程序使用数据报的形式出现,需要使用到以下两个类:
数据报的内容: DatagramPacket
发送和接收数据报: DatagramSocket
客户端代码:
package com.ares.demo.udpdemo; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(9000); // 服务器端在9000端口等待接收服务器发来的信息 String str = "hello world!!!"; DatagramPacket pack = new DatagramPacket(str.getBytes(), 0, str .length(), InetAddress.getLocalHost(), 3000);// 开辟1024的空间 //3000端口是服务器的端口 socket.send(pack); } }
服务器端代码:
package com.ares.demo.udpdemo; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(3000); // 客户端在3000端口等待接收服务器发来的信息 DatagramPacket pack = new DatagramPacket(new byte[1024], 1024); // 开辟1024的空间 socket.receive(pack); System.out.println("接收到的内容是:" + new String(pack.getData())) ; } }
20150529
JAVA学习笔记系列
--------------------------------------------
联系方式
--------------------------------------------
Weibo: ARESXIONG
E-Mail: [email protected]
------------------------------------------------