目录
注意:1.网络开发中不要使用0-1024的端口
2.端口号不能重复,否则会出现端口被占用的情况
人和人交流(通讯):依靠语言【中文,英语,日语...】
语言本身就是一种协议。
在网络编程中,数据的组织形式就是协议。
用法
- //获取本机的InetAddress对象
- InetAddress localHost = InetAddress.getLocalHost();
- System.out.println(localHost);
- //2.根据指定的主机名获取 InetAddress对象
- InetAddress host1 = InetAddress.getByName("DESKTOP-6HLDCVM");
- System.out.println(host1);
-
- //3.根据域名 返回InetAddress对象比如 www.baidu.com
- InetAddress host2 = InetAddress.getByName("www.baidu.com");
- System.out.println(host2);
- //4.通过返回InetAddress对象 反向获取地址
- String hostAddress = host2.getCanonicalHostName();
- System.out.println(hostAddress);
- //5. 通过INetAddress 获取域名/主机名
- String hostName = host2.getHostName();
- String hostName2 = host1.getHostName();
- System.out.println(hostName);
- System.out.println(hostName2);
发起连接的一方通过Socket调用OutputStream将数据写到数据通道中,然后接收方通过Socket调用InputStream读取数据。结束连接的时候要关闭Socket。
思路分析
客户端
- public class Scoket02 {
- public static void main(String[] args) throws IOException {
- //1.连接本机的9999端口 ,连接成功返回一个socket对象
- Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
- System.out.println("socket对象获取");
- //得到和 socket对象关联的输出流对象
- OutputStream outputStream = socket.getOutputStream();
- //通过输出流 写入数据到通道
- outputStream.write("hello".getBytes());
- //关闭流
- socket.close();
- outputStream.close();
- System.out.println("客户端关闭");
- }
- }
服务端
- //服务端
- public class Socket01 {
- public static void main(String[] args) throws IOException {
- //在本机的9999端口监听,等待连接
- ServerSocket serverSocket = new ServerSocket(9999);
- System.out.println("服务端在9999监听");
- //当没有客户端连接9999端口时,程序会阻塞,等待连接
- Socket socket = serverSocket.accept();//当有客户端连接时会返回一个 socket对象
- System.out.println("有客户连接,正在获取数据");
- //通过socket 获取通道中的数据
- InputStream inputStream = socket.getInputStream();
- //IO读取
- byte[] buf =new byte[1024];
- int readLen = 0;
- while ((readLen = inputStream.read(buf)) != -1){
- System.out.println(new String(buf,0,readLen));
- }
- //关闭流
- inputStream.close();
- socket.close();
- serverSocket.close();
-
- }
- }
小练习2
- public class Socket03 {
- public static void main(String[] args) throws IOException {
- //服务器在9999端口监听
- ServerSocket serverSocket = new ServerSocket(9999);
- //等待客户端连接
- System.out.println("等待用户端连接");
- Socket socket = serverSocket.accept();
- //有客户端连接 读取通道内消息
- InputStream inputStream = socket.getInputStream();
- int readlen = 0;
- byte[] buf = new byte[1024];
- while ((readlen = inputStream.read(buf)) != -1 ){
- System.out.println(new String(buf,0,readlen));
- }
- //获取数据后准备,回信
- OutputStream outputStream = socket.getOutputStream();
- outputStream.write("hello,client".getBytes());
- socket.shutdownOutput();
- //关闭流
- outputStream.close();
- inputStream.close();
- socket.close();
- serverSocket.close();
- System.out.println("服务端退出");
- }
- }
- public class Socket04 {
- public static void main(String[] args) throws IOException {
- //连接到9999端口
- Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
- //发送消息
- OutputStream outputStream = socket.getOutputStream();
- outputStream.write("hello server".getBytes());
- System.out.println("等待对方回信");
- socket.shutdownOutput();
- //接收回信
- InputStream inputStream = socket.getInputStream();
- int i = 0;
- byte[] b = new byte[1024];
- while ((i = inputStream.read(b))!= -1){
- System.out.println(new String(b,0,i));
- }
- System.out.println("客户端退出");
- //关闭流
- inputStream.close();
- outputStream.close();
- socket.close();
-
-
- }
- }
小练习3
有了前面的示范,这题也很容易解决
利用Buffered 和 转换流
- public class Socket05 {
- public static void main(String[] args) throws IOException {
- //监听9999
- ServerSocket serverSocket = new ServerSocket(9999);
- //等待连接
- Socket socket = serverSocket.accept();
- InputStream inputStream = socket.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- String string = bufferedReader.readLine();
- System.out.println(string);
- //回信
- OutputStream outputStream = socket.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
- bufferedWriter.write("你也好,客户端");
- bufferedWriter.newLine();//换行表示输入结束
- bufferedWriter.flush();如果使用字符流一点要刷新,否则不能加入通道
- //关闭流
- socket.close();
- serverSocket.close();
- bufferedReader.close();
- bufferedWriter.close();
- System.out.println("服务端关闭");
- }
- }
- public class Socket06 {
- public static void main(String[] args) throws IOException {
- Socket socket = new Socket(InetAddress.getLocalHost(),9999);
- OutputStream outputStream = socket.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
- bufferedWriter.write("你好啊服务器");
- bufferedWriter.newLine();//换行表示输入结束
- bufferedWriter.flush();//如果使用字符流一点要刷新,否则不能加入通道
- //接收回信
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String string = bufferedReader.readLine();
- System.out.println(string);
- //关闭流
- bufferedReader.close();
- bufferedWriter.close();
- socket.close();
- System.out.println("服务端关闭");
-
- }
- }
还有个 netstat -anb 可以显示哪个程序连接到这个端口
-
- public class UdpReceiverA {
- public static void main(String[] args) throws IOException {
- //1.创建一个DatagramSocket,准备在9999接收数据
- DatagramSocket datagramSocket = new DatagramSocket(9999);
- //2.构建一个 DatagramPacket对象,准备接收数据
- //UDP协议数据包最大64k 64*1024
- byte[] buf = new byte[64 * 1024];
- DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
- //3.调用,接收方法,将通过网络传输的 DatagramPacket 对象
- datagramSocket.receive(datagramPacket);//在9999端口等待,如果有数据包传输,就接收
- //4.把Packet 进行拆包,取出数据并显示
- int len = datagramPacket.getLength();//实际接收到的数据字节长度
- byte[] data = datagramPacket.getData();//接收到的数据
- String s =new String(data,0,len);
- System.out.println(s);
- //回复消息
- byte[] bytes = "没问题,我去找你".getBytes();
- DatagramPacket datagramPacket1 = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.43.237"),9998);
- datagramSocket.send(datagramPacket1);
- //5.关闭流
- datagramSocket.close();
- }
- }
- public class UdpSenderB {
- public static void main(String[] args) throws IOException {
- //1。创建DatagramPacket 对象,准备接收和发送数据
- DatagramSocket socket = new DatagramSocket(9998);//发送端也可以是接收端,如果
- //之后接收数据,就在9998端口接收
- //2.发送数据封转 DatagramPacket(数组,数组长度,对方ip地址,端口号)
- byte[] bytes = "hello,今天吃火锅".getBytes();
- DatagramPacket packet =
- new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.43.237"),9999);
- socket.send(packet);//发送封装后的数据
- //接收回信
- byte[] bytes1 = new byte[1024*5];//
- DatagramPacket datagramPacket = new DatagramPacket(bytes1, bytes1.length);
- int len = datagramPacket.getLength();
- socket.receive(datagramPacket);
- byte[] bytes2 = datagramPacket.getData();
- String s = new String(bytes2,0,len);
- System.out.println(s);
-
- socket.close();
- }
- }