网络编程
通信的要素
-
ip
-
端口号
规则:网络通信的协议
获取本地IP
再这个类下InetAddress
public static void main(String[] args) throws UnknownHostException {
InetAddress byName1 = InetAddress.getByName("127.0.0.1");
System.out.println(byName1);
InetAddress byName3 = InetAddress.getLocalHost();
System.out.println(byName3);
InetAddress byName2 = InetAddress.getByName("www.baidu.com");
System.out.println(byName2);
}
端口
端口表示计算机一个程序的进程。
不同的进程有不同的端口号,用来区分软件。
有TCP、UDP端口,每个都有65535个
端口分类:公有端口0~1023
HTTP:80
HTTPS:443
FTP:21
Tomcat:8080
MySQL:3306
Oracle:1521
通信协议
TCP/UDP协议簇
两个重要协议:TCP:用户传输协议
UDP:用户数据报协议
TCU PDP对比
TCP:打电话
需要连接、稳定
三次握手、四次挥手
三次握手
A:在吗?
B:在的
A:你是个好人
四次分手
A:我要走了
B:你真的要走了吗
B:你真的真的要走了吗
A:我真的真的要走了
客户端、服务端
传输完成、释放连接、效率低
UDP:发短信
不需要连接、不稳定
不管对方有没有准备好都进行发送
TCP实现文件传输
//客户端,发送文件
public class TcpClientDemo {
public static void main(String[] args) throws Exception{
//创建连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//创建一个输出流
OutputStream os = socket.getOutputStream();
//读取文件
FileInputStream fis = new FileInputStream(new File("header.png"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//关闭资源
fis.close();
os.close();
socket.close();
}
}
//服务端
public class TcpServerDemo {
public static void main(String[] args) throws Exception {
//创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//监听客户端连接
Socket socket = serverSocket.accept();//阻塞监听,一直等待客户端连接
//获取输入流
InputStream is = socket.getInputStream();
//文件传输
FileOutputStream fos = new FileOutputStream(new File("haha.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
UDP实现简单的聊天功能
public class UdpReceiveDemo {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
byte[] container = new byte[10];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(s);
if(s.equals("bye")){
break;
}
}
socket.close();
}
}
public class UdpSenderDemo {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//从控制台读取数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket localhost = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("localhost",6666));
socket.send(localhost);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
多线程实现聊天
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[] bytes = data.getBytes();
DatagramPacket localhost = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIp, this.toPort));
socket.send(localhost);
if (data.equals("bye")){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
byte[] container = new byte[10];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(msgFrom + ":" + s);
if(s.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) {
//开启线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
public class TalkTeacker {
public static void main(String[] args) {
//开启线程
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
通过URL进行下载
public class UrlDown {
public static void main(String[] args) throws Exception{
//下载地址
URL url = new URL("https://m801.music.126.net/20230421115728/0fde0139eb4e53bad5e12dfa1ba164c2/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/17232400523/d00f/506f/f6ea/208a2d3445b710176338c131d631002b.m4a");
//连接到资源
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream stream = new FileOutputStream("td.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1){
stream.write(buffer,0,len);
}
stream.close();
inputStream.close();
urlConnection.disconnect();
}
}
标签:String,编程,网络,close,new,byte,public,socket From: https://www.cnblogs.com/aseed/p/17339845.html