网络编程
cmd ping www.baidu.com
IP
127.0.0.1:本机 localhost
ip地址分类
- ipv4/ipv6
- ipv4 127.0.0.1 4个字节 ~42亿 2011年已经用尽
- ipv6 240e:b5:4100:3cf3:18b5:1368:bfcd:163 128位 8个无符号整数表示
- 公网(互联网)-私网(局域网)
- 192.168.xx.xx(专门给组织内部使用的)
- A B C D类地址 0~127 128~192 193~224 225~255
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
//查询百度地址
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
//常用方法
System.out.println(inetAddress4.getAddress());
System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
System.out.println(inetAddress4.getHostAddress());//ip
System.out.println(inetAddress4.getHostName());//域名或者自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
端口
netstat -aon//cmd查询所有端口
netstat -ano|findstr “5900” //查看指定端口
tasklist|findstr “8696”//查看指定端口进程
协议
TCP:用户传输协议
UDP:用户数据报协议
TCP UDP对比
TCP:打电话
- 连接+稳定
- 三次握手 四次分手
- A->B B->A A->B
- A->B B->A B->A A->B
- 客户端 服务端
UDP:发短信
- 不连接+不稳定
- 客户端和服务端没有明确界限
- 不管是否准备好 都能发送
InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(inetSocketAddress1);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress1.getAddress());
System.out.println(inetSocketAddress1.getHostName());//地址
System.out.println(inetSocketAddress1.getPort());//端口
TCP
服务端
-
建立服务端口ServerSocket
-
等待用户连接accept
-
接受用户消息
package com.Eiso; 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; Socket socket =null; InputStream is=null; ByteArrayOutputStream baos=null; try { //地址9998 serverSocket = new ServerSocket(9998); while(true){ //等待客户端连接 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(socket!=null){ try{ socket.close(); }catch (IOException e){ e.printStackTrace(); } } if(serverSocket!=null){ try{ serverSocket.close(); }catch (IOException e){ e.printStackTrace(); } } } } }
客户端
- 连接服务器Socket
- 发送消息
package com.Eiso;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TcpClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream os =null;
try {
//获得服务器地址 端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9998;
//创建socket连接
socket = new Socket(serverIP,9998);
//发送消息IO流
os = socket.getOutputStream();
os.write("cnewclkweckl".getBytes());
} catch (Exception 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.Eiso;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClient {
public static void main(String[] args) throws Exception{
//创建socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9998);
//创建输出流
OutputStream os = socket.getOutputStream();
//读取文件
FileInputStream fis= new FileInputStream(new File("untitled.jpg"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//通知服务器
socket.shutdownOutput();
//确定
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2048];
int len2;
while((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
package com.Eiso;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServer {
public static void main(String[] args) throws Exception{
//创建服务
ServerSocket serverSocket = new ServerSocket(9998);
//监听客户端
Socket socket=serverSocket.accept();
//获取输入流
InputStream is = socket.getInputStream();
//文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端
OutputStream os = socket.getOutputStream();
os.write("接收成功".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
Tomcat
服务端 tomcat
客户端 网页
UDP
package com.Eiso;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//服务端
public class TcpServer {
public static void main(String[] args) throws Exception{
//开放端口
DatagramSocket socket = new DatagramSocket(9998);
//接受包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭流
socket.close();
}
}
package com.Eiso;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//客户端
public class TcpClient {
public static void main(String[] args) throws Exception{
//建立socket
DatagramSocket socket = new DatagramSocket();
//建立包
String msg = "hello world";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9998;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//发送包
socket.send(packet);
//关闭流
socket.close();
}
}
UDP聊天
多线程:
package com.Eiso;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String ID;
public TalkReceive(int port,String ID){
this.port=port;
this.ID=ID;
try{
socket = new DatagramSocket(port);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
try {
//准备接受
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(ID+":"+receiveData);
if(receiveData.startsWith("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭流
socket.close();
}
}
package com.Eiso;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort,String toIP,int toPort){
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try{
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
try{
String data = reader.readLine();
byte[] msg = data.getBytes();
DatagramPacket packet = new DatagramPacket(msg,0,msg.length,new InetSocketAddress(this.toIP,this.toPort));
//发送包
socket.send(packet);
if(data.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
package com.Eiso;
public class TalkStudent {
public static void main(String[] args) {
//双线程
new Thread(new TalkSend(9996,"localhost",9998)).start();
new Thread(new TalkReceive(9997,"Teacher")).start();
}
}
package com.Eiso;
public class TalkTeacher {
public static void main(String[] args) {
//双线程
new Thread(new TalkSend(9995,"localhost",9997)).start();
new Thread(new TalkReceive(9998,"Student")).start();
}
}
URL
统一资源定位符
协议(http)+://+IP地址(www.baidu.com)+:+端口号(8080)+/+项目名+/+资源
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//IP
System.out.println(url.getPort());//端口
System.out.println(url.getPath());//文件
System.out.println(url.getFile());//文件全路径
System.out.println(url.getQuery());//参数
//下载地址
url = new URL("http://localhost:8080/test/this.txt");
//连接资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("this.txt");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
/test/this.txt");
//连接资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("this.txt");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}