文章目录
现在是互联网时代,我们面临如何和网络打交道。而在Java中的网络编程就是针对于网络进行编程的。
一般会使用InetAddress和Socket这两个类。
网络编程中的两个主要问题及其要素
- 如何准确地定位网络上一台或多台主机;定位主机上的特定的应用。这个问题对应元素是:IP和端口号
- 找到主机后如何可靠高效地进行数据传输。这个问题对应元素是:网络通信协议。
InetAddress类的使用
try {
InetAddress inet1 = InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
InetAddress inet2 = InetAddress.getByName("www.atguigu.com");
System.out.println(inet2);
InetAddress inet3 = InetAddress.getByName("127.0.0.1");
System.out.println(inet3);
//获取本地ip
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);
//getHostName():获取主机名
System.out.println(inet2.getHostName());
//getHostAddress():获取主机ip地址
System.out.println(inet2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
TCP通信
三次握手
四次挥手
TCP通信实现
//客户端,实际应该是写成一个类,这里为了测试方便,是写在JUnit单元的一个测试方法
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
//接收来自于服务器端的数据,并显示到控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bufferr = new byte[20];
int len1;
while((len1 = is.read(buffer)) != -1){
baos.write(buffer,0,len1);
}
System.out.println(baos.toString());
fis.close();
os.close();
socket.close();
baos.close();
}
//服务器,实际应该是写成一个类,这里为了测试方便,是写在JUnit单元的一个测试方法
public void server() throws IOException {
ServerSocket ss = new ServerSocket(9090);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("beauty2.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();
ss.close();
os.close();
}
UDP通信实现(需要使用DatagramSocket 和DatagramPacket )
//发送端
//实际应该是写成一个类,这里为了测试方便,是写在JUnit单元的一个测试方法
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的导弹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
//接收端
//实际应该是写成一个类,这里为了测试方便,是写在JUnit单元的一个测试方法
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
URL的使用(实现使用URL来下载网络资源)
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcrawl.nosdn.127.net%2F9e9f8b8595d2d7e8767cdf0191bfc76f.jpg&refer=http%3A%2F%2Fcrawl.nosdn.127.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639997746&t=c6ea2954969b2011e0da4314691ecc39");//保证是一个有效的url
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("photo3.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
后续更新,敬请期待!