标签:png writer image 构造 sys 主机 网络编程 网络 equal
ipconfig 查看本机IP地址
ping IP地址 检查网络是否连通
特殊IP地址:127.0.0.1:是回送地址,可以表示本机地址,一般用来测试使用
// InetAddress inetAddress = InetAddress.getByName("DESKTOP-2PVNKVI"); InetAddress inetAddress = InetAddress.getByName("192.168.0.111"); String hostAddress = inetAddress.getHostAddress(); String hostName = inetAddress.getHostName(); System.out.println(hostAddress+":"+hostName);
public static void main(String[] args) throws IOException { // 1.创建发送端Socket对象DatagramSocket DatagramSocket datagramSocket = new DatagramSocket(); // 2.创建数据,并打包数据 // DatagramPacket?(byte[] buf, int length, InetAddress address, int port) : // 构造一个数据包,发送长度为 length的数据包到指定主机上的指定端口号。 byte[] bytes = "hello,UDP,I`M COMMING!!!".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); // 3.发送数据 datagramSocket.send(datagramPacket); // 4.关闭发送端 datagramSocket.close(); }
public static void main(String[] args) throws IOException { // 1.创建接收端的Socket对象,DataGramSocket // DatagramSocket?(int port) 构造数据报套接字并将其绑定到本地主机上的指定端口。 DatagramSocket datagramSocket = new DatagramSocket(8088); // 2.创建一个数据包用来接收数据 // DatagramPacket?(byte[] buf, int length) 构造一个 DatagramPacket用于接收长度为 length数据 byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length); // 3.接收数据 datagramSocket.receive(datagramPacket); // 4.解析数据,加工数据,使数据为实际数据长度 byte[] data = datagramPacket.getData(); String s = new String(data, 0, datgramPacket.getLength); // 5.展示数据 System.out.println("接收到数据:"+s); // 6.关闭接收端 datagramSocket.close(); }
public class PracticeSend { public static void main(String[] args) throws IOException { BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line=bufferedreader.readLine())!=null){ // 输入数据是886,终止发送 if (line.equals("886")){ break; } send(line); } } private static void send(String s) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(); byte[] bytes = s.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); datagramSocket.send(datagramPacket); datagramSocket.close(); } }
public class PracticeRecive { public static void main(String[] args) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(8088); while (true) {//因为不知道发送端什么时候停止发送,所以一直接收 recive(datagramSocket); } } private static void recive(DatagramSocket datagramSocket) throws IOException { byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); datagramSocket.receive(datagramPacket); byte[] data = datagramPacket.getData(); System.out.println("接收到数据:" + new String(data, 0, datagramPacket.getLength())); } }
public static void main(String[] args) throws IOException { // 创建客户端Socket对象(Socket) // Socket?(InetAddress address, int port) 创建流套接字并将其连接到指定IP地址的指定端口号。 // Socket socket = new Socket(InetAddress.getByName("192.168.0.111"),8099); // Socket?(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号。 Socket st = new Socket("192.168.0.111", 8099); // 2.获取输出流写数据 OutputStream outputStream = st.getOutputStream(); outputStream.write("^_^,你好TCP!!!".getBytes()); // 释放资源 st.close(); }
//1.创建服务器端Socket对象(ServerSocket) //ServerSocket?(int port) 创建绑定到指定端口的服务器套接字。 ServerSocket serverSocket = new ServerSocket(8099); // 2.监听客户连接并返回一个Socket对象:Socket accept?() 侦听要连接到此套接字并接受它。 Socket socket = serverSocket.accept(); // 3.获取输入流并读取数据进行展示 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int length=inputStream.read(bytes); String s = new String(bytes, 0, length); System.out.println("数据是:"+s); // 4.释放资源 inputStream.close(); serverSocket.close(); }
public class Client2 { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.0.111", 8099); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line=bufferedReader.readLine())!=null){ if ("886".equals(line)){ break; } bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.newLine(); } socket.close(); } }
public class Server2 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8099); Socket socket = serverSocket.accept(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } serverSocket.close(); } }
标签:png writer image 构造 sys 主机 网络编程 网络 equal
原文地址:https://www.cnblogs.com/luzhanshi/p/13196431.html