ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java--网络编程

2021-11-20 19:05:12  阅读:141  来源: 互联网

标签:Java socket -- 编程 buffer InetAddress close new byte


文章目录

现在是互联网时代,我们面临如何和网络打交道。而在Java中的网络编程就是针对于网络进行编程的。
一般会使用InetAddress和Socket这两个类。

网络编程中的两个主要问题及其要素

  1. 如何准确地定位网络上一台或多台主机;定位主机上的特定的应用。这个问题对应元素是:IP和端口号
  2. 找到主机后如何可靠高效地进行数据传输。这个问题对应元素是:网络通信协议。

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();
     }
 }

后续更新,敬请期待!

标签:Java,socket,--,编程,buffer,InetAddress,close,new,byte
来源: https://blog.csdn.net/weixin_42075274/article/details/121443179

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有