- 1、IP地址
- 2、端口
- 3、TCP协议实现聊天
- 4、TCP实现文件上传
- 5、UDP发送消息
- 6、UDP实现聊天
- 7、URL下载网络资源
- 本机IP地址:127.0.0.1、localhost
- 域名:域名是IP相绑定
- IP相关的代码
package network; import java.net.InetAddress; import java.net.UnknownHostException; //测试IP public class TestInetaddress { public static void main(String[] args) { try { //查询本机地址 InetAddress localHost = InetAddress.getByName("localhost"); System.out.println(localHost); //查询网络地址 InetAddress baidu = InetAddress.getByName("www.baidu.com"); System.out.println(baidu); } catch (UnknownHostException e) { e.printStackTrace(); } } }
- 运行结果
- 端口表示计算机上的一个程序的进程
- 不同的进程有不同的端口号,用来区分软件;
- 被规定0~65532
- TCP,UDP:单个协议号不能冲突
- 公用端口:0~1023
- 代码演示
package network; import java.net.InetSocketAddress; public class TestIntSocketAddress { public static void main(String[] args) { //获取端口 InetSocketAddress localhost = new InetSocketAddress("localhost", 8080); System.out.println(localhost); System.out.println(localhost.getAddress()); //获取IP地址 System.out.println(localhost.getHostName()); //获取域名 System.out.println(localhost.getPort()); //获取端口号 } }
- 运行结果
- 客户端
- 连接服务器Socket
- 发送消息
- 代码演示
package network; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; //客户端 public class TcpClientDemo01 { public static void main( String[] args ) { Socket socket = null; OutputStream os = null; try { //1.连接服务器地址 InetAddress serverIP = InetAddress.getByName("localhost"); int port=537; //2.创建一个socket连接 socket = new Socket(serverIP,port); //3.发送消息 os = socket.getOutputStream(); os.write("Java学不会".getBytes()); } catch (Exception e) { e.printStackTrace(); }finally{ if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
- 服务器
- 建立服务的端口ServerSocket
- 等待用户连接accept
- 接受客户端消息
- 代码演示
package network; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //服务器 public class TcpServerdemo01 { public static void main( String[] args ) { ServerSocket serverSocket = null; Socket socket = null; ByteArrayOutputStream baos = null; InputStream is = null; while (true) { try { //1.得有一个地址 serverSocket = new ServerSocket(537); //2.接受消息 socket = serverSocket.accept(); //3.获取客户端消息 is = socket.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while((len=is.read(buffer))!=-1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); }finally{ //关闭流 先开后关 if(baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
- 运行结果
- 客户端
package network; import java.io.*; import java.net.InetAddress; import java.net.Socket; public class TcpClientDemo02 { public static void main( String[] args ) { Socket socket = null; OutputStream os = null; FileInputStream fis = null; InputStream inputStream = null; ByteArrayOutputStream baos = null; try { //1.创建一个Socket连接 socket = new Socket(InetAddress.getByName("localhost"), 537); //2.创建一个输出流 os = socket.getOutputStream(); //3.读取文件 fis = new FileInputStream(new File("1.jpg")); //4.写出文件 byte[] buffer = new byte[1024]; int len; while((len=fis.read(buffer))!=-1){ os.write(buffer,0,len); } //通知服务器,我已经结束了 socket.shutdownOutput();//已经传输完了 //确定服务器以接收,再断开连接 inputStream = socket.getInputStream(); baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2; while((len2=inputStream.read(buffer2))!=-1){ baos.write(buffer2,0,len2); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); }finally{ //5.关闭资源 if(baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis!=null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
- 服务器端
package network; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class TestServerDemo02 { public static void main( String[] args ) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; FileOutputStream fos = null; OutputStream os = null; try { //1.创建一个服务端口 serverSocket = new ServerSocket(537); //2.接听客户端的连接 socket = serverSocket.accept();//阻断式监听,一直等待客户端连接 //3.获取输入流 is = socket.getInputStream(); //4.文件输出 fos = new FileOutputStream(new File("接收.jpg")); byte[] buffer = new byte[1024]; int len; while((len=is.read(buffer))!=-1){ fos.write(buffer,0,len); } //通知客户端接收完毕 os = socket.getOutputStream(); os.write("over".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally{ //5.关闭资源 if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
- 运行结果
- 发送端代码
package network; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; //不需要服务器 public class UdpclientDemo01 { public static void main( String[] args ) { DatagramSocket socket = null; InetAddress localhost = null; DatagramPacket packet = null; try { //1.建立一个Socket socket = new DatagramSocket(); //2.建个包 String msg = "Java学不会"; //3.发给谁 localhost = InetAddress.getByName("localhost"); int port = 537; //数据,数据的长度其实,要发送给谁 packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port); //4.发送包 socket.send(packet); } catch (Exception e) { e.printStackTrace(); }finally{ //5.关闭流 不需要关闭包 if(socket!=null){ socket.close(); } } } }
- 接收端代码
package network; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; //还是要等待客户端的连接 public class UdpServerDemo01 { public static void main( String[] args ) { DatagramSocket socket=null; try { //1.开放端口 socket = new DatagramSocket(537); //2.接收数据包 byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet);//阻塞接收 System.out.println(new String(packet.getData(),0,packet.getLength())); } catch (Exception e) { e.printStackTrace(); }finally{ //3.关闭资源 if(socket!=null){ socket.close(); } } } }
- 运行结果
- 两个都是接收端,两个都是发送端
- 创建两个线程
- 发送端线程
package network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class Talksend implements Runnable{ DatagramSocket socket = null; BufferedReader reader = null; private int fromPort; private String toIP; private int toPort; public Talksend( int fromPort, String toIP, int toPort ) { this.fromPort = fromPort; this.toIP = toIP; this.toPort = toPort; try { socket = new DatagramSocket(fromPort); reader = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run( ) { //准备数据: 控制台读取Ststem.in while (true) { try { String data = reader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort)); socket.send(packet); if(data.equals("bye")){ break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
- 接收端线程
package network; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class TalkRecevie implements Runnable{ DatagramSocket socket = null; private int port; private String msgFrom; public TalkRecevie( int port ,String msgFrom) { this.port = port; this.msgFrom=msgFrom; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run( ) { while (true) { try { //准备接收消息 byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container, 0, container.length); socket.receive(packet);//阻塞接收 //断开连接 String receiveData = new String(packet.getData(),0,packet.getLength()); System.out.println(msgFrom+receiveData); if(receiveData.equals("over")){ break; } } catch (Exception e) { e.printStackTrace(); } } //关闭资源 socket.close(); } }
- 创建两个客户端
- 学生端
package network; public class TalkStudent { public static void main( String[] args ) { //开启两个线程 new Thread(new Talksend(8888,"localhost",9999)).start(); new Thread(new TalkRecevie(7777,"学生:")).start(); } }
- 老师端
package network; public class TalkTeacher { public static void main( String[] args ) { new Thread(new Talksend(5555,"localhost",7777)).start(); new Thread(new TalkRecevie(9999,"老师:")).start(); } }
- 运行结果
- URL:统一资源定位符,定位资源。
协议://ip地址:端口号/项目名/资源
package network; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class UrlDemo { public static void main( String[] args ) throws Exception { //1.下载地址 URL url = new URL("https://m10.music.126.net/20220104173853/f67c6ebdd42e82b76676b36e0a56a8bb/ymusic/0e0e/070c/030e/9d372a7aec0fbbca8d764d2261cacb1d.mp3"); //2.连接到这个资源 http HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("1.mp3"); byte[] buffer = new byte[1024]; int len; while((len=(inputStream.read(buffer)))!=-1){ fos.write(buffer,0,len); } fos.close(); inputStream.close(); urlConnection.disconnect(); } }
- 运行结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)