Java网络编程笔记
程序员文章站
2022-03-05 15:39:00
...
网络编程
1. InetAddress
package com.mashiro.lesson01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class INetAddressTest {
public static void main(String[] args) {
InetAddress inetAddress;
InetAddress inetAddress1;
InetAddress inetAddress2;
InetAddress inetAddress3;
try {
inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
inetAddress1 = InetAddress.getByName("localhost");
System.out.println(inetAddress1);
inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
inetAddress3 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress3);
System.out.println(inetAddress3.getAddress());
System.out.println(inetAddress3.getCanonicalHostName());
System.out.println(inetAddress3.getHostAddress());
System.out.println(inetAddress3.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
2. TCP
2.1. TCP Test01
- Client
package com.mashiro.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
// 1.客户端创建Socket连接
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 6666;
socket = new Socket(address,port);
// 2.
os = socket.getOutputStream();
os.write("Hello,Java Socket".getBytes());
} catch (IOException 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();
}
}
}
}
}
- Server
package com.mashiro.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 1.创建服务器连接 ip
serverSocket = new ServerSocket(6666);
while (true){
// 2.等待获取客户端连接请求
Socket socket = serverSocket.accept();
// 3.读取客户端发送的信息
is = socket.getInputStream();
// 4.创建信息管道流
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 (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.2.TCP Test02
- Client
package com.mashiro.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient2 {
public static void main(String[] args) throws Exception {
// 1.创建socket连接
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 9000;
Socket socket = new Socket(address, port);
// 2. 创建socket输出流
OutputStream ops = socket.getOutputStream();
// 3. 创建文件输入流,并读取文件
FileInputStream fis = new FileInputStream(new File("1.png"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
// 4. 向服务器写入流
ops.write(buffer,0,len);
}
// 4. 通知服务器文件发送完毕,结束输出流
socket.shutdownOutput();
// 5.获取服务器反馈信息
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream 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());
// 6.释放资源
baos.close();
inputStream.close();
fis.close();
ops.close();
socket.close();
}
}
- Server
package com.mashiro.lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer2 {
public static void main(String[] args) throws Exception {
// 1. 开放服务器连接端口
ServerSocket serverSocket = new ServerSocket(9000);
// 2. 获取客户端连接请求
Socket socket = serverSocket.accept();
// 3.创建socket输入流,获取客户端输入
InputStream is = socket.getInputStream();
// 4.创建文件输出流,写出文件
FileOutputStream fos = new FileOutputStream("result.png");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
// 5.文件接收完毕,反馈客户端
OutputStream outputStream = socket.getOutputStream();
outputStream.write("File Send Success".getBytes());
// 6.释放资源
outputStream.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
3. UDP
-
package com.mashiro.lesson03; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UdpClient { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); String msg = "Hello Udp Packet"; InetAddress address = InetAddress.getByName("localhost"); int port = 9090; DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, address,port); socket.send(packet); socket.close(); } }
-
package com.mashiro.lesson03; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UdpServer { public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(9090); byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet); System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData(), 0, packet.getLength())); socket.close(); } }
上一篇: 数值分析c语言(2)
下一篇: Java语言 随机点名程序