zoukankan      html  css  js  c++  java
  • 自然语言交流系统 phxnet团队 创新实训 个人博客 (十三)

    关于java socket的一些总结:

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


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

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

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

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

        package com.biesan.sms.gate.unioncom.communication;  
        import com.biesan.commons.Constants;  
        import com.biesan.commons.util.CodeUtil;  
        import com.biesan.sms.gate.unioncom.data.*;  
        import com.biesan.sms.gate.unioncom.util.GateInfo;  
        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通信的详细介绍。

    如果您愿意花几块钱请我喝杯茶的话,可以用手机扫描下方的二维码,通过 支付宝 捐赠。我会努力写出更好的文章。 
    (捐赠不显示捐赠者的个人信息,如需要,请注明您的联系方式) 
    Thank you for your kindly donation!!

     

    
    
     
  • 相关阅读:
    j2ee学习笔记
    go开发和运行环境的配置
    Java基础学习笔记
    经典C/S服务器模型之守护进程
    linux抓包命令-tcpdump命令详解
    PostgreSQL的HA解决方案-2负载均衡(load balance)
    PostgreSQL的HA解决方案-1主从和备份(master/slave and backup)
    PostgreSQL的HA解决方案-项目概述
    将数据写到kafka的topic
    将sparkStreaming结果保存到Redshift数据库
  • 原文地址:https://www.cnblogs.com/qiaoyanlin/p/6891511.html
Copyright © 2011-2022 走看看