Java专题十二:网络
2021-02-07 11:17
标签:bind master accept back static cep end new output Linux中Socket编程:http://www.man7.org/linux/man-pages/man2/socket.2.html java.net 包中的许多类可以提供更加高级的抽象,允许方便地访问网络上的资源。 例:从网络上下载文件并保存到本地,详细参考NetTools Java专题十二:网络 标签:bind master accept back static cep end new output 原文地址:https://www.cnblogs.com/myibu/p/12775638.htmlJava专题十二:网络
12.1.套接字
Socket
: 客户端套接字ServerSocket
: 服务器端套接字
SOCKET_API
DESCRIPTION
int socket(int domain, int type, int protocol);
create an endpoint for communication
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
accept a connection on a socket
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
initiate a connection on a socket
int bind(int sockfd, const struct sockaddr *add, socklen_t addrlen);
bind a name to a socket
int listen(int sockfd, int backlog);
listen for connections on a socket
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
send a message on a socket
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
send a message on a socket
ssize_t recv(int sockfd, const void *buf, size_t len, int flags);
receive a message on a socket
ssize_t recvfrom(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
receive a message on a socket
ssize_t read(int fd, void *buf, size_t count);
read from a file descriptor
ssize_t write(int fd, const void *buf, size_t count);
write to a file descriptor
int close(int fd);
close a file descriptor
12.1.1 服务器端(
java.net.ServerSocket
)
new ServerSocket()
)bind(SocketAddress endpoint)
)accept()
)getInputStream()、getOutputStream()
)close()
)12.1.2客户端(
java.net.Socket
)
Socket()
)connect(SocketAddress endpoint)
)getOutputStream()、getInputStream()
)close()
)
12.2.高级API
URI
: 表示统一资源标识符(URI)引用URL
: 表示统一资源定位器,即指向万维网上“资源”的指针URLConnection
: 表示应用程序和URL之间的通信链接HttpURLConnection
: 支持HTTP特定功能的URLConnection,规范见http://www.w3.org/pub/www/Protocols/NetTools.download("https://dl.google.com/android/repository/platform-tools-latest-windows.zip","");
public static boolean download(String url, String saveDir)
throws URISyntaxException, IOException {
// get download file name
String fileName = url.substring(url.indexOf(‘/‘));
if (saveDir == null || saveDir.trim().equals("")){
saveDir = "";
}else{
File out = new File(saveDir);
if (!out.exists() || !out.isDirectory()){
throw new FileNotFoundException();
}
}
// open connection
URI uri = new URI(url);
URL url0 = uri.toURL();
HttpURLConnection conn = (HttpURLConnection)url0.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
conn.connect();
// response is ok
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream is = conn.getInputStream();
File file = new File(saveDir, fileName);
FileOutputStream fos = new FileOutputStream(new File("platform-tools-latest-windows.zip"));
byte[] buf = new byte[1024];
int readCount;
while((readCount = is.read(buf)) > 0){
fos.write(buf, 0, readCount);
}
is.close();
fos.close();
return true;
}
return false;
}