记录Java网络编程相关知识,欢迎大家沟通交流
唯一定义一台网络上的计算机。
127.0.0.1:本机localhost
解决记忆IP问题,方便记忆,最终域名会映射到IP上。
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
端口表示计算机上一个程序的进程。
客户端:
1、连接服务器Socket
2、发送消息
服务端:
1、建立服务端的端口 ServerSocket
2、等待用户的连接 accept
3、接收消息
收发消息示例
/**
* 客户端
*/
public class TCPClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
try {
InetAddress localHost = InetAddress.getLocalHost();
int port = 9999;
socket = new Socket(localHost, port);
outputStream = socket.getOutputStream();
outputStream.write("你好,世界!".getBytes());
outputStream.flush();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 服务端
*/
public class TCPServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
serverSocket = new ServerSocket(9999);
while (true) {
accept = serverSocket.accept();
inputStream = accept.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
byteArrayOutputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
TCP上传文件流
在项目的根目录下拷贝client.jpg图片
/**
* 服务器
*/
public class TCPUploadFileServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
OutputStream outputStream = accept.getOutputStream();
outputStream.write("服务端接收完毕".getBytes());
outputStream.flush();
outputStream.close();
fileOutputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* 客户端
*/
public class TCPUploadFileClient {
public static void main(String[] args) {
try {
InetAddress host = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(host, port);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(new File("client.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
//通知服务器输出流传输完毕
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] iBuffer = new byte[1024];
int ilen;
while ((ilen = inputStream.read(iBuffer)) != -1) {
byteArrayOutputStream.write(iBuffer, 0, ilen);
}
System.out.println(byteArrayOutputStream.toString());
fileInputStream.close();
outputStream.close();
socket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
服务端
1、自定义(S端)
2、Tomcat服务器(S端):java后台开发
客户端
1、自定义(C端)
2、浏览器(B端)
UDP不存在服务端、客户端的概念,可以互相发送
public class UDPClient {
public static void main(String[] args) {
try {
DatagramSocket socket = null;
socket = new DatagramSocket();
InetAddress host = InetAddress.getByName("127.0.0.1");
int port = 9090;
String msg = "你好啊,服务器";
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, host, port);
socket.send(datagramPacket);
socket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public class UDPServer {
public static void main(String[] args) {
{
try {
DatagramSocket socket = null;
socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(datagramPacket);
System.out.println(datagramPacket.getAddress().getHostAddress());
System.out.println(new java.lang.String(datagramPacket.getData(), 0, datagramPacket.getLength()));
socket.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
http://www.baidu.com/
统一资源定位符:用于定位资源,定位互联网上的某一个资源。
DNS域名解析:把 域名 解析成 IP+Port
URL组成结构:协议://IP地址:端口/项目名/资源
/**
* URL网络编程,常用api示例
*/
public class URLDemo001 {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=xiaowu&password=123");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//主机名
System.out.println(url.getPort());//端口
System.out.println(url.getPath());//文件相对路径
System.out.println(url.getFile());//全路径
System.out.println(url.getQuery());//参数
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
/**
* URL网络编程,下载网络图片
*/
public class URLDemo001 {
public static void main(String[] args) {
try {
URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
//文件会下载到根目录下(项目根目录)
FileOutputStream fileOutputStream = new FileOutputStream(new File("baidu.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
**end**