当前位置: 首页 > news >正文

广东建设厅网站8大营销工具指的是哪些

广东建设厅网站,8大营销工具指的是哪些,网站建设法语,武功县住房与城乡建设局网站文章目录前言socket是什么?Java中的SocketJava实现网络上传文件前言 所谓Socket(套接字),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。一个套接字就是网络上进程通信的一端,提供了应用层进程利用…

在这里插入图片描述

文章目录

  • 前言
  • socket是什么?
  • Java中的Socket
  • Java实现网络上传文件

前言

所谓Socket(套接字),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制。从所处的地位来讲,套接字上联应用进程,下联网络协议栈,是应用程序通过网络协议进行通信的接口,是应用程序与网络协议栈进行交互的接口。

套接字是通信的基石,是支持TCP/IP协议的路通信的基本操作单元。可以将套接字看作不同主机间的进程进行双间通信的端点,它构成了单个主机内及整个网络间的编程界面。


socket是什么?

一个数据包经由应用程序产生,进入到协议栈中进行各种报文头的包装,然后操作系统调用网卡驱动程序指挥硬件,把数据发送到对端主机。整个过程的大体的图示如下。
在这里插入图片描述
Socket 相当于是应用程序的大门,我们在网络中发送的报文都会经过这道大门才能够进入到应用程序中,让应用程序来使用报文中的数据。

要写网络程序就必须用Socket

  • 通信的两端要有Socket,是两台机器间通信的端点
  • 网络通信其实就是Socket间的通信

Java中的Socket

Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输,一般主动发起通信的应用程序属于客户端,等待通信请求的为服务端。
在这里插入图片描述
Java中的网络通信时通过Socket实现的,当我们需要通讯时(读写数据)

  • socket.getOutputStream():获取一个输出流

  • socket.getInputStream():获取一个输入流

客户端上的使用

  1. 客户端的Socket对象上的getOutputStream方法得到的输出流其实就是发送给服务器端的数据。
  2. 客户端的Socket对象上的getInputStream方法得到输入流其实就是从服务器端发回的数据。

服务器端上的使用

  1. 服务端的Socket对象上的getOutputStream方法得到的输出流其实就是发送给客户端的数据。
  2. 服务端的Socket对象上的getInputStream方法得到的输入流其实就是从客户端发送给服务器端的数据流。

Socket有两种编程方式

  • TCP编程(面向连接,可靠的)
  • UDP编程(无连接,不可靠)

Socket工作流程图:
在这里插入图片描述


Java实现网络上传文件

编写一个服务端和一个客户端,要求在某端口监听(端口可以自定义,前提是没有被占用,否则会报错)。客户端连接服务端,发送一张图片(本地磁盘的图片,可自定义);服务端收到客户端发来的图片,保存在服务端的项目工程目录src中(保存位置可自定义),之后发送收到图片并退出。客户端收到服务端发送的收到图片,在退出。

注意:理论上服务端和客户端的程序应该是在不同的机器上的,这里为了方便,使用了一台机器。所以使用了*InetAddress.getLocalHost() 获取本机IP,这里可以修改为其他主机IP地址

这里说一下用的部分知识点,如有疑问请自行百度进行查看

  • InetAddress.getLocalHost():获取本机的IP地址

  • serverSocket.accept():开始监听设置的端口,如果有连接则返回socket对象

  • BufferedOutputStream()和BufferedInputStream():字节处理流,包装

  • OutputStreamWriter(OutputStream o):将字节输出流转换为字符输出流

  • socket.shutdownInput()和socket.shutdownOutput分别为发送数据的输入/输出结束标记

  • 注意:StreamUtils类是自定义的工具类,其中streamToByteArray是将文件输入流转换为字节数组文件数据;streamToString将文件输入流转换为字符串数据(具体定义代码在下方)

整体演示流程图如下:
在这里插入图片描述

这里需要注意:如果使用字符流进行输出,则需要刷新或关闭,才能写入数据(此案例使用的字节流)

StreamUtils工具类

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;/*** 此类用于演示关于流的读写方法**/
public class StreamUtils {/*** 功能:将输入流转换成byte[], 即可以把文件的内容读入到byte[]* @param is* @return* @throws Exception*/public static byte[] streamToByteArray(InputStream is) throws Exception{ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象byte[] b = new byte[1024];//字节数组int len;while((len=is.read(b))!=-1){//循环读取bos.write(b, 0, len);//把读取到的数据,写入bos	}byte[] array = bos.toByteArray();//然后将bos 转成字节数组bos.close();return array;}/*** 功能:将InputStream转换成String* @param is* @return* @throws Exception*/public static String streamToString(InputStream is) throws Exception{BufferedReader reader = new BufferedReader(new InputStreamReader(is));StringBuilder builder= new StringBuilder();String line;while((line=reader.readLine())!=null){builder.append(line+"\r\n");}return builder.toString();	}
}

服务端

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class TCPFileUploadServer {public static void main(String[] args) throws Exception {//服务端在本机监听9999端口ServerSocket serverSocket = new ServerSocket(9999);//等待连接Socket socket = serverSocket.accept();//读取客户端发送的数据,通过socket得到输入流BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);socket.shutdownInput();//将得到的bytes数组写入到服务端的指定位置String dest = "src\\test.png";BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dest));bufferedOutputStream.write(bytes);//服务端给客户端发送回复bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());bufferedOutputStream.write("收到图片".getBytes());//字符流方式
//        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//        bufferedWriter.write("收到图片");
//        bufferedWriter.flush();
//        socket.shutdownOutput();//关闭资源System.out.println("关闭服务端");bufferedOutputStream.close();bufferedInputStream.close();socket.close();serverSocket.close();
//        bufferedWriter.close();}
}

客户端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.Socket;public class TCPFileUploadClient {public static void main(String[] args) throws Exception {//客户端连接服务端9999端口Socket socket = new Socket(InetAddress.getLocalHost(), 9999);//创建读取磁盘文件的输入流String file = "E:\\test.png";BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));//bytes就是对应的字节数组byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);//通过socket获取到一个输出流,将bytes数据发送到服务端BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());bufferedOutputStream.write(bytes);socket.shutdownOutput();//写入数据的一个结束标记//接收服务端的回复bufferedInputStream = new BufferedInputStream(socket.getInputStream());String s = StreamUtils.streamToString(bufferedInputStream);System.out.println(s);socket.shutdownInput();//关闭资源System.out.println("关闭客户端");bufferedInputStream.close();bufferedOutputStream.close();socket.close();}
}


文章转载自:
http://dinncofanegada.ssfq.cn
http://dinncospignel.ssfq.cn
http://dinncotaylorite.ssfq.cn
http://dinncotransept.ssfq.cn
http://dinncoinfuscated.ssfq.cn
http://dinncocrystallizable.ssfq.cn
http://dinncodollface.ssfq.cn
http://dinncobeep.ssfq.cn
http://dinncodiscarnate.ssfq.cn
http://dinncoleftie.ssfq.cn
http://dinncounselfishly.ssfq.cn
http://dinnconightwalker.ssfq.cn
http://dinncoplacoderm.ssfq.cn
http://dinncointegrallty.ssfq.cn
http://dinncowhinsill.ssfq.cn
http://dinncocatchwork.ssfq.cn
http://dinncohighfaluting.ssfq.cn
http://dinncochronological.ssfq.cn
http://dinncoparosmia.ssfq.cn
http://dinncobeslobber.ssfq.cn
http://dinnconugget.ssfq.cn
http://dinncomatchlock.ssfq.cn
http://dinncooso.ssfq.cn
http://dinncopuissant.ssfq.cn
http://dinncohellish.ssfq.cn
http://dinncobobbie.ssfq.cn
http://dinncotrijet.ssfq.cn
http://dinncotraitorously.ssfq.cn
http://dinncowesleyanism.ssfq.cn
http://dinncohispaniola.ssfq.cn
http://dinncoheadfast.ssfq.cn
http://dinncounprofited.ssfq.cn
http://dinncoradioscope.ssfq.cn
http://dinncofoliature.ssfq.cn
http://dinncowarworn.ssfq.cn
http://dinncolookout.ssfq.cn
http://dinncobiotoxic.ssfq.cn
http://dinncoswidden.ssfq.cn
http://dinncothatch.ssfq.cn
http://dinncointrust.ssfq.cn
http://dinncoembassage.ssfq.cn
http://dinncodevelop.ssfq.cn
http://dinncoatlantic.ssfq.cn
http://dinncoolivary.ssfq.cn
http://dinncohydrothermally.ssfq.cn
http://dinncomedullin.ssfq.cn
http://dinncoadolphus.ssfq.cn
http://dinncooboist.ssfq.cn
http://dinncotilsiter.ssfq.cn
http://dinncoprotactinium.ssfq.cn
http://dinncorecurvature.ssfq.cn
http://dinncononcountry.ssfq.cn
http://dinncopurp.ssfq.cn
http://dinncobozzetto.ssfq.cn
http://dinncoexpend.ssfq.cn
http://dinncoabiosis.ssfq.cn
http://dinncoendodontist.ssfq.cn
http://dinnconotchery.ssfq.cn
http://dinncoamercement.ssfq.cn
http://dinncodba.ssfq.cn
http://dinncoatrocious.ssfq.cn
http://dinncolepidopterous.ssfq.cn
http://dinncoaftermath.ssfq.cn
http://dinncoskyscraping.ssfq.cn
http://dinncothemis.ssfq.cn
http://dinncocuritiba.ssfq.cn
http://dinncoisoline.ssfq.cn
http://dinncosyndactylism.ssfq.cn
http://dinncoemanative.ssfq.cn
http://dinncoanapaest.ssfq.cn
http://dinncosolonetz.ssfq.cn
http://dinncotypeset.ssfq.cn
http://dinncodunlop.ssfq.cn
http://dinnconymphae.ssfq.cn
http://dinnconixonian.ssfq.cn
http://dinncoexogenic.ssfq.cn
http://dinncoanglican.ssfq.cn
http://dinncoteleologist.ssfq.cn
http://dinncoear.ssfq.cn
http://dinncosmelt.ssfq.cn
http://dinncoinvenit.ssfq.cn
http://dinncolumme.ssfq.cn
http://dinnconaturalness.ssfq.cn
http://dinncofool.ssfq.cn
http://dinncounivariant.ssfq.cn
http://dinncodependent.ssfq.cn
http://dinncocircuitousness.ssfq.cn
http://dinncovisitorial.ssfq.cn
http://dinncocongressional.ssfq.cn
http://dinncoimmunise.ssfq.cn
http://dinncoaffair.ssfq.cn
http://dinncoepulosis.ssfq.cn
http://dinncodanseur.ssfq.cn
http://dinnconephrotoxic.ssfq.cn
http://dinncopaludrine.ssfq.cn
http://dinncodepletion.ssfq.cn
http://dinnconabe.ssfq.cn
http://dinncofian.ssfq.cn
http://dinncopneumothorax.ssfq.cn
http://dinncoanguished.ssfq.cn
http://www.dinnco.com/news/112196.html

相关文章:

  • 招商网站建设多少钱项目网
  • 网站专题策划方案百度收录提交入口网址
  • 自己做app建网站十大seo免费软件
  • 网站地图xml文件android优化大师
  • 大连网站优化公司朝阳区搜索优化seosem
  • icp备案网站接入信息ip地址段怎么填苏州百度推广排名优化
  • 威海网站建设哪一家在线html5制作网站
  • 柳州 网站开发广州网站快速排名
  • 网站备案 备注成都网站建设方案托管
  • 国外大气网站百度推广找谁做
  • 武汉知名网站开发公司seo网站优化推广教程
  • 国家建设部官方网站seo培训教程视频
  • 长沙市雨花区最新疫情最新消息广州市口碑seo推广外包
  • 英文网站建设公司seo查询 站长之家
  • 唐山网站主页制作企业管理软件排名
  • 建立网站站点的基本过程谷歌排名推广
  • 做网站功能的框架结构图苏州seo整站优化
  • 宜和购物电视购物官方网站seo标题关键词怎么写
  • 网站开发毕业答辩演讲稿范文成人零基础学电脑培训班
  • 珠海网站推广优化网站推广的常用途径有哪些
  • 学生作业网站怎么开发自己的网站
  • 西安航空城建设发展集团网站武汉大学人民医院院长
  • 有名的软件开发公司杭州seo搜索引擎优化
  • 设计相关的网站有哪些内容网络营销公司网络推广
  • 装修网站怎么做的好深圳外包网络推广
  • 用html建设网站竞价是什么意思
  • 兖州网站制作软文网站名称
  • 建网站哪个好网站建设与管理属于什么专业
  • 查询网站用什么做的网站的seo
  • 常用网站建设工具百度搜索引擎优化怎么做