可分配端口号:0~65535,一般情况下,0~1024为系统使用端口或保留端口。
用于UDP传输的类
主要方法:1.public DatagramSocket() throws SocketException;//创建一个Scoket对象,系统分配默认的端口号,该方法主要用于接收数据;
2.public DatagramSocket?(int port) throws SocketException;//创建一个指定端口的Scoket对象,该方法主要用于发送数据;
3.public void send?(DatagramPacket p) throws IOException;//将数据包发送出去;
4.public void receive?(DatagramPacket p) throws IOException;//接收相应端口监视的数据包。
简单的UDP通信代码
package udptest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPChart { public static void main(String[] args) { // 发送端端口无所谓,只需要发送给相应IP地址的相应端口就行, Send s = new Send(10001, "192.168.2.158"); // 监听相应的端口,测试时需要运行两个该程序,只需要将10001和10002两个端口号互换即可通信。 Rece r = new Rece(10002); new Thread(s).start(); new Thread(r).start(); } } class Send implements Runnable { // 发送端地址 private String exIPAddress; // 发送端口号 private int port; private DatagramSocket datagramSocket = null; private BufferedReader bufr = null; Send(int port, String exIPAddress) { this.exIPAddress = exIPAddress; this.port = port; init(); } private void init() { try { datagramSocket = new DatagramSocket(); } catch (SocketException ex) { datagramSocket.close(); throw new RuntimeException("UDP Socket 创建失败"); } bufr = new BufferedReader(new InputStreamReader(System.in)); } @Override public void run() { try { String line = null; while ((line = bufr.readLine()) != null) { byte[] buf = line.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName(exIPAddress), port); datagramSocket.send(datagramPacket); if ("886".equals(line)) break; } } catch (IOException ex) { throw new RuntimeException("发送错误"); }finally { datagramSocket.close(); } } } class Rece implements Runnable { // 接收端口号 private int port; private DatagramSocket datagramSocket = null; Rece(int port) { this.port = port; init(); } private void init() { try { datagramSocket = new DatagramSocket(port); } catch (SocketException ex) { datagramSocket.close(); throw new RuntimeException("UDP Socket 创建失败"); } } @Override public void run() { while(true) { // 创建数据包,将收到的数据存入数据包中 byte[] buf = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length); try { datagramSocket.receive(datagramPacket); } catch (IOException ex) { throw new RuntimeException("UDP接收失败"); } String line = new String(datagramPacket.getData(),0, datagramPacket.getLength()); if("886".equals(line)) break; System.out.println(datagramPacket.getAddress() + line); } datagramSocket.close(); } }
Socket(Package java.net)
TCP传输分客户端和服务端,客户端对应的是Socket,服务端对应的是ServerSocket。
主要方法:1.构造方法:public Socket?(String host, int port) throws UnknownHostException, IOException;//客户端一初始化就要绑定地址和端口号;
2.public InputStream getInputStream() throws IOException;//获取该Socket的输入流,即服务器传给客户端的数据流;
3.public OutputStream getOutputStream() throws IOException;//获取该Socket的输出流,即客户端发送给服务器的数据流;
主要方法:1.构造方法:public ServerSocket?(int port) throws IOException;//创建该对象要监听端口的服务流;
2.public Socket accept() throws IOException;//获取与之连接对应的Socket对象;然后就可以使用Socket流中的方法;
练习:将文本数据上传到服务器
package tcptest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * 需求:将客户端传入的.MP3文件写到client.mp3文件中 * */ public class Server { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub ServerSocket serverSocket = new ServerSocket(10004); System.out.println("Server open"); int num = 0; while (true) { Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); File file = new File("client" + (num++) + ".mp3"); while(file.exists()) { file = new File("client" + (num++) + ".mp3"); } FileOutputStream fos = new FileOutputStream(file); write2File(inputStream, fos); OutputStream outputStream = socket.getOutputStream(); write2Client(outputStream); fos.close(); } } private static void write2Client(OutputStream outputStream) throws IOException { outputStream.write("客户端,以接收完成".getBytes()); } /** * * @param inputStream:Socket输入流 * @param fos:文件输出流 * @throws IOException */ private static void write2File(InputStream inputStream, FileOutputStream fos) throws IOException { byte[] buf = new byte[1024 * 5]; int len = 0; while ((len = inputStream.read(buf)) != -1) { fos.write(buf, 0, len); fos.flush(); } } }
package tcptest; import java.io.FileInputStream; /** * 需求:将客户端.MP3文件发送到服务端。 * 步骤: * 1.创建Socket流 * 2.打开指定文件,将文件读入字节缓冲区,将字节缓冲区的文件传入输出流; * 2.读取Socket流,从服务端获取结果。 * */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = new Socket("192.168.2.158", 10004); System.out.println("client open"); OutputStream outputStream = socket.getOutputStream(); writeFile2Server(outputStream); // 输出结束标志 socket.shutdownOutput(); InputStream inputStream = socket.getInputStream(); getMessFromServer(inputStream); socket.close(); } /** * * @param inputStream:Socket输入流 * @throws IOException */ public static void getMessFromServer(InputStream inputStream) throws IOException { byte[] buf = new byte[1024]; int len = inputStream.read(buf); System.out.println(new String(buf,0,len)); } /** * * @param outputStream:文件的输出流 * @throws IOException */ public static void writeFile2Server(OutputStream outputStream) throws IOException { byte[] buf = new byte[1024*5]; FileInputStream fis = new FileInputStream("test.mp3"); int len = 0; while((len = fis.read(buf)) != -1) { outputStream.write(buf, 0, len); outputStream.flush(); } fis.close(); } }
原文:https://www.cnblogs.com/star-491849224/p/12534823.html