导航:首页 > 编程语言 > java长连接短连接

java长连接短连接

发布时间:2023-02-04 21:55:11

java实现长连接转短网址

长连接听起来神秘而已。

只要不关都是长连接啊。

transferSocket=newSocket(transferServerIp,Integer.valueOf(transferServerport));
transferSocket.close()

每次运行的时候生成一个socket,用完了就close就是短连接啊

② java socket 长连接 客户端

首先说长连接和短连接
短连接:在获得tcp连接之后发送数据,然后关闭连接
长连接:获得tcp连接之后,有数据则发送数据,无数据发送则定时发送数据包,保持连接状态.

长短只是相对的

你的需求应该在取得socket连接之后建立输入输出流,在输入流得到相应数据之后就可以关闭连接了

③ 长连接和短连接 java 代码中具体是怎么实现的

你是指微博那种短连接么?
如果是请看下面的,如果不是,额,就当我不存在吧。
其实就是将原有的连接根据一定的算法变为一个定长的字符串,然后保存在db的表中。对于可能出现的重复对应,采取类似于hash中的处理方法来解决。
额,这个方面的文档网上挺多的呀

④ java 如何实现系统消息推送

消息推送方式分为两种:短连接和长连接。也就是客户端与服务器之间的数据传输交互方式不同。
1、短连接又称为轮询,方式为pull。客户端定时向服务器发送请求,询问是否有数据,时间频次可以设置。这种的方式更适合web端使用,用在APP有很多弊端。
2、长连接方式为push,服务器主动向客户端发送数据。市面上移动端产品基本采用这种方式,但是ios和android系统有很大的区别。不论手机上安装了多少个APP,也只有一条长连接,消息最终由苹果服务器进行内置分发。即使用户关闭了APP,依然可以收到消息推送。
实现系统消息推送,就需要比较好的第三方软件就行辅助,极光就是一个不错的选择。极光JPush 是经过考验的大规模 App 推送平台,每天推送消息量级为数百亿条。JPush 提供可视化的 web 端控制台发送通知,统计分析推送效果。 JPush 全面支持 Android, iOS, Winphone 三大手机平台。

⑤ java怎么实现长连接

应该是HTTP连接吧,HTTP连接本身的request/response模式设计就是短链接的,即服务器端响应之后断开连接。
若要做长连接,在没有具体业务的情况下,最基础的就是建立java.net.Socket连接,自己制定协议来控制断开。服务器端的实现老的有标准IO(java.net.*),新的有NIO(java.nio.*)。有具体业务时可以考虑一些成熟的协议来实现,如XMPP等。

⑥ 如何干净的实现Android/Java Socket 长连接通信

JavaSocket通信有很多的时候需要我们不断的学习。方面效率虽然不及C与C++但它以灵活语言优势,为大家广为使用。本文就对在使用java做通信方面程序时候应改注意问题做以说明。

1.长连接、短链接只是针对客户端而言,服务器无所谓长、短;

2.无论同步或者异步通信,发送之后务必要又响应回复,确认收到,负责进行一定范围内重发,例如重发三次;

3.长连接服务器与客户端之间务必需要心跳探测,由客户端主动发起;

4.短连接服务器通用代码:

python">packagecom.biesan.sms.gate.unioncom.communication;
importcom.biesan.commons.Constants;
importcom.biesan.commons.util.CodeUtil;
importcom.biesan.sms.gate.unioncom.data.*;
importcom.biesan.sms.gate.unioncom.util.GateInfo;
importjava.net.*;
importjava.io.*;
importjava.util.*;
importorg.apache.log4j.*;
importspApi.*;
{
//stopflag
privatebooleanunInterrupt=true;
privatebooleanunErr=true;
//privatebooleancloseSocketFlag=false;
//serversocket
privateServerSocketserverSo=null;
//currentsocket
privateSocketso=null
privateOutputStreamoutput=null;
privateInputStreaminput=null;
//gatecommand
privateSGIP_CommandtmpCmd=null;
privateSGIP_Commandcmd=null;
privateBindbind=null;
privateBindRespbindResp=null;
//privateUnbinnBind=null;
privateUnbindRespunBindResp=null;
=true;
LoggerunioncomLog=Logger.getLogger(Unioncom
Deliver.class.getName());
publicUnioncomDeliver(){
}
publicvoidrun(){
unioncomLog.info("Start...");
while(unInterrupt){
this.initServer();
this.startServices();
while(this.unAcceptErrorFlag){
try{
//接受连接请求
unioncomLog.info("beforeacceptconnection!.......
FreeMemroy:"+Runtime.getRuntime().freeMemory());
this.acceptConnection();
unioncomLog.info("afteracceptconnection!.......
FreeMemroy:"+Runtime.getRuntime().freeMemory());
while(unErr){
cmd=newCommand();
unioncomLog.info("beforereadcommandfromstream
...........FreeMemroy:"+Runtime.getRuntime().
freeMemory());
tmpCmd=cmd.read(input);
unioncomLog.info("afterreadcommandfromstream"+
getCommandString(cmd.getCommandID())+"FreeMemroy:"+
Runtime.getRuntime().freeMemory());
if(tmpCmd==null){
unErr=false;
break;
}
switch(cmd.getCommandID()){
//biadreadycommunication
caseSGIP_Command.ID_SGIP_BIND:{
this.dealBind();
break;
}//exitbind
caseSGIP_Command.ID_SGIP_UNBIND:{
this.dealUnBind();
unioncomLog.info("afterunbindconnection!.......
FreeMemroy:"+Runtime.getRuntime().freeMemory());
break;
}//deliver
....
default://错误的命令
break;
}//switch
}//while(unErr)
}catch(Exceptione){
unioncomLog.error("UnioncomRecvServiceError"
+e.getMessage());
}finally{
if(this.so!=null){
this.closeSocket();
}
this.unErr=true;
}
}//while(this.unAcceptErrorFlag)
try{
this.closeServerSocket();
sleep(200);//sleep
}catch(InterruptedExceptionie){
}
}//while(unInterrupt)
}
privateStringgetCommandString(intcmd){
switch(cmd){
//biadreadycommunication
caseSGIP_Command.ID_SGIP_BIND:{
return"BINDCOMMAND";
}//exitbind
caseSGIP_Command.ID_SGIP_UNBIND:{
return"UNBINDCOMMAND";
}//deliver
case...
default:
return"UNKNOWNCOMMAND";
}
}
privatevoiddealBind(){
try{
bind=newBind(tmpCmd);
if(bind.readbody()!=0){
unioncomLog.warn("ReadBinderror");
this.unErr=false;
}
bindResp=newBindResp(tmpCmd.getMsgHead());
bindResp.SetResult(0);
bindResp.write(output);
unioncomLog.debug("Bindsuccess!");
}catch(Exceptione){
unioncomLog.error("DelaUnionRecvBindError!"+
e.getMessage());
this.unErr=false;
}
}
privatevoiddealUnBind(){
try{
//unBind=(Unbind)tmpCmd;
unBindResp=newUnbindResp(tmpCmd.getMsgHead());
unBindResp.write(output);
unioncomLog.debug("UnBindsuccess!");
}catch(Exceptione){
unioncomLog.warn("Unbinderror!"+e.getMessage());
}
this.unErr=false;
}
privatevoidstartServices(){
booleanunStartServices=true;
while(unStartServices){
try{
serverSo=newServerSocket(ugInfo.getLocalServerPort(),5,
InetAddress.getByName(ugInfo.getLocalIpAdd()));
//serverSo.setSoTimeout(60000);
unStartServices=false;
unioncomLog.info("CreateunionrecvsocketOk!");
}catch(IOExceptione){
unioncomLog.warn("Createunionrecvsocketerror!"
+e.getMessage());
unStartServices=true;
UnioncomSubmit.thrSlp(3000);
}
}
}
privatevoidacceptConnection(){
//Accept失败
try{
so=serverSo.accept();
so.setSoTimeout(10000);
}catch(Exceptione){
unioncomLog.warn("AcceptError!"+e.getMessage());
this.closeServerSocket();
this.unAcceptErrorFlag=false;
this.unErr=false;
}
//Accept成功
try{
input=so.getInputStream();
output=so.getOutputStream();
}catch(IOExceptione){
unioncomLog.warn("GetI/OstreamError!"+e.getMessage());
this.closeService();
this.unAcceptErrorFlag=false;
this.unErr=false;
}
}
privatevoidcloseSocket(){
try{
so.close();
unioncomLog.info("SocketCloseSuccess!!!");
}catch(Exceptione){
unioncomLog.error("SocketCloseFailure!!!"+e.getMessage());
}
}
privatevoidcloseServerSocket(){
try{
serverSo.close();
unioncomLog.info("ServerSocketCloseSuccess!!!");
}catch(Exceptione){
unioncomLog
.error("ServerSocketCloseFailure!!!"+e.getMessage());
}
}
privatevoidcloseService(){
this.closeSocket();
this.closeServerSocket();
}
privatevoidinitServer(){
this.bind=null;
this.bindResp=null;
//this.unBind=null;
this.unBindResp=null;
this.tmpCmd=null;
this.cmd=null;
this.serverSo=null;
this.so=null;
this.output=null;
this.input=null;
this.unErr=true;
//this.closeSocketFlag=false;
unioncomLog.info("Memory***==="
+java.lang.Runtime.getRuntime().freeMemory());
}
(){
this.unInterrupt=false;
unioncomLog.info("Requreinterrupt!!!");
}

(intmsgCoding,byte[]msgContent){
StringdeliverContent=null;
try{
if(msgContent!=null){
if(msgCoding==8){//处理ucs32编码
deliverContent=newString(msgContent,
"UnicodeBigUnmarked");
}elseif(msgCoding==0){//处理ASCII编码
deliverContent=newString(msgContent,"ASCII");
}elseif(msgCoding==4){//处理binary编码
deliverContent=newString(msgContent);
}elseif(msgCoding==15){//处理GBK编码
deliverContent=newString(msgContent,"GBK");
//处理DELIVER数据包的短信息ID
}else{
unioncomLog.error("编码格式错误!");
return"";
}
}else
return"";
returndeliverContent;
}catch(){
unioncomLog.error("dealcontenterror!"+
ex.getMessage());
return"";
}
}
}
阅读全文

与java长连接短连接相关的资料

热点内容
php压缩包如何安装 浏览:646
免费看慢网站 浏览:150
外国影片女孩头一次出去上外地 浏览:478
程序员创业接到小程序订单 浏览:391
java复用反编译代码 浏览:551
qq聊天发送的文件在哪个文件夹 浏览:819
代理服务器地址格式是什么意思 浏览:443
苏e行app为什么会有登录过期 浏览:800
杰森坐牢 下象棋是什么电影 浏览:408
苹果相机也么加密 浏览:891
java图片打印 浏览:173
恶魔小丑电影 浏览:548
apriori算法软件 浏览:24
波利亚怎样解题pdf 浏览:570
法国电影耽美 浏览:642
java调用迅雷 浏览:423
开发云服务器cvm需要做些什么 浏览:259
程序员长期变胖 浏览:629
平板怎么创建图标文件夹 浏览:220
alphafrance制作的影片 浏览:281