InetAddress
import java.net.InetAddress; import java.net.UnknownHostException; public class test01 { /** * 端口号与IP地址的组合得出一个网络套接字:Socket * * 1. 本地回路地址:127.0.0.1 对应着:localhost * 2. InetAddress的两个实例化方法:getByName(String host)、getLocalHost() * 3. InetAddress的两个常用方法:getHostName()、getHostAddress() * @param args */ public static void main(String[] args) throws UnknownHostException { InetAddress inet1 = InetAddress.getByName("192.168.10.23"); System.out.println(inet1); InetAddress inet2 = InetAddress.getByName("www.baidu.com"); System.out.println(inet2); InetAddress inet3 = InetAddress.getByName("127.0.0.1"); System.out.println(inet3); InetAddress inet4 = InetAddress.getLocalHost(); System.out.println(inet4); //getHostName() System.out.println(inet2.getHostName()); //getHostAddress() System.out.println(inet2.getHostAddress()); } }
TCP
1.客户端发送内容给服务端,服务端将内容打印到控制台上
import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class test02 { //客户端 @Test public void client() throws IOException { //1.创建一个Socket对象,指明服务器端的ip和端口号 InetAddress inet = InetAddress.getByName("127.0.0.1"); Socket socket = new Socket(inet, 8899); //2.获取一个输出流,用于传输数据 OutputStream os = socket.getOutputStream(); os.write("你好,我是客户端".getBytes()); //3.关闭资源 os.close(); socket.close(); } //服务器 @Test public void server() throws IOException { //1.创建服务器端的ServerSocket,指明自己的端口号 ServerSocket ss = new ServerSocket(8899); //2.调用accept()表示接收来自客户端的socket Socket socket = ss.accept(); //3.获取输入流 InputStream is = socket.getInputStream(); //4.读取数据 ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){ baos.write(buffer, 0, len); } System.out.println(baos.toString()); //5.关闭资源 baos.close(); is.close(); socket.close(); ss.close(); } }
2.客户端发送文件给服务端,服务端将文件保存在本地
import com.sun.security.ntlm.Server; import org.junit.Test; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class test03 { //客户端 @Test public void client() throws IOException { //1. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); //2. OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("hello.jpg")); //3. byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ os.write(buffer, 0, len); } //4. fis.close(); os.close(); socket.close(); } //服务器 @Test public void server() throws IOException { //1. ServerSocket ss = new ServerSocket(); //2. Socket socket = ss.accept(); //3. InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("feifei.jpg")); //4. byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer, 0, len); } //5. fos.close(); is.close(); socket.close(); ss.close(); } }
3.从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给 客户端。并关闭相应的连接
import org.junit.Test; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class test04 { //客户端 @Test public void client() throws IOException { //1. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); //2. OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("hello.jpg")); //3. byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ os.write(buffer, 0, len); } //图片传完了,不再发了 socket.shutdownOutput(); //4.接收来自服务器端的数据,并显示到控制台上 InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[20]; int len2; while((len2 = is.read(buffer2)) != -1){ baos.write(buffer2, 0, len2); } System.out.println(baos.toString()); //5. fis.close(); os.close(); socket.close(); is.close(); baos.close(); } //服务器 @Test public void server() throws IOException { //1. ServerSocket ss = new ServerSocket(); //2. Socket socket = ss.accept(); //3. InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("feifei.jpg")); //4. byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer, 0, len); } //5.服务器端给予客户反馈 OutputStream os = socket.getOutputStream(); os.write("我已经收到消息了".getBytes()); //6. fos.close(); is.close(); socket.close(); ss.close(); os.close(); } }
UDP
import org.junit.Test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class test05 { //发送端 @Test public void sender() throws IOException { DatagramSocket socket = new DatagramSocket(); String str = "我是通过UDP发送的消息"; byte[] data = str.getBytes(); InetAddress inet = InetAddress.getLocalHost(); DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090); socket.send(packet); socket.close(); } //接受端 @Test public void receiver() throws IOException { DatagramSocket socket = new DatagramSocket(9090); byte[] buffer = new byte[100]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); System.out.println(new String(packet.getData(), 0, packet.getLength())); socket.close(); } }
URL
import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class test07 { @Test public void testURL() throws IOException { URL url = new URL("http://localhost:8080/examples/hello.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); InputStream is = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("hello2.jpg"); byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer, 0, len); } //关闭资源 is.close(); fos.close(); urlConnection.disconnect(); } }
来源:https://www.cnblogs.com/xiaoran991/p/12543899.html