Java提供的通信方式
Socket——面向连接
使用TCP协议,TCP是一种面向连接保证可靠传输的协议。通过TCP协议传输,得到的是一个顺序的无差错的数据流。Java提供的Socket和ServerSocket类支持TCP协议。在发送数据前,发送方和接收方各有一个Socket,它们要建立连接,一旦这两个Socket建立了连接,双方就可以进行双向数据传输,并且所有发送的信息都会在另一端以同样的顺序被接收,安全性高。
服务器程序编写
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class msa { public static void main(String[] args) { try { //1.创建ServerSocket对象,指明提供服务的端口4321 ServerSocket ss = new ServerSocket(4321); System.out.println("Server OK"); while(true) { //2.等待客户连接 Socket s = ss.accept(); //3.生成输入输出流 InputStreamReader ins = new InputStreamReader(s.getInputStream()); BufferedReader in = new BufferedReader(ins); //4.处理输入输出操作 String x = in.readLine(); System.out.println("Information from client:"+x); PrintStream out = new PrintStream(s.getOutputStream()); out.println("hello client"); //5.关闭输入输出流 in.close(); out.close(); s.close(); } }catch(IOException e) {} } }
客户端程序编写
public class mca { public static void main(String[] args) { try { //1.创建Socket,本例中IP地址为本地地址127.0.0.1,端口为4321 Socket s = new Socket("127.0.0.1",4321); //2.生成输入输出流 PrintStream out = new PrintStream(s.getOutputStream()); InputStreamReader ins = new InputStreamReader(s.getInputStream()); BufferedReader in = new BufferedReader(ins); //处理输入输出 String c = "hello server"; out.println(c); String x = in.readLine(); System.out.println("Information from server:"+x); //4.关闭输入输出流 out.close(); in.close(); s.close(); }catch(IOException e){ } } }
Datagram——无连接
使用UDP协议,UDP是一种无连接的协议,一个数据报是一个独立的单元,包含目的地址和要发送的数据,无需建立连接,只是简单地投出数据报,Java提供的DatagramSocket和DatagramPacket类支持UDP协议。效率高,但安全性不佳。
发送方程序编写
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; public class udps { public static void main(String[] args) { //1.创建发送方套接字 DatagramSocket ds = null; //2.构造发送方数据包:数据,数据长度,目的地址,端口号 DatagramPacket dp = null; try { ds = new DatagramSocket(300); }catch(SocketException e) { } String str = "Hello World"; try { dp = new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("localhost"),9000); } catch (UnknownHostException e) { } //3.发送 try { ds.send(dp); }catch(IOException e) {} //4.关闭发送方套接字 ds.close(); } }
接收方程序编写
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class udprec { public static void main(String[] args) { //1.创建接收方套接字 DatagramSocket ds = null; //2.构造接收数据报包 byte[] buf = new byte[1024]; DatagramPacket dp = null; try { ds = new DatagramSocket(9000); }catch(SocketException e) {} dp = new DatagramPacket(buf,1024); //3.接收数据报包 try { ds.receive(dp); }catch(IOException e) {} String str = new String(dp.getData(),0,dp.getLength())+"from"+dp.getAddress().getHostAddress()+":"+dp.getPort(); System.out.println(str+" length="+dp.getLength()); //4.关闭接收方套接字 ds.close(); } }
编写简单的Web服务器
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class msa { public static void main(String[] args) { try { //1.创建ServerSocket对象,指明提供服务的端口4321 ServerSocket ss = new ServerSocket(4321); System.out.println("Server OK"); while(true) { //2.等待客户连接 Socket s = ss.accept(); //3.生成输入输出流 InputStreamReader ins = new InputStreamReader(s.getInputStream()); BufferedReader in = new BufferedReader(ins); //4.处理输入输出操作 String x = in.readLine(); System.out.println("Information from client:"+x); PrintStream out = new PrintStream(s.getOutputStream()); out.println("hello client"); //5.关闭输入输出流 in.close(); out.close(); s.close(); } }catch(IOException e) {} } }
可以传输文件的Web服务器
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class webs1 { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(80); System.out.println("web server ok"); while(true) { Socket s = ss.accept(); vebserv1 p = new vebserv1(s); Thread t = new Thread(p); t.start(); } }catch(Exception e) { System.out.println(e); } } } class vebserv1 implements Runnable{ Socket s; static int i; public vebserv1(Socket s1) { s = s1; } public void run() { try { PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String info = in.readLine(); System.out.println("now got"+info); out.println("HTTP/1.0"); out.println("Content-type: text/html"); out.println(""); int sp1 = info.indexOf(‘ ‘); int sp2 = info.indexOf(‘ ‘,sp1+1); String fn = info.substring(sp1+2,sp2); if(fn.equals("")||fn.endsWith("/")) fn = fn+"index.html"; System.out.println("sending file: "+fn+" to client"); InputStream fs = new FileInputStream(fn); byte buf[] = new byte[1024]; int n; while((n=fs.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); in.close(); }catch(Exception e) {System.out.println(e);} } }
编写简单的代理服务器
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.net.URL; public class msp { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(80); System.out.println("web server ok"); while(true) { Socket s = ss.accept(); mspserv p = new mspserv(s); Thread t = new Thread(p); t.start(); } }catch(Exception e) { System.out.println(e); } } } class mspserv implements Runnable{ Socket s; static int i; public mspserv(Socket s1) { s =s1; } public void run() { try { PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String info = in.readLine(); System.out.println("client is: "+s.getInetAddress()); System.out.println("now got "+info); int sp1 = info.indexOf(‘ ‘); int sp2 = info.indexOf(‘ ‘,sp1+1); String targ = info.substring(sp1+1,sp2); System.out.println("now connecting "+targ); URL con = new URL(targ); InputStream gotoin = con.openStream(); int n; byte buf[] = new byte[1024]; out.println("HTTP/1.0"); out.println("Content - type: text/html"); out.println(""); while((n = gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); in.close(); }catch(Exception e) {System.out.println(e);} } }
原文地址:https://www.cnblogs.com/heibaimao123/p/9527138.html
时间: 2024-09-29 09:19:35