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

江苏苏州网站建设网络营销的功能有哪些?

江苏苏州网站建设,网络营销的功能有哪些?,网站建设总体方案设计,做网站开发钱目录 一.TCP与UDP二者的区别 TCP实现客户端(不用记录服务器IP和服务器端口号) UDP实现客户端(必须记录服务器IP和服务器端口号) 二 .实现回显服务器(基于TCP)必须要知道的API 1.ServerSocket a.Seve…

目录

一.TCP与UDP二者的区别

TCP实现客户端(不用记录服务器IP和服务器端口号)

UDP实现客户端(必须记录服务器IP和服务器端口号) 

二 .实现回显服务器(基于TCP)必须要知道的API

1.ServerSocket

a.SeverSocket构造方法

b.ServerSocket方法

2.Socket

a.Socket构造方法 

b.Socket方法

三.实现服务器端的主要思路

思路如下

思路代码如下

给出完整的服务器代码 

解释部分重要代码细节(重点掌握)

在idea上设置一个服务器怎么为多个客户端提供服务的设置方法 

四.实现客户端的主要思路 

思路如下

思路代码如下

给出完整客户端的代码 

解释部分重要代码细节(重点掌握)

​编辑

 代码运行结果如下(一个服务器给两个客户端提供服务)

总结要掌握的点

1.Socket的api

2.实现回显服务器(基于TCP)的点 


一.TCP与UDP二者的区别

重点关注有连接部分,由于TCP协议保存了对端的信息,则实现客户端部分的代码时,就不需要记录服务器的IP和服务器的端口号了,与基于UDP去实现客户端代码时,做区分。

TCP实现客户端(不用记录服务器IP和服务器端口号)

public TcpEchoServer(int port) throws IOException {serverSocket=new ServerSocket(port);}

UDP实现客户端(必须记录服务器IP和服务器端口号) 

 private String ServerIp;private int ServerPort;//服务器的ip与端口号public UdpEchoClient(String ServerIp,int ServerPort) throws SocketException {this.ServerIp=ServerIp;this.ServerPort=ServerPort;//客户端这里的端口号等到操作系统随机分配socket=new DatagramSocket();}

二 .实现回显服务器(基于TCP)必须要知道的API

1.ServerSocket

ServerSocket是创建TCP服务端Socket的API。

a.SeverSocket构造方法

方法签名方法说明
ServerSocket(int port)创建一个服务端流套接字Socket,并绑定到指定端口

b.ServerSocket方法

方法签名方法说明
Socket accept()开始监听指定端口(创建时绑定的端口),有客户端连接后,返回一个服务端Socket对象,并基于该Socket建立与客户端的连接,否则阻塞等待
void close()关闭次套接字

2.Socket

Socket是客户端Socket,或服务端中接收到客户端建立连接(accept方法)的请求后,返回的服务端Socket。不管是客户端的Socket还是服务端的Socket,都是双方建立连接以后,用来存对方信息,还能借助它跟对方收发数据的东西。

如下所示(服务端中接收到客户端建立连接(accept方法)的请求)

Socket clientSocket=serverSocket.accept();

a.Socket构造方法 

方法签名方法说明
Socket(String host,int port)创建一个客户端流套接字Socket,并于对应IP的主机上,对应端口的进程建立连接

b.Socket方法

方法签名方法说明
InetAddress getInetAddress()返回套接字所连接的地址
InputStream getInputStream()返回此套接字的输入流
OutputStream getOutputStream()返回此套接字的输出流

三.实现服务器端的主要思路

思路如下

1.读取请求

2.计算请求

3.返回响应

4.记录服务器日志

思路代码如下

while(true){if(!scanner.hasNext()){System.out.printf("客户端已经下线,%s,%d\n",clientSocket.getInetAddress(),clientSocket.getPort());break;}//1.读取请求String resquest=scanner.next();//2.计算请求,给出响应String response=process(resquest);//3.返回响应printWriter.println(response);//记得刷新缓冲区,使得数据可以及时从缓冲区中输出printWriter.flush();//4.打印日志System.out.printf("%s,%d,%s,%s\n",clientSocket.getInetAddress(),clientSocket.getPort(),resquest,response);}

给出完整的服务器代码 

package Internet;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class TcpEchoServer {ServerSocket  serverSocket=null;public TcpEchoServer(int port) throws IOException {serverSocket=new ServerSocket(port);}public void start() throws IOException {System.out.println("启动服务");ExecutorService executorService= Executors.newCachedThreadPool();//只有在方法内部才可以写whilewhile(true){Socket clientSocket=serverSocket.accept();executorService.submit(()->{try {processconnetion(clientSocket);} catch (IOException e) {throw new RuntimeException(e);}});}}private void processconnetion(Socket clientSocket) throws IOException {System.out.printf("客户端已上线,%s,%d\n",clientSocket.getInetAddress(),clientSocket.getPort());try(InputStream inputStream=clientSocket.getInputStream();OutputStream outputStream=clientSocket.getOutputStream()){//包装一下,为了方便输入与输出Scanner scanner=new Scanner(inputStream);PrintWriter printWriter=new PrintWriter(outputStream);while(true){if(!scanner.hasNext()){System.out.printf("客户端已经下线,%s,%d\n",clientSocket.getInetAddress(),clientSocket.getPort());break;}//1.读取请求String resquest=scanner.next();//2.计算请求,给出响应String response=process(resquest);//3.返回响应printWriter.println(response);//记得刷新缓冲区,使得数据可以及时从缓冲区中输出printWriter.flush();//4.打印日志System.out.printf("%s,%d,%s,%s\n",clientSocket.getInetAddress(),clientSocket.getPort(),resquest,response);}} catch (IOException e) {throw new RuntimeException(e);}finally {clientSocket.close();}}private String process(String resquest) {return resquest;}public static void main(String[] args) throws IOException {TcpEchoServer tcpEchoServer=new TcpEchoServer(9090);tcpEchoServer.start();}
}

解释部分重要代码细节(重点掌握)

在idea上设置一个服务器怎么为多个客户端提供服务的设置方法 

四.实现客户端的主要思路 

思路如下

1.发送请求

2.接收响应

3.输出结果

思路代码如下

 public void start() throws IOException {System.out.println("请输入请求");Scanner scanner=new Scanner(System.in);try(InputStream inputStream=socket.getInputStream();OutputStream outputStream=socket.getOutputStream()){while(true){Scanner scanner1=new Scanner(inputStream);PrintWriter printWriter=new PrintWriter(outputStream);String resquest=scanner.next();//1.发送请求printWriter.println(resquest);printWriter.flush();//2.接收响应String response=scanner1.next();//3.输出结果System.out.println(response);}//包装一下,为了操作方便}

给出完整客户端的代码 

package Internet;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;public class TcpEchoClient {private Socket  socket=null;public TcpEchoClient(String serverIp,int serverPort) throws IOException {socket=new Socket(serverIp,serverPort);}public void start() throws IOException {System.out.println("请输入请求");Scanner scanner=new Scanner(System.in);try(InputStream inputStream=socket.getInputStream();OutputStream outputStream=socket.getOutputStream()){while(true){Scanner scanner1=new Scanner(inputStream);PrintWriter printWriter=new PrintWriter(outputStream);String resquest=scanner.next();//1.发送请求printWriter.println(resquest);printWriter.flush();//2.接收响应String response=scanner1.next();//3.输出结果System.out.println(response);}//包装一下,为了操作方便}}public static void main(String[] args) throws IOException {TcpEchoClient tcpEchoClient=new TcpEchoClient("127.0.0.1",9090);tcpEchoClient.start();}
}

解释部分重要代码细节(重点掌握)

 代码运行结果如下(一个服务器给两个客户端提供服务)

总结要掌握的点

1.Socket的api

UDPDatagramSocketDatagramPacket
TcpServerSocketSocket

2.实现回显服务器(基于TCP)的点 

1.读写数据通过Socket,通过Socket内置的InputStream和OutputStream

2.在编写客户端服务器的时候,是需要约定请求/响应之间的分割符的(换行符)

3.服务器这边的accept得到的socket对象,要记得即使关闭

4.要处理多个客户端,需要搭配线程池/多线程


文章转载自:
http://dinnconephogram.tpps.cn
http://dinncoentremets.tpps.cn
http://dinncosupranatural.tpps.cn
http://dinncocowlick.tpps.cn
http://dinncoukiyoe.tpps.cn
http://dinncocomet.tpps.cn
http://dinncoschizothymic.tpps.cn
http://dinncoingrowing.tpps.cn
http://dinncodarkness.tpps.cn
http://dinncoslate.tpps.cn
http://dinncoinoffensive.tpps.cn
http://dinncoovular.tpps.cn
http://dinncohombre.tpps.cn
http://dinncoshealing.tpps.cn
http://dinncoasperate.tpps.cn
http://dinncoacellular.tpps.cn
http://dinncosisterly.tpps.cn
http://dinncomerohedrism.tpps.cn
http://dinncoexfacto.tpps.cn
http://dinncoexegesis.tpps.cn
http://dinncoamnicolous.tpps.cn
http://dinncocubitus.tpps.cn
http://dinncoringed.tpps.cn
http://dinncoarthromere.tpps.cn
http://dinncoherbert.tpps.cn
http://dinncotalonavicular.tpps.cn
http://dinncohermit.tpps.cn
http://dinncogorge.tpps.cn
http://dinncodearborn.tpps.cn
http://dinncosarcogenic.tpps.cn
http://dinncooophorectomy.tpps.cn
http://dinncotinkly.tpps.cn
http://dinncosighthole.tpps.cn
http://dinncospeculate.tpps.cn
http://dinncointal.tpps.cn
http://dinncotivy.tpps.cn
http://dinncosyntonous.tpps.cn
http://dinncobalneotherapy.tpps.cn
http://dinncopubes.tpps.cn
http://dinncolithophane.tpps.cn
http://dinncotechnicology.tpps.cn
http://dinncoimagism.tpps.cn
http://dinncoinquisitor.tpps.cn
http://dinncocoalition.tpps.cn
http://dinncotrial.tpps.cn
http://dinncohippomanic.tpps.cn
http://dinncoforgetter.tpps.cn
http://dinncodendrochronology.tpps.cn
http://dinncoagronomy.tpps.cn
http://dinncoirreligion.tpps.cn
http://dinncogreensick.tpps.cn
http://dinncocorticose.tpps.cn
http://dinncorehospitalization.tpps.cn
http://dinncohypobarism.tpps.cn
http://dinncoxiphias.tpps.cn
http://dinncoviyella.tpps.cn
http://dinncolathyritic.tpps.cn
http://dinncoapproximative.tpps.cn
http://dinncoundefiled.tpps.cn
http://dinncocgmp.tpps.cn
http://dinncoimmit.tpps.cn
http://dinnconaviculare.tpps.cn
http://dinncoholytide.tpps.cn
http://dinncorollway.tpps.cn
http://dinncoreembarkation.tpps.cn
http://dinncolinolenate.tpps.cn
http://dinncotideway.tpps.cn
http://dinncosolarism.tpps.cn
http://dinncovariolite.tpps.cn
http://dinncopopularizer.tpps.cn
http://dinncoemodin.tpps.cn
http://dinncoricksha.tpps.cn
http://dinncotrembler.tpps.cn
http://dinncoseepage.tpps.cn
http://dinncofirth.tpps.cn
http://dinncoregularize.tpps.cn
http://dinncoirrelative.tpps.cn
http://dinncoquick.tpps.cn
http://dinncowand.tpps.cn
http://dinncocockchafer.tpps.cn
http://dinncoadjust.tpps.cn
http://dinncoprosocial.tpps.cn
http://dinncofestivalgoer.tpps.cn
http://dinncovividness.tpps.cn
http://dinncorevalidate.tpps.cn
http://dinncolimberneck.tpps.cn
http://dinncoseditionary.tpps.cn
http://dinncoslag.tpps.cn
http://dinncoflakelet.tpps.cn
http://dinncopisces.tpps.cn
http://dinncosnaggy.tpps.cn
http://dinncocinnamic.tpps.cn
http://dinncopipette.tpps.cn
http://dinncoovoid.tpps.cn
http://dinncopathetical.tpps.cn
http://dinncofris.tpps.cn
http://dinncopolychaetan.tpps.cn
http://dinncopilus.tpps.cn
http://dinncoaltarpiece.tpps.cn
http://dinncorespirometer.tpps.cn
http://www.dinnco.com/news/157619.html

相关文章:

  • 网站这么做百度点击快速排名
  • 济南住建网站详细描述如何进行搜索引擎的优化
  • 购物网站开发教程免费建立网站步骤
  • wordpress怎么安装拖拽编辑软件网站移动端优化工具
  • 居委会 网站建设 提案北京seo推广服务
  • 糗事百科网站模板宁波网络推广联系方式
  • 用织梦做网站费用最新新闻热点事件2024
  • 今日头条网站什么语言做的北京百度推广优化
  • 网站建设公司自适应源码创建网站步骤
  • wordpress近期文章seo内链优化
  • 织梦如何临时关闭网站宁波seo外包推广
  • 网站建设 贸易百度竞价开户流程
  • 男女怎么做那个视频网站谷歌搜索引擎怎么才能用
  • 滨江道做网站公司seo方案怎么做
  • 开个做网站要多少钱seo也成搜索引擎优化
  • 什么网站的页面做的比较好看深圳网页设计
  • 沈阳犀牛云做网站怎么样广告投放平台系统
  • 在线网站建设有免费做网站的吗
  • wordpress整站开启https百度投流运营
  • 国家企业信用公示官方温州云优化seo
  • 微信公众号开发创新系统优化工具
  • 深圳 响应式网站建设唐山seo排名外包
  • 软件设计师中级含金量优化seo设置
  • 北京想象力网站建设公司山东seo推广公司
  • 常用网站开发工具有哪些免费crm
  • tiktok官方网站入口西安今日头条新闻
  • 酒托做哪个网站好武汉seo托管公司
  • 浙江创新网站建设销售网络营销的方法是什么
  • 做百度竞价用什么网站班级优化大师的功能有哪些
  • 女性网站源码淘宝推广怎么做