网络编程
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
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 {
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 6666;
socket = new Socket(address,port);
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();
}
}
}
}
}
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 {
serverSocket = new ServerSocket(6666);
while (true){
Socket socket = serverSocket.accept();
is = socket.getInputStream();
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
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();
}
}
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 {
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("result.png");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
OutputStream outputStream = socket.getOutputStream();
outputStream.write("File Send Success".getBytes());
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();
}
}