在网络通信协议下。不同计算机上运行的程序,可以进行数据传输
import java.net.InetAddress;
import java.net.UnknownHostException;public class Demo1 {public static void main(String[] args) throws UnknownHostException {InetAddress address=InetAddress.getByName("zfz");String hostName = address.getHostName();System.out.println("主机名为:"+hostName);String hostAddress = address.getHostAddress();System.out.println("Ip为:"+hostAddress);}
}
import java.io.IOException;
import java.net.*;public class ClientDemo {public static void main(String[] args) throws IOException {//1.找码头DatagramSocket ds=new DatagramSocket();//2.打包数据// public DatagramPacket(byte[] buf, int length, InetAddress address, int port)String str="朋友送的 好烟 好酒 好茶";byte[] bytes = str.getBytes();InetAddress address=InetAddress.getByName("127.0.0.1");int port=10000;DatagramPacket dp=new DatagramPacket(bytes,bytes.length,address,port);//3.发送数据ds.send(dp);//发送完成ds.close();}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class ServerDemo {/*** 注意点:* 1. 要先运行接收端 再运行发送端* 2. 如果接收端再启动之后,没有收到数据 ,就会死等(阻塞状态)* 3. 在接受数据时 可以调用getLength方法,表示接收到了多少字节* 4.*/public static void main(String[] args) throws IOException {//1.找码头//表示接受端从10000端口接受数据DatagramSocket ds=new DatagramSocket(10000);//2.创建接受数据的箱子// public DatagramPacket(byte[] buf, int length)byte bytes[]=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes,bytes.length);//3.接受数据并放到箱子中System.out.println("--------接受前--------");ds.receive(dp);System.out.println("--------接受后--------");//4.从箱子中拿出数据//byte [] data=dp.getData();int length=dp.getLength();System.out.println(new String(bytes,0,length));//5.接受完成ds.close();}
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;public class ClientDemo {public static void main(String[] args) throws IOException {Scanner sc=new Scanner(System.in);DatagramSocket ds=new DatagramSocket();while (true) {String str=sc.nextLine();if (str.equals("886")){break;}byte[] bytes = str.getBytes();InetAddress address=InetAddress.getByName("127.0.0.1");int port=10000;DatagramPacket dp=new DatagramPacket(bytes,bytes.length,address,port);ds.send(dp);}ds.close();}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class ServerDemo {public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket(10000);while (true) {byte bytes[]=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes,bytes.length);ds.receive(dp);int length=dp.getLength();System.out.println(new String(bytes,0,length));}}
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;public class ClientDemo {public static void main(String[] args) throws IOException {Scanner sc=new Scanner(System.in);DatagramSocket ds=new DatagramSocket();while (true) {String str=sc.nextLine();if (str.equals("886")){break;}byte[] bytes = str.getBytes();InetAddress address=InetAddress.getByName("127.0.0.1");int port=10000;DatagramPacket dp=new DatagramPacket(bytes,bytes.length,address,port);ds.send(dp);}ds.close();}
}import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class ServerDemo {public static void main(String[] args) throws IOException {DatagramSocket ds=new DatagramSocket(10000);while (true) {byte bytes[]=new byte[1024];DatagramPacket dp=new DatagramPacket(bytes,bytes.length);ds.receive(dp);int length=dp.getLength();System.out.println(new String(bytes,0,length));}}
}
import java.io.IOException;
import java.net.*;
public class ClientDemo {public static void main(String[] args) throws IOException {DatagramSocket ds = new DatagramSocket();String str = "组播转发";byte[] bytes = str.getBytes();InetAddress address = InetAddress.getByName("224.0.1.0");int port = 10000;DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);ds.send(dp);ds.close();}
}import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class ServerDemo {public static void main(String[] args) throws IOException {MulticastSocket ms=new MulticastSocket(10000);DatagramPacket dp=new DatagramPacket(new byte[1024],1024);//把当前计算机绑定一个组播地址,表示添加到这一组中ms.joinGroup(InetAddress.getByName("224.0.1.0"));ms.receive(dp);byte [] data=dp.getData();int length=dp.getLength();System.out.println(new String(data,0,length));}
}
import java.io.IOException;
import java.net.*;
public class ClientDemo {public static void main(String[] args) throws IOException {DatagramSocket ds = new DatagramSocket();String str = " 广播转发";byte[] bytes = str.getBytes();InetAddress address = InetAddress.getByName("255.255.255.255");int port = 10000;DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);ds.send(dp);ds.close();}
}import java.io.IOException;
import java.net.DatagramPacket;import java.net.MulticastSocket;
public class ServerDemo {public static void main(String[] args) throws IOException {MulticastSocket ms=new MulticastSocket(10000);DatagramPacket dp=new DatagramPacket(new byte[1024],1024);//把当前计算机绑定一个组播地址,表示添加到这一组中ms.receive(dp);byte [] data=dp.getData();int length=dp.getLength();System.out.println(new String(data,0,length));}
}
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;public class ClientDemo {public static void main(String[] args) throws IOException {//1.创建一个Socket对象Socket socket=new Socket("127.0.0.1",10005);//2.获取IO流写数据OutputStream os = socket.getOutputStream();os.write("hello".getBytes());//3.释放资源os.close();socket.close();}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class ServerDemo {public static void main(String[] args) throws IOException {//1. 创建Socket对象ServerSocket socket=new ServerSocket(10005);//2. 等待客户端连接System.out.println("---------接受前--------");//socket.accept()开启服务后 阻塞状态 等待就收 死等ingSocket accept = socket.accept();System.out.println("---------接受后--------");//3.获得输入流都西昂InputStream in = accept.getInputStream();int b;//如果客户端不执行close关闭操作 则服务端read方法也是阻塞状态 一直等着读取,会认为读取数据未结束while ((b=in.read())!=-1){System.out.print((char)b);}//4.释放资源accept.close();socket.close();}
}
三次握手简述:
上一篇:单目标跟踪算法调研(2020)
下一篇:HTML版权符号写法及美化