网络编程的目的是直接或间接地通过网络协议与其它计算机实现数据交换,进行通讯。
网络通信要素
IP和端口号
IP
@Test
public void test1() {
try {
InetAddress address_1 = InetAddress.getByName("www.atguigu.com");
System.out.println(address_1);
//获取InetAddress对象所含的域名
System.out.println(address_1.getHostName());
//获取InetAddress对象所含的IP地址
System.out.println(address_1.getHostAddress());
//获取本机的域名和工P地址.
InetAddress address_2 = InetAddress.getLocalHost();
System.out.println(address_2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
端口号
网络协议
TCP协议
UDP协议
网络编程
TCP网络编程
- 发送字符
```java
//客户端
@Test
public void client1() {
Socket socket = null;
OutputStream os = null;
try {
} catch (IOException e) {//1、创建Socket对象,指明服务器端的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");//服务端ip
socket = new Socket(inet, 8899);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
os.write("你好,我是客户端".getBytes());
} finally {e.printStackTrace();
}//4、资源关闭 if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } }
}
//服务端 @Test public void server1() { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、创建服务器端的ServerSockt,指明自己的端口号 serverSocket = new ServerSocket(8899); //2、调用accept()表示接受来自于客户端的socket socket = serverSocket.accept(); //3、获取输入流 is = socket.getInputStream();
//不建议这么写,可能会有乱码
// byte[] buffer = new byte[1024];
// int len;
// while ((len = is.read(buffer)) != -1) {
// String s = new String(buffer, 0, len);
//
// }
//4、读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、资源关闭
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<br />
2. **发送文件**
**⚠️ **如果传输不成功报错,尝试修改一下端口号。
```java
//客户端
@Test
public void client3() {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1、
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9099);
//2、
outputStream = socket.getOutputStream();
//3、
fileInputStream = new FileInputStream(new File("src/web/Kobe Byrant.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
socket.shutdownOutput(); //图片传完,不再传输数据,防止阻塞。
//4、接收来自于服务器端的数据,并显示到控制台上
InputStream inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer1 = new byte[5];
int len1;
while ((len1 = inputStream.read(buffer1)) != -1) {
byteArrayOutputStream.write(buffer1, 0, len1);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务器端
@Test
public void server3() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
try {
//1、
serverSocket = new ServerSocket(9099);
//2、
socket = serverSocket.accept();
//3、
inputStream = socket.getInputStream();
//4、
fileOutputStream = new FileOutputStream(new File("src/web/(copy)Kobe Byrant.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
//5、服务器端给予客户端反馈
outputStream = socket.getOutputStream();
outputStream.write("你好,文件已收到!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//6、
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP网络编程
//发送端
@Test
public void sender() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str = "UDP方式发送的数据";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9099);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
//接收端
@Test
public void receiver() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9099);
byte[] data = new byte[100];
DatagramPacket packet = new DatagramPacket(data, 0, data.length);
socket.receive(packet);
System.out.println(new String(packet.getData(), 0, packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
URL编程
@Test
public void test1() {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("http://localhost:8080/examples/Kobe_Byrant.jpg?username=Huang");
System.out.println("getProtocol() :" + url.getProtocol());
System.out.println("getHost() :" + url.getHost());
System.out.println("getPort() :" + url.getPort());
System.out.println("getPath() :" + url.getPath());
System.out.println("getFile() :" + url.getFile());
System.out.println("getQuery() :" + url.getQuery());
//下载图片到本地
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("src/web/Kobe_Byrant1.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
System.out.println("下载完成!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}