目录
一、网络相关概念
二、IP地址
三、域名和端口
四、网络协议
五、 tcp和udp
六、Java_InetAddress
七、Socket
八、tcp字节流编程
1.应用案例01(单向通信)
2.应用案例02(字节流双向通信)
3.应用案例03(字符流双向通信)
4.应用案例04(网络上传文件)
一、网络相关概念
二、IP地址
三、域名和端口
四、网络协议
五、 tcp和udp
六、Java_InetAddress
package com.feiyang.basic16_net;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:InetAddress类API
*/
public class InetAddress_ {
public static void main(String[] args) throws UnknownHostException {
//获取本机的InetAddress对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("本机地址=" + localHost);//LAPTOP-VLF7OM29/192.168.191.1
//根据主机名获取InetAddress对象
InetAddress host1 = InetAddress.getByName("LAPTOP-VLF7OM29");
System.out.println("host1=" + host1);//LAPTOP-VLF7OM29/192.168.0.102
//根据域名返回InetAddress
InetAddress host2 = InetAddress.getByName("www.baidu.com");
System.out.println("host2=" + host2);//www.baidu.com/180.101.49.11
//根据InetAddress对象获取主机地址(即IP)
String hostAddress = host2.getHostAddress();
System.out.println("host2对应IP=" + hostAddress);
String hostName = host2.getHostName();
System.out.println("host2对应主机名/域名=" + hostName);
}
}
打印输出:
本机地址=LAPTOP-VLF7OM29/192.168.0.102
host1=LAPTOP-VLF7OM29/192.168.0.102
host2=www.baidu.com/180.101.49.12
host2对应IP=180.101.49.12
host2对应主机名/域名=www.baidu.com
七、Socket
八、tcp字节流编程
1.应用案例01(单向通信)
思路分析:
package com.feiyang.basic16_net;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke通信,服务端
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//1.在本机9999端口监听
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端在9999端口监听...");
//2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3.读取数据
InputStream inputStream = socket.getInputStream();
/*byte[] buf = new byte[1024];
int readLen = 0;
while((readLen=inputStream.read()) != -1){
System.out.println(new String(buf,0,readLen));
}*/
//方式2:如果读取乱码,使用转换流读取
InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(isr);
String s = bufferedReader.readLine();
System.out.println("服务端接收=" + s);
//4.关闭流
inputStream.close();
socket.close();
serverSocket.close();
}
}
package com.feiyang.basic16_net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke通信,客户端
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//1.连接服务端(IP,端口)
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket=" + socket.getClass());
//2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据
outputStream.write("hello,server".getBytes());
System.out.println("客户端写数据。。。");
//4.关闭io流,关闭socket连接
outputStream.close();
socket.close();
System.out.println("客户端退出。");
}
}
运行结果:
2.应用案例02(字节流双向通信)
package com.feiyang.basic16_net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke双向通信,服务端
*/
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
//1.在本机9999端口监听
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端在9999端口监听...");
//2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3.读取数据
InputStream inputStream = socket.getInputStream();
/*byte[] buf = new byte[1024];
int readLen = 0;
while((readLen=inputStream.read()) != -1){
System.out.println(new String(buf,0,readLen));
}*/
//方式2:如果读取乱码,使用转换流读取
InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(isr);
String s = bufferedReader.readLine();
System.out.println("服务端接收=" + s);
//服务端发送消息
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,client".getBytes());
socket.shutdownOutput(); //输出结束标记
//4.关闭流
inputStream.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出。");
}
}
package com.feiyang.basic16_net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke双向通信,客户端
*/
public class SocketTCP02Client {
public static void main(String[] args) throws IOException {
//1.连接服务端(IP,端口)
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket=" + socket.getClass());
//2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据
outputStream.write("hello,server".getBytes());
System.out.println("客户端写数据。。。");
socket.shutdownOutput();//输出结束标记
//接收数据
InputStream inputStream = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(isr);
String s = bufferedReader.readLine();
System.out.println("客户端接收=" + s);
//4.关闭io流,关闭socket连接
outputStream.close();
socket.close();
System.out.println("客户端退出。");
}
}
运行结果:
3.应用案例03(字符流双向通信)
package com.feiyang.basic16_net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke通信,字符流,服务端
*/
public class SocketTCP03Server {
public static void main(String[] args) throws IOException {
//1.在本机9999端口监听
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端在9999端口监听...");
//2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3.读取数据
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String s = bufferedReader.readLine();
System.out.println("服务端接收=" + s);
//服务端发送消息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,client,字符流");
bufferedWriter.newLine();
bufferedWriter.flush();
//4.关闭流
bufferedReader.close();
bufferedWriter.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出。");
}
}
package com.feiyang.basic16_net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description: socke通信,字符流,客户端
*/
public class SocketTCP03Client {
public static void main(String[] args) throws IOException {
//1.连接服务端(IP,端口)
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket=" + socket.getClass());
//2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server,字符流");
bufferedWriter.newLine();
bufferedWriter.flush();
//接收数据
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String s = bufferedReader.readLine();
System.out.println("客户端接收=" + s);
//4.关闭io流,关闭socket连接
bufferedReader.close();
bufferedWriter.close();
socket.close();
System.out.println("客户端退出。");
}
}
4.应用案例04(网络上传文件)
思路分析:
package com.feiyang.basic16_net;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class StreamUtils {
/**
* stream流转换成字节数组
* @param is
* @return
* @throws IOException
*/
public static byte[] streamtoByteArray(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len;
while((len=is.read(b)) != -1){
bos.write(b,0,len);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
/**
* 将stream转换成String
* @param is
* @return
* @throws IOException
*/
public static String streamToString(InputStream is) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line + "\r\n");
}
return stringBuilder.toString();
}
}
package com.feiyang.basic16_net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class TCPFileUploadServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamtoByteArray(bis);
String destPath = "src\\bbb.png";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
bos.write(bytes);
System.out.println("服务端,写入完毕");
bos.close();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write("服务端收到图片");
System.out.println("服务端:服务端收到图片");
writer.flush();
socket.shutdownOutput();
bis.close();
socket.close();
serverSocket.close();
}
}
package com.feiyang.basic16_net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author:飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class TCPFileUploadClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
String file = "d:\\bbb.png";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = StreamUtils.streamtoByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
bis.close();
socket.shutdownOutput();
String s = StreamUtils.streamToString(socket.getInputStream());
System.out.println("客户端:" + s);
bos.close();
socket.close();
}
}
运行结果:
|