1、网络通信的两要素
1.1 IP和端口号
- IP:唯一的标识 Internet 上的计算机(通信实体)
- 在Java中使用InetAddress类代表IP
- IP分类:IPv4 和 IPv6
- 域名:容易记忆,域名服务器(DNS)负责将域名转化成IP地址,这样才能和主机建立连接
- 端口号:正在计算机上运行的进程
- 端口号与IP地址的组合得出一个网络套接字:Socket
1.2 网络通信协议
TCP和UDP
2、InetAddress的使用
InetAddress类:此类的一个对象就代表着一个具体的IP地址
public class testInetAddress {
public static void main(String[] args) {
try {
InetAddress inet = InetAddress.getByName("www.baidu.com");
System.out.println(inet);
System.out.println(inet.getHostAddress());
System.out.println(inet.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
3、TCP的网络编程
/**
* @author LFuser
* @create 2019-06-20-16:53
* 客户端给服务端发送信息,服务端输出信息到控制台
*/
public class testTCP {
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try{
//1.创建一个Socket的对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
//3.输出过程
os.write("这是一个客户端的请求信息".getBytes());
//4.shutdownOutput():显式的告诉服务端发送完毕
socket.shutdownOutput();
//5.接收服务端的信息
is = socket.getInputStream();
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
String str = new String(b,0,len);
System.out.println(str);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//4.关闭资源
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try{
//1.创建一个ServerSocket对象,通过构造器指明自身的端口号
serverSocket = new ServerSocket(9090);
//2.调用其accept()方法,返回一个Socket的对象
socket = serverSocket.accept();
//3.调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流
inputStream = socket.getInputStream();
//4.对获取的输入流进行的操作
byte[] b = new byte[1024];
int len;
while((len = inputStream.read(b))!=-1){
String str = new String(b,0,len);
System.out.println(str);
}
//5.向客户端发送信息
outputStream = socket.getOutputStream();
outputStream.write("这是服务端反馈的信息".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
//5.关闭相应资源
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4、URL编程
URL:统一资源定位符,对应着互联网的某一资源地址
/**
* @author LFuser
* @create 2019-06-20-19:39
*/
public class testURL {
public static void main(String[] args) {
//1.创建一个URL对象
URL url = null;
try {
url = new URL("");
} catch (MalformedURLException e) {
e.printStackTrace();
}
//方法一:
InputStream inputStream = null;
try {
//2.从服务端将资源读取进来
inputStream = url.openStream();
byte[] b = new byte[20];
int len;
while ((len = inputStream.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//方法二:
URLConnection urlConnection = null;
InputStream inputStream1 = null;
FileOutputStream fileOutputStream = null;
try{
urlConnection = url.openConnection();
inputStream1 = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream(new File("a.txt"));
byte[] b1 = new byte[20];
int len1;
while((len1 = inputStream1.read(b1))!=-1){
fileOutputStream.write(b1,0,len1);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream1!=null){
try {
inputStream1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}