Day Twenty-One
网络编程
- 网络编程中有两个主要的问题
- 如何准确的定位到网络上的一台或者多台主机
- 找到主机之后如何进行通信
- 网络编程中的要素
IP
如何能获取到IP地址,在Java中有这样的一个类inetAddress,这个类是在java.net.InetAddress这个包下的,这个包下面的都是和网络相关的。在JavaAPI中对这个类有详细的介绍以及如何去使用。这个类有两个子类,Inet4Address和Inet6Address。
这个类里面没有构造器,只有静态方法。
也就是说我们new一个address,它就会把当前这个地址的IP返回出来。
package com.liuHuan.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
System.out.println(InetAddress.getByName("127.0.0.1"));
System.out.println(InetAddress.getByName("localhost"));
System.out.println(InetAddress.getLocalHost());
InetAddress inetAddress = InetAddress.getByName("www.csdn.com");
System.out.println(inetAddress);
System.out.println(inetAddress.getAddress());
System.out.println(inetAddress.getCanonicalHostName());
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
}
}
端口Port
端口表示计算机上的一个程序的进程:
-
不同的进程有不同的端口号!端口号用来区分软件。 -
端口范围 0~65535 -
端口号分为TCP,UDP端口号,每一个分别有65535个端口号,所以总共有65535*2个。 -
如果说我们在TCP下使用了11这个端口号,我们在UDP下也可同时使用11这个端口号。但是单个协议下,端口号不能冲突。 -
端口分类:
- 公有端口 0~1023。尽量不要使用,一般会被内置的进程或者服务器使用
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- 程序注册端口:1024~49151,分配用户或者程序
- Tomcat:8080
- MySQL:3306
- Oracle:1521
- 动态、私有端口:49152~65535
-
常用的DOS命令: netstat -ano
netstat -ano|findstr "5900"
tasklist|findstr "8696"
package com.liuHuan.net;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
}
}
通信协议
协议:就是一个约定。
网络通信协议:速率,传输码率,代码结构,传输控制…
对于网络通信的协议太多了,所以这里我们就要有一个分层的概念。这里对网络通信有OSI和TCP/IP的分层协议。但是最好的,我们现在用的是TCP/IP协议簇,其中有两个比较重要的协议,TCP:用户传输协议;UDP:用户数据报协议。
比较出名的协议就是TCP、IP(网络互联协议)协议
TCP实现聊天
-
客户端
- 连接服务器 Socket
- 发送消息
package com.liuHuan.net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) {
InetAddress serverIP;
Socket socket = null;
OutputStream outPut = null;
try {
serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(serverIP, port);
outPut = socket.getOutputStream();
outPut.write("你好!!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (null != outPut) {
try {
outPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != socket) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
-
服务器
- 建立服务的端口 ServerSocket
- 等待用户的连接 accept
- 接受用户消息
package com.liuHuan.net;
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 inPut = null;
ByteArrayOutputStream outputStream = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
inPut = socket.getInputStream();
outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inPut.read(buffer)) != -1) {
outputStream.write(buffer,0,len);
}
System.out.println(outputStream.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inPut != null) {
try {
inPut.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();
}
}
}
}
}
实现文件上传
服务端:
package com.liuHuan.net;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer02 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("10.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer,0,len);
}
OutputStream outputStream = socket.getOutputStream();
outputStream.write("收完了,88".getBytes());
outputStream.close();
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端
package com.liuHuan.net;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient02 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream clientOutPut = socket.getOutputStream();
FileInputStream picInPut = new FileInputStream(new File("1.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = picInPut.read(buffer)) != -1) {
clientOutPut.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2)) != -1) {
outputStream.write(buffer2,0,len2);
}
System.out.println(outputStream.toString());
outputStream.close();
inputStream.close();
picInPut.close();
clientOutPut.close();
socket.close();
}
}
UDP消息发送
可以理解为发短信一样,双方不用建立连接,只需要知道对方的地址就可以。
客户端
package com.liuHuan.net;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpClient01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "你好,服务器!";
InetAddress inetAddress = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,inetAddress,port);
socket.send(packet);
socket.close();
}
}
服务端
package com.liuHuan.net;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;
public class UdpServer01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostName());
System.out.println(new String(packet.getData()));
socket.close();
}
}
UDP聊天实现
一个人发,另一个人只能听:
发送方:
package com.liuHuan.net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
public class UdpSender01 {
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();
DatagramPacket packet = new DatagramPacket(
data.getBytes()
, 0
, data.getBytes().length
, new InetSocketAddress("localhost", 6666)
);
socket.send(packet);
if (data.equals("bye")) {
break;
}
}
socket.close();
}
}
接收方:
package com.liuHuan.net;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceive01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveDate = new String(data, 0, data.length);
System.out.println(receiveDate);
if (receiveDate.startsWith("bye")) {
break;
}
}
socket.close();
}
}
UDP多线程在线咨询
双方既能发送又能收到:
路人甲要和宋兵乙进行实时性的交流。
发送方法:
package com.liuHuan.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable{
DatagramSocket socket;
BufferedReader reader;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) throws Exception {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while (true) {
try {
String data = reader.readLine();
DatagramPacket packet = new DatagramPacket(
data.getBytes()
, 0
, data.getBytes().length
, new InetSocketAddress(this.toIP, this.toPort)
);
socket.send(packet);
if (data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
接收方法:
package com.liuHuan.net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
DatagramSocket socket;
private int port;
private String fromName;
public TalkReceive(int port,String fromName) {
this.port = port;
this.fromName = fromName;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveDate = new String(data, 0, data.length);
System.out.println(fromName + ":" + receiveDate);
if (receiveDate.startsWith("bye")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
路人甲
package com.liuHuan.net;
public class TalkPeople {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(6666,"localhost",3333)).start();
new Thread(new TalkReceive(8888,"路人甲")).start();
}
}
宋兵乙
package com.liuHuan.net;
public class TalkPeople2 {
public static void main(String[] args) throws Exception {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(3333,"宋兵乙")).start();
}
}
URL下载网络资源
package com.liuHuan.net;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class UrlDown {
public static void main(String[] args) throws IOException {
URL url = new URL("");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,len);
}
outputStream.close();
inputStream.close();
urlConnection.disconnect();
}
}
|