首页编程java编程java socket 什么时候连接,java中如何创建socket连接的过程

java socket 什么时候连接,java中如何创建socket连接的过程

编程之家2023-10-13109次浏览

大家好,关于java socket 什么时候连接很多朋友都还不太明白,今天小编就来为大家分享关于java中如何创建socket连接的过程的知识,希望对各位有所帮助!

java socket 什么时候连接,java中如何创建socket连接的过程

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

Java Socket通信有很多的时候需要我们不断的学习。方面效率虽然不及C与C++但它以灵活语言优势,为大家广为使用。本文就对在使用java做通信方面程序时候应改注意问题做以说明。1.长连接、短链接只是针对客户端而言,服务器无所谓长、短;2.无论同步或者异步通信,发送之后务必要又响应回复,确认收到,负责进行一定范围内重发,例如重发三次;3.长连接服务器与客户端之间务必需要心跳探测,由客户端主动发起;4.短连接服务器通用代码:

package com.biesan.sms.gate.unioncom.communication;

import com.biesan.commons.Constants;

java socket 什么时候连接,java中如何创建socket连接的过程

import com.biesan.commons.util.CodeUtil;

import com.biesan.sms.gate.unioncom.data.*;

import com.biesan.sms.gate.unioncom.util.GateInfo;

java socket 什么时候连接,java中如何创建socket连接的过程

import java.net.*;

import java.io.*;

import java.util.*;

import org.apache.log4j.*;

import spApi.*;

public class UnioncomDeliver extends Thread{

// stop flag

private boolean unInterrupt= true;

private boolean unErr= true;

//private boolean closeSocketFlag= false;

// server socket

private ServerSocket serverSo= null;

// current socket

private Socket so= null

private OutputStream output= null;

private InputStream input= null;

// gate command

private SGIP_Command tmpCmd= null;

private SGIP_Command cmd= null;

private Bind bind= null;

private BindResp bindResp= null;

//private Unbind unBind= null;

private UnbindResp unBindResp= null;

private boolean unAcceptErrorFlag= true;

Logger unioncomLog= Logger.getLogger(Unioncom

Deliver.class.getName());

public UnioncomDeliver(){

}

public void run(){

unioncomLog.info("Start...");

while(unInterrupt){

this.initServer();

this.startServices();

while(this.unAcceptErrorFlag){

try{

//接受连接请求

unioncomLog.info("before accept connection!.......

FreeMemroy:"+ Runtime.getRuntime().freeMemory());

this.acceptConnection();

unioncomLog.info("after accept connection!.......

FreeMemroy:"+ Runtime.getRuntime().freeMemory());

while(unErr){

cmd= new Command();

unioncomLog.info("before read command from stream

........... FreeMemroy:"+ Runtime.getRuntime().

freeMemory());

tmpCmd= cmd.read(input);

unioncomLog.info("after read command from stream"+

getCommandString(cmd.getCommandID())+" FreeMemroy:"+

Runtime.getRuntime().freeMemory());

if(tmpCmd== null){

unErr= false;

break;

}

switch(cmd.getCommandID()){

// biad ready communication

case SGIP_Command.ID_SGIP_BIND:{

this.dealBind();

break;

}// exit bind

case SGIP_Command.ID_SGIP_UNBIND:{

this.dealUnBind();

unioncomLog.info("after unbind connection!.......

FreeMemroy:"+ Runtime.getRuntime().freeMemory());

break;

}// deliver

....

default://错误的命令字

break;

}// switch

}// while(unErr)

} catch(Exception e){

unioncomLog.error("Unioncom Recv Service Error"

+ e.getMessage());

} finally{

if(this.so!= null){

this.closeSocket();

}

this.unErr= true;

}

}// while(this.unAcceptErrorFlag)

try{

this.closeServerSocket();

sleep(200);// sleep

} catch(InterruptedException ie){

}

}// while(unInterrupt)

}

private String getCommandString(int cmd){

switch(cmd){

// biad ready communication

case SGIP_Command.ID_SGIP_BIND:{

return" BIND COMMAND";

}// exit bind

case SGIP_Command.ID_SGIP_UNBIND:{

return" UNBIND COMMAND";

}// deliver

case...

default:

return" UNKNOWN COMMAND";

}

}

private void dealBind(){

try{

bind= new Bind(tmpCmd);

if(bind.readbody()!= 0){

unioncomLog.warn("Read Bind error");

this.unErr= false;

}

bindResp= new BindResp(tmpCmd.getMsgHead());

bindResp.SetResult(0);

bindResp.write(output);

unioncomLog.debug("Bind success!");

} catch(Exception e){

unioncomLog.error("Dela Union Recv Bind Error!"+

e.getMessage());

this.unErr= false;

}

}

private void dealUnBind(){

try{

//unBind=(Unbind) tmpCmd;

unBindResp= new UnbindResp(tmpCmd.getMsgHead());

unBindResp.write(output);

unioncomLog.debug("UnBind success!");

} catch(Exception e){

unioncomLog.warn("Unbind error!"+ e.getMessage());

}

this.unErr= false;

}

private void startServices(){

boolean unStartServices= true;

while(unStartServices){

try{

serverSo= new ServerSocket(ugInfo.getLocalServerPort(), 5,

InetAddress.getByName(ugInfo.getLocalIpAdd()));

//serverSo.setSoTimeout(60000);

unStartServices= false;

unioncomLog.info("Create union recv socket Ok!");

} catch(IOException e){

unioncomLog.warn("Create union recv socket error!"

+ e.getMessage());

unStartServices= true;

UnioncomSubmit.thrSlp(3000);

}

}

}

private void acceptConnection(){

// Accept失败

try{

so= serverSo.accept();

so.setSoTimeout(10000);

} catch(Exception e){

unioncomLog.warn("Accept Error!"+ e.getMessage());

this.closeServerSocket();

this.unAcceptErrorFlag= false;

this.unErr=false;

}

// Accept成功

try{

input= so.getInputStream();

output= so.getOutputStream();

} catch(IOException e){

unioncomLog.warn("Get I/O stream Error!"+ e.getMessage());

this.closeService();

this.unAcceptErrorFlag= false;

this.unErr=false;

}

}

private void closeSocket(){

try{

so.close();

unioncomLog.info("Socket Close Success!!!");

} catch(Exception e){

unioncomLog.error("Socket Close Failure!!!"+ e.getMessage());

}

}

private void closeServerSocket(){

try{

serverSo.close();

unioncomLog.info("ServerSocket Close Success!!!");

} catch(Exception e){

unioncomLog

.error("ServerSocket Close Failure!!!"+ e.getMessage());

}

}

private void closeService(){

this.closeSocket();

this.closeServerSocket();

}

private void initServer(){

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

}

public synchronized void requireStop(){

this.unInterrupt= false;

unioncomLog.info("Requre interrupt!!!");

}

public String convertMsgContentCoding

(int msgCoding, byte[] msgContent){

String deliverContent= null;

try{

if(msgContent!= null){

if(msgCoding== 8){//处理ucs32编码

deliverContent= new String(msgContent,

"UnicodeBigUnmarked");

} else if(msgCoding== 0){//处理ASCII编码

deliverContent= new String(msgContent,"ASCII");

} else if(msgCoding== 4){//处理binary编码

deliverContent= new String(msgContent);

} else if(msgCoding== 15){//处理GBK编码

deliverContent= new String(msgContent,"GBK");

//处理DELIVER数据包的短信息ID

} else{

unioncomLog.error("编码格式错误!");

return"";

}

} else

return"";

return deliverContent;

} catch(UnsupportedEncodingException ex){

unioncomLog.error("deal content error!"+

ex.getMessage());

return"";

}

}

}

java中怎么判断socket是否连接成功

工程里遇到了需要判断Socket是否已经关闭的问题,使用

public boolean isAlive(){ if(mSocket.equals(null)||mSocket.isClosed()||!mSocket.isConnected()||mSocket.isInputShutdown()||mSocket.isOutputShutdown()){ MyLog.d("mSocket.isConnected()"+mSocket.isConnected()); return false;}else{ MyLog.d("mSocket.isConnected()"+mSocket.isConnected()); return true;}}毫无作用,随后查看了网上先关解决办法,发现Socket中有一个方法void sendUrgentData(0xFF)当对方SO_OOBINLINE属性没有打开时,就会自动舍弃这个字节,而SO_OOBINLINE属性默认情况下就是关闭的,但是只要能够发送到对方Socket,sendUrgentData(0xFF)方法就不会报错,只要catch一下IOException就OK了,当对方关掉了Socket之后就会进入catch,问题迎刃而解。

try{ mSocket.sendUrgentData(0xFF);}catch(IOException e){ Log.d(e+":要关掉了阿!"); mSocket.close();}当socket断掉之后,抛出java.net.socketexception broken pipe异常,没有问题。

java中如何创建socket连接的过程

1、在打开的ie浏览器窗口右上方点击齿轮图标,选择“Internet选项”,如下图所示:

2、在打开的Internet选项窗口中,切换到安全栏,在安全选卡中点击“自定义级别”,如下图所示:

3、在“安全设置-Internet区域”界面找到“Java小程序脚本”、“活动脚本”,并将这两个选项都选择为“禁用”,然后点击确定,如下图所示:

关于java socket 什么时候连接,java中如何创建socket连接的过程的介绍到此结束,希望对大家有所帮助。

java的框架有什么用,JAVA的三大框架有什么作用java是什么软件(JAVA是什么软件)