導航:首頁 > 編程語言 > 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長連接短連接相關的資料

熱點內容
泰國科幻電影機器人 瀏覽:693
十部頂級粵語電影 瀏覽:588
黑色豪門之純情老婆全本免費 瀏覽:290
最美人物微電影名字 瀏覽:62
德國愛情電影推薦 瀏覽:859
好看的,每天更新的網址推薦 瀏覽:910
五行拳電影 瀏覽:788
wolfram編程 瀏覽:140
蜘蛛有哪幾種類動畫片? 瀏覽:852
韓劇這個男的和三姐妹都有關系 瀏覽:847
電影中的大奶的視頻 瀏覽:178
日本電影一個女的乳頭被切掉了 瀏覽:268
看電影男朋友聽我心跳 瀏覽:494
cudac語言編程指南 瀏覽:856
小說女主被男主囚禁各種逃跑 瀏覽:722
重生在黃埔一期參加北伐的小說 瀏覽:88
韓國床戲電影合集 瀏覽:199
好看的快穿肉文 瀏覽:512
曼曼雅朵小仙兒有聲小說 瀏覽:298