127.0.0.1代表本机(localhost)
查询ip地址的几种方法:
//查询本地地址
InetAddress byName = InetAddress.getByName(“127.0.0.1”);
InetAddress byname1 = InetAddress.getByName(“localhost”);
InetAddress byname2 = InetAddress.getLocalHost();//本机ip地址
InetAddress byName1 = InetAddress.getByName(“www.baidu.com”);
InetAddress与InetSocketAddress两种方法,了解即可。
socket在网络编程属于插槽,
tcp连接案例
可以下载网络资源代码:
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class download {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL(“https://m701.music.126.net/20220105133033/424a7d447098f13bf603a04eace181a8/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/12380906077/95e1/9fe4/370a/e82ad1fd8281f85f551f29a5a8000c82.m4a”);
//2.连接到这个资源 http
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream(“c6.m4a”);
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
}
}



