TCP连接客户端的方法

Posted computervip

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP连接客户端的方法相关的知识,希望对你有一定的参考价值。

 

 

 

SDKUtils.java 工具类

package com.gsafety.dispatcher.maikexing.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gsafety.dispatcher.maikexing.employee.Employee;
import com.gsafety.dispatcher.maikexing.group.Group;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * 用于定义所有的sdk的调用方式
 * @author luwch
 *
 */
public class SdkUtil {
    private static Map<String,Dispatch> dispatchMap = new HashMap<String,Dispatch>();
    
    /**
     * 获得SDK的内容信息
     * @return
     */
    public static  Dispatch getDispatch(String ip,String port){
        Dispatch dispatch = null;
        if(dispatchMap.get("dispatch")==null){
            //用来获取Dispatch对象
            dispatch = ResultUtil.createDispatch();
//            InitSdk.connectSDK(dispatch, "118.178.92.58", "9000");
            SdkUtil.connectSDK(dispatch, ip, port);
            dispatchMap.put("dispatch", dispatch);
        }else{
            dispatch = dispatchMap.get("dispatch");
        }
        return dispatch;
    }
    
    /**
     * 初始化sdk的内容
     * @param dispatch
     * @param ip
     * @param port
     */
    public static void connectSDK(Dispatch dispatch,String ip,String port) {
        Variant f = dispatch.call(dispatch, "Net_SDK_Init",
                new Variant(ip), new Variant(port),
                new Variant(""), new Variant(0));
        System.out.println("connectSDK======f.getInt()=======>"+f.getInt());
    }
    
    /*******销毁SDK*******start***/
    public void getCleanup(Dispatch dispatch,int cmdId,String username,String password){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_Cleanup", 
                new Variant(cmdId), new Variant(username), new Variant(password), new Variant());
        System.out.println("b.getInt()=====>"+b.getInt());
    }
    /*******销毁SDK*******end***/
    
    
    /***********登录和登录返回的信息********start*******/
    /**
     * 登录信息
     * @param dispatch
     * @param cmdId
     * @param username
     * @param password
     */
    public static  void getLogin(Dispatch dispatch,int cmdId,String username,String password){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_Login", 
                new Variant(cmdId), new Variant(username), new Variant(password), new Variant());
        System.out.println("getLogin=====b.getInt()=====>"+b.getInt());
    }
    /***********登录和登录返回的信息********end*******/
    
    /*********退出和退出返回的信息********start*********/
    /**
     * @param dispatch
     * @param cmdId  命令序列号,自增长。
     * @param bstrAgentID  登录操作员的ID,如果是其他座席的ID代表将它下线(只有admin或班长座席有权限)。
     */
    public static void getLogout(Dispatch dispatch,int cmdId,String bstrAgentID){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_Logout", 
                cmdId, bstrAgentID);
        System.out.println("getLogout===============》b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 连接保活
     * @param dispatch
     * @param sReconnectInterval
     * @param sHeartInterval
     * @param sWaitTime
     */
    public static void setTimeout(Dispatch dispatch,Integer sReconnectInterval,Integer sHeartInterval,Integer sWaitTime){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_SetConnectTime", 
                new Variant(sReconnectInterval), new Variant(sHeartInterval),new Variant(sWaitTime));
        System.out.println("getLogout===============》b.getInt()=====>"+b.getInt());
    }
    
    /*********退出和退出返回的信息********end*********/
    
    
    
    /******************用于打电话**********start***********/
    /**
     * 调用打电话的功能
     * @param dispatch SDK 
     * @param cmdId    编码
     * @param bstrCallingDevice  主叫用户号码
     * @param bstrCalledDevice   被叫用户号码
     * @param bstrUserID   被叫用户ID,可选
     * @param eCallType  呼叫类型,枚举定义 0 表示语音呼叫  其他的为视频的接口信息
     * @param eDuplexMode  双工模式,枚举定义 0表示全双工,1表示半双工
     */
    public static void makeCall(Dispatch dispatch,int cmdId,String bstrCallingDevice,String bstrCalledDevice,
            String bstrUserID,int eCallType,int  eDuplexMode){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_MakeCall", 
                new Variant(cmdId), new Variant(bstrCallingDevice), new Variant(bstrCalledDevice), new Variant(bstrUserID)
                , new Variant(eCallType) , new Variant(eDuplexMode));
        System.out.println("makeCall=======b.getInt()=====>"+b.getInt());
    }
    

    /**
     * 调度台获取当前号码的通话录音记录。
     * @param dispatch  sdk初始化内容
     * @param cmdId    登录编码
     * @param bstrBeginTime  起始时间,可为空,格式:YYYYMMDDHHMMSS
     * @param bstrEndTime  截止时间,可为空,格式:YYYYMMDDHHMMSS
     * @param bstrCallingDevice  主叫用户号码,可为空
     * @param bstrCalledDevice  被叫用户号码,可为空 
     * @param bstrCallDevice   通话的用户号码,主叫或被叫,可为空
     * @param bstrOperatorID  操作员ID,可为空
     * @param sBeginIndex   起始索引,必填
     * @param sCount   请求条数,必填
     */
    public  static void getGetCallRecord(Dispatch dispatch,int cmdId,String bstrBeginTime,String bstrEndTime,String bstrCallingDevice,
            String bstrCalledDevice,String bstrCallDevice,String bstrOperatorID,int sBeginIndex,int sCount){
        Variant b = Dispatch.call(dispatch, "Net_SDK_GetCallRecord", 
                cmdId, bstrBeginTime,bstrEndTime,bstrCallingDevice,bstrCalledDevice,bstrCallDevice,bstrOperatorID,sBeginIndex,sCount);
        System.out.println("b.getInt()=====>"+b.getInt());
    }
    
    
    /**
     * 调度台对用户号码进行拆线操作。
     * @param dispatch sdk
     * @param cmdId   编号
     * @param bstrCalledDevice  
     * @param bstrUserID
     */
    public  static void getClearConnection(Dispatch dispatch,int cmdId,String bstrCalledDevice,String bstrUserID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_ClearConnection", 
                new Variant(cmdId), new Variant(bstrCalledDevice),new Variant(bstrUserID));
        System.out.println("b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 用于挂断电话的信息
     * @param dispatch
     * @param cmdId  编码
     * @param bstrAgentID  操作员id
     */
    public static void getClearCall(Dispatch dispatch,int cmdId,String bstrAgentID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_ClearCall", 
                new Variant(cmdId), new Variant(bstrAgentID));
        System.out.println("b.getInt()=====>"+b.getInt());
    }
    /******************用于打电话**********end***********/
    
    
    
    /*******用于操作职员信息*****start****/
    /**
     * 用于添加职员的信息
     * @param dispatch
     * @param cmdId
     * @param info
     */
    public static void employeeAdd(Dispatch dispatch,int cmdId,Employee info){
        // 自增ID;userName用户名 password用户密码【】
        Variant b = Dispatch.call(dispatch, "Net_SDK_EmployeeAdd", 
                new Variant(cmdId), JSON.toJSONString(info));
        System.out.println("employeeAdd=======b.getInt()=====>"+b.getInt());
    }
    /**
     * 用于修改职员的信息
     * @param dispatch
     * @param cmdId
     * @param info
     */
    public static void employeeMod(Dispatch dispatch,int cmdId,Employee info){
        // 自增ID;userName用户名 password用户密码【】
        Variant b = Dispatch.call(dispatch, "Net_SDK_EmployeeMod", 
                new Variant(cmdId), JSON.toJSONString(info));
        System.out.println("employeeMod=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 用于删除职员的信息
     * @param dispatch
     * @param cmdId
     * @param bstrEmployeeID
     */
    public static void employeeDel(Dispatch dispatch,int cmdId,String bstrEmployeeID){
        // 自增ID;userName用户名 password用户密码【】
        Variant b = Dispatch.call(dispatch, "Net_SDK_EmployeeDel", 
                new Variant(cmdId), bstrEmployeeID);
        System.out.println("employeeDel=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 用于查询人员的信息
     * @param dispatch
     * @param cmdId      命令序列号,自增长
     * @param bstrEmployeeID   职员ID,当该项不为空时,查询对应ID的职员信息
     * @param bstrGroupID  组ID,当该项不为空时,查询对应ID组下的所有职员信息
     */
    public static void employeeQuery(Dispatch dispatch,int cmdId,String bstrEmployeeID,String bstrGroupID){
//        // 自增ID;userName用户名 password用户密码【】
//        Variant b = Dispatch.call(dispatch, "Net_SDK_EmployeeQuery", 
//                new Variant(cmdId), bstrEmployeeID,bstrGroupID);
        // 自增ID;userName用户名 password用户密码【】
        Variant b = Dispatch.call(dispatch, "Net_SDK_EmployeeQuery", 
                new Variant(cmdId));
        System.out.println("employeeQuery=======b.getInt()=====>"+b.getInt());
    }
    
    /*******用于操作职员信息*****end****/
    
    
    /*******用于操作组的信息*****start****/
    /**
     * 添加组的信息
     * @param dispatch
     * @param cmdId
     * @param info
     */
    public static void groupAdd(Dispatch dispatch,int cmdId,Group info){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_GroupAdd", 
                new Variant(cmdId), JSON.toJSONString(info));
        System.out.println("groupAdd=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 查询组的信息
     * @param dispatch
     * @param cmdId
     * @param info
     */
    public static void groupQuery(Dispatch dispatch,int cmdId,String bstrGroupID,String bstrParentID){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_GroupQuery", 
                new Variant(cmdId), bstrGroupID,bstrParentID);
        System.out.println("groupQuery=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 删除用户组的信息
     * @param dispatch
     * @param cmdId
     * @param bstrGroupID
     */
    public static void groupDel(Dispatch dispatch,int cmdId,String bstrGroupID){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_GroupDel", 
                new Variant(cmdId), bstrGroupID);
        System.out.println("groupQuery=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 用于修改组的信息
     * @param dispatch
     * @param cmdId
     * @param info
     */
    public static void groupMod(Dispatch dispatch,int cmdId,Group info){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_GroupMod", 
                new Variant(cmdId), JSON.toJSONString(info));
        System.out.println("groupQuery=======b.getInt()=====>"+b.getInt());
    }
    /*******用于操作组的信息*****end****/
    
    
    /*******用于发送短信的信息***********start*******/
    /**
     * 
     * @param dispatch
     * @param cmdId  命令序列号,自增长
     * @param bstrSmsContent  短信内容,支持中文,最大1024字节
     * @param bstrSmsContacts  接收人的短信号码或调度台ID,支持群发多个,数组大小1000
     * @param bstrSmsGroupID   群发组ID,首次群发时由服务端生成,可选
     * @param eSmsFormat   短信文件类型,枚举定义  
     *             typedef enum E_SMS_FORMAT
                {
                SMS_FORMAT = 0,      //文本
                PHOTO_FORMAT,        //图片
                VIDEO_FORMAT,        //视频
                AUDIO_FORMAT,        //语音
                FILE_FORAMT,            //文件
                } 
     * @param bstrSmsFileName   图片、视频上传到服务器的文件名(确保唯一性)
     * @param bstrSmsRealFileName   图片、视频显示文件名称
     * @param bstrSmsFileSize   文件大小,单位:字节
     * @param eSmsType   短信类型
     * typedef enum E_SENDTYPE
        {
        SENDTYPE_SINGLE = 0,   //单发
        SENDTYPE_GROUP,           //群发
        SENDTYPE_CHAT,            //群聊
        SENDTYPE_MCHAT,        //会议群聊
        } 

     */
    public static void smsSend(Dispatch dispatch,int cmdId,String bstrSmsContent,String [] bstrSmsContacts,    String bstrSmsGroupID,
            int eSmsFormat,String bstrSmsFileName,String bstrSmsRealFileName,String bstrSmsFileSize,int eSmsType){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_SmsSend", 
                new Variant(cmdId), bstrSmsContent, JSON.toJSONString(bstrSmsContacts),bstrSmsGroupID,eSmsFormat,
                bstrSmsFileName,bstrSmsRealFileName,bstrSmsFileSize,eSmsType);
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    /**
     * 
     * @param dispatch
     * @param cmdId
     * @param sBeginIndex
     * @param sCount
     */
    public static void smsList(Dispatch dispatch,int cmdId,int sBeginIndex,int sCount){
        // 自增ID;userName用户名 password用户密码
        Variant b = Dispatch.call(dispatch, "Net_SDK_GetSmsList", 
                new Variant(cmdId), sBeginIndex,sCount);
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    
    /*******用于创建会议***********start*******/
    public static void createMeet(Dispatch dispatch, int cmdid, String meetName, String meetNum, int isAllowSpeak,
            int meetMode) {
        Variant b = Dispatch.call(dispatch, "Net_SDK_MeetCreate",
                new Variant(cmdid),meetName,meetNum, isAllowSpeak,meetMode);
        System.out.println("createMeet=======b.getInt()=====>"+b.getInt());
    }
    /*******用于结束会议***********start*******/
    public static void endMeet(Dispatch dispatch, int cmdid, String meetID) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetEnd",
                new Variant(cmdid),meetID);
        System.out.println("endMeet=======b.getInt()=====>"+b.getInt());
    }

    //修改会议
    public static void modifyMeet(Dispatch dispatch, int cmdid, String meetID, String meetName, String meetNum,
            int isAllowSpeak) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetModify",
                new Variant(cmdid),meetID,meetName,meetNum,isAllowSpeak);
        System.out.println("modifyMeet=======b.getInt()=====>"+b.getInt());
    }

    //查询会议
    public  static void  queryMeet(Dispatch dispatch, int cmdid, String meetId) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_GetMeetInfo",new Variant(cmdid));
        System.out.println("queryMeet=======b.getInt()=====>"+b.getInt());
    }

    //锁定会议
    public static void lockedMeet(Dispatch dispatch, int cmdid, String meetId, int isLocked) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetLock",new Variant(cmdid),meetId,isLocked);
        System.out.println("lockedMeet=======b.getInt()=====>"+b.getInt());
    }

    //会议录音
    public static void recordMeet(Dispatch dispatch, int cmdid, String meetId, int isRecord) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetRecord",new Variant(cmdid),meetId,isRecord);
        System.out.println("recordMeet=======b.getInt()=====>"+b.getInt());
    }

    //会议广播
    public static void MeetBroadcast(Dispatch dispatch, int cmdid, String meetId, int isBroadcast, String fileName) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetBroadcast",new Variant(cmdid),meetId,isBroadcast,fileName);
        System.out.println("MeetBroadcast=======b.getInt()=====>"+b.getInt());
    }
    //成员发言
    public static void allowSpeak(Dispatch dispatch, int cmdid, String meetId, String activeDevice, String userID) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetSpeakAllow",new Variant(cmdid),meetId,activeDevice,userID);
        System.out.println("allowSpeak=======b.getInt()=====>"+b.getInt());
    }

    //成员禁言
    public static void banSpeak(Dispatch dispatch, int cmdid, String meetId, String activeDevice, String userID) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetSpeakBan",new Variant(cmdid),meetId,activeDevice,userID);
        System.out.println("banSpeak=======b.getInt()=====>"+b.getInt());
    }

    //成员踢出
    public static void kickMember(Dispatch dispatch, int cmdid, String meetId, String activeDevice, String userID) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetKick",new Variant(cmdid),meetId,activeDevice,userID);
        System.out.println("kickMember=======b.getInt()=====>"+b.getInt());
    }

    //回到会场
    public static void backMeet(Dispatch dispatch, int cmdid, String meetId, String activeDevice, String userID) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_MeetBack",new Variant(cmdid),meetId,activeDevice,userID);
        System.out.println("backMeet=======b.getInt()=====>"+b.getInt());
    }

    //查询会议成员
    public static void showMeetMemeber(Dispatch dispatch, int cmdid) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_GetMeetMemberInfo",new Variant(cmdid));
        System.out.println("backMeet=======b.getInt()=====>"+b.getInt());
    }

    //加入会议
    public static void joinMeet(Dispatch dispatch, int cmdid, String phonenum, String bstrCalledDevice,
            String bstrUserID, String meetId) {
        Variant b= Dispatch.call(dispatch, "Net_SDK_JoinMeetCall",new Variant(cmdid),phonenum,bstrCalledDevice,bstrUserID,meetId);
        System.out.println("joinMeet=======b.getInt()=====>"+b.getInt());
    }
    /**
     * 电话点名功能
     * @param dispatch
     * @param cmdId
     * @param bstrCallingDevice
     * @param bstrCalledDevice
     * @param bstrUserID
     * @param eCallMode
     * @param eFileName
     */
    public static void rollCall(Dispatch dispatch,int cmdId,String bstrCallingDevice,String bstrCalledDevice,
            String bstrUserID,Integer eCallMode,String  eFileName){
        Variant b = Dispatch.call(dispatch, "Net_SDK_RollCall", 
                new Variant(cmdId), new Variant(bstrCallingDevice), new Variant(bstrCalledDevice)
                , new Variant(eCallMode) , eFileName);
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 中断点名操作
     * @param dispatch
     * @param cmdId
     * @param flowID
     */
    public static void endRollCall(Dispatch dispatch,int cmdId,String flowID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_EndRollCall", 
                new Variant(cmdId), new Variant(flowID));
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 轮询功能
     * @param dispatch
     * @param cmdId
     * @param bstrCallingDevice
     * @param bstrCalledDevice
     * @param bstrUserID
     * @param ecallType
     */
    public static void pollCall(Dispatch dispatch,int cmdId,String bstrCallingDevice,String bstrCalledDevice,
            String bstrUserID,Integer ecallType){
        Variant b = Dispatch.call(dispatch, "Net_SDK_PollCall", 
                new Variant(cmdId), new Variant(bstrCallingDevice), 
                new Variant(bstrCalledDevice), new Variant(ecallType));
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 中断点名操作
     * @param dispatch
     * @param cmdId
     * @param flowID
     */
    public static void pollCallEnd(Dispatch dispatch,int cmdId,String flowID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_PollCallEnd", 
                new Variant(cmdId), new Variant(flowID));
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 呼叫广播操作
     * @param dispatch
     * @param cmdId
     * @param bstrCallingDevice
     * @param bstrCalledDevice
     */
    public static void broadcastCall(Dispatch dispatch,int cmdId,String bstrCallingDevice,
            String bstrCalledDevice,Integer callMode,String bstrFileName){
        Variant b = Dispatch.call(dispatch, "Net_SDK_BroadcastCall", 
                new Variant(cmdId),new Variant(bstrCallingDevice), 
                new Variant(bstrCalledDevice),new Variant(callMode),new Variant(), 
                new Variant(bstrFileName), new Variant(0), new Variant());
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 结束广播操作
     * @param dispatch
     * @param cmdId
     * @param flowID
     */
    public static void broadcastCallEnd(Dispatch dispatch,int cmdId,String flowID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_BroadcastCallEnd", 
                new Variant(cmdId), new Variant(flowID));
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
    
    /**
     * 电话录音
     * @param dispatch
     * @param cmdId
     * @param flowID
     */
    public static void recordCall(Dispatch dispatch,String isRecord,String bstrActiveDevice,int bstrActiveUserID){
        Variant b = Dispatch.call(dispatch, "Net_SDK_RecordCall",new Variant(isRecord),new Variant(bstrActiveDevice),new Variant(bstrActiveUserID));
        System.out.println("smsSend=======b.getInt()=====>"+b.getInt());
    }
}

 

以上是关于TCP连接客户端的方法的主要内容,如果未能解决你的问题,请参考以下文章

tcp协议详解(四

如何在 VB.NET 中检查 TCP 服务器(套接字)与 TCP 客户端的连接状态

基于 TCP Socket 的服务器与客户端的简单连接

TCP阻塞模型下服务器和客户端的建立步骤

【tcp】 TCP的连接重连&QUIC的连接迁移

字节一面:服务端挂了,客户端的 TCP 连接还在吗?