Android Open Source - msghandle Init Msg Socket Server






From Project

Back to project page msghandle.

License

The source code is released under:

GNU General Public License

If you think the Android project msghandle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anlong.msghandle.socket;
//w  w w .  ja  va 2  s .c  o  m
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.util.Log;

import com.anlong.msghandle.common.HandleStaticValue;
import com.anlong.msghandle.util.IMLog;
import com.anlong.msghandle.util.Utils;


/**
 * @Title: InitMsgSocketServer.java 
 * @Package com.anlong.msghandle.socket
 * @company ShenZhen AnLong Technology CO.,LTD.   
 * @author lixl   
 * @date 2014-3-12 ????3:59:55 
 * @version V1.0   
 * @Description: ??????????Socket??
 */
public class InitMsgSocketServer {
  private static OutputStream outputStream = null;
  private static InputStream inputStream = null;
  private static InetSocketAddress inetSocketAddress = null;
  private static Socket socket = null;
  
  private static String ip = "";
  private static int port = 0;
  
  /**
   * Private constructor
   */
  private InitMsgSocketServer(){}
  
  /**
   * get socket instance
   */
  public static void init(String ipAddress,int socketPort) throws IOException {
    try {
       
      closeSocketConnection();
      
      if(Utils.isNotNull(ipAddress) && Utils.isNotNull(socketPort)){
        ip = ipAddress;
        port = socketPort;
        if(openSocketConnection()==null){
          Log.e("test", "----??socket ?????????----------->");
          Utils.notifyMessage(8,HandleStaticValue.BCODE1000);
        }
      } else {
        // TODO ????????????
        Utils.notifyMessage(4,HandleStaticValue.BCODE1000);
        return;
      }
      
    } catch (IOException e) {
      Utils.notifyMessage(4,HandleStaticValue.BCODE1000);
      throw e;
    }
  }
  
  /**
   * Get socket instance
   */
  public static Socket getInstance(){
    return socket;
  }
  
  /**
   * destroy socket instance
   */
  private static void destroy() {
    if (socket != null )
      socket = null;
  }
  
  /**
   * close socket connection
   */
  public static void closeSocketConnection(){
    if ( socket != null ) {
      try {
        socket.close();
      } catch (IOException e) {
        IMLog.anlong("????Socket????!");
        e.printStackTrace();
      }
      
      destroy();
    }
  }  
  
  /**
   * create connection instance
   */
  private static Socket openSocketConnection() throws IOException {
    try {
      IMLog.anlong("???????([" + ip + "]:[" + port + "])?????!");
      //socket = new Socket(ip,port);
      socket = new Socket();   
      inetSocketAddress = new InetSocketAddress(ip, port);     
      socket.connect(inetSocketAddress, HandleStaticValue.SOCKET_TIMEOUT);
      IMLog.anlong("?????([" + ip + "]:[" + port + "])?????!");
    }catch(IOException e) {
      //e.printStackTrace();
      socket = null;
      // TODO ?????????
      IMLog.anlong("??[" + ip + "]:[" + port + "]??.");
      throw e;
    }
    return socket;
  }
  
  
  /**
   * Get the input stream
   */
  public static InputStream getInputStream() throws IOException {
    try {
      if ( socket != null ) 
        inputStream = socket.getInputStream();
    } catch (IOException e) {
      throw e;
    }
    return inputStream;
  }
  
  /**
   * Get the output stream
   */
  public static OutputStream getOutputStream() throws IOException {
    try {
      if ( socket != null )
        outputStream = socket.getOutputStream();
    } catch (IOException e) {
      throw e;
    }
    return outputStream;
  }
  
}




Java Source Code List

com.anlong.msghandle.common.BaseMessage.java
com.anlong.msghandle.common.HandleStaticValue.java
com.anlong.msghandle.common.HandleTimer.java
com.anlong.msghandle.common.MessageTimer.java
com.anlong.msghandle.event.MessageEventListener.java
com.anlong.msghandle.event.MessageEventSource.java
com.anlong.msghandle.event.MessageEvent.java
com.anlong.msghandle.file.InitImageFileServer.java
com.anlong.msghandle.handle.ImageRequestHandle.java
com.anlong.msghandle.handle.ImageResponseHandle.java
com.anlong.msghandle.handle.MsgRequestHandle.java
com.anlong.msghandle.handle.MsgResponseHandle.java
com.anlong.msghandle.impl.AppParamImpl.java
com.anlong.msghandle.impl.AppSystemMessageReportImpl.java
com.anlong.msghandle.impl.AuthenticationImpl.java
com.anlong.msghandle.impl.BroadcastImpl.java
com.anlong.msghandle.impl.CommonContactImpl.java
com.anlong.msghandle.impl.DepImpl.java
com.anlong.msghandle.impl.DepInfoListImpl.java
com.anlong.msghandle.impl.EditGroupImpl.java
com.anlong.msghandle.impl.EditGroupUserImpl.java
com.anlong.msghandle.impl.EditUserInfoImpl.java
com.anlong.msghandle.impl.ExitPreventGroupImpl.java
com.anlong.msghandle.impl.GetGroupInfoImpl.java
com.anlong.msghandle.impl.GetHistoryMessageImpl.java
com.anlong.msghandle.impl.GetUserInfoImpl105.java
com.anlong.msghandle.impl.GroupInfoListImpl.java
com.anlong.msghandle.impl.GroupShareImpl.java
com.anlong.msghandle.impl.HeartImpl103.java
com.anlong.msghandle.impl.LocationImpl.java
com.anlong.msghandle.impl.LoginImpl101.java
com.anlong.msghandle.impl.MessageStateReportImpl.java
com.anlong.msghandle.impl.OnlineStateImpl102.java
com.anlong.msghandle.impl.OnlineUserListImpl110.java
com.anlong.msghandle.impl.SendImageFile.java
com.anlong.msghandle.impl.SendMessageImpl203.java
com.anlong.msghandle.impl.SystemParaImpl.java
com.anlong.msghandle.impl.UnreadMessageImpl.java
com.anlong.msghandle.impl.UpdatePasswordImpl.java
com.anlong.msghandle.impl.UserInfoListImpl.java
com.anlong.msghandle.interfac.AbstractMsgHandle.java
com.anlong.msghandle.interfac.BaseActivity.java
com.anlong.msghandle.interfac.MessageHandleActivity.java
com.anlong.msghandle.interfac.MsgHandle.java
com.anlong.msghandle.message.InitRequMessageServer.java
com.anlong.msghandle.message.InitRespMessageServer.java
com.anlong.msghandle.request.BaseRequest.java
com.anlong.msghandle.request.Request100.java
com.anlong.msghandle.request.Request101.java
com.anlong.msghandle.request.Request102.java
com.anlong.msghandle.request.Request103.java
com.anlong.msghandle.request.Request105.java
com.anlong.msghandle.request.Request110.java
com.anlong.msghandle.request.Request203.java
com.anlong.msghandle.request.Request301.java
com.anlong.msghandle.response.BaseResponse.java
com.anlong.msghandle.response.Response1000.java
com.anlong.msghandle.response.Response1010.java
com.anlong.msghandle.response.Response1020.java
com.anlong.msghandle.response.Response1030.java
com.anlong.msghandle.response.Response1050.java
com.anlong.msghandle.response.Response1100.java
com.anlong.msghandle.response.Response2030.java
com.anlong.msghandle.response.Response3010.java
com.anlong.msghandle.service.InitServerManager.java
com.anlong.msghandle.service.ServerManager.java
com.anlong.msghandle.socket.InitFileSocketServer.java
com.anlong.msghandle.socket.InitMsgSocketServer.java
com.anlong.msghandle.util.ByteAndInt.java
com.anlong.msghandle.util.FormatTransfer.java
com.anlong.msghandle.util.IMLog.java
com.anlong.msghandle.util.ReflectionUtil.java
com.anlong.msghandle.util.Utils.java
com.anlong.msghandle.vo.AppMessage.java
com.anlong.msghandle.vo.Application.java
com.anlong.msghandle.vo.BroadcastMessage.java
com.anlong.msghandle.vo.DepInfo.java
com.anlong.msghandle.vo.GroupInfo.java
com.anlong.msghandle.vo.GroupShare.java
com.anlong.msghandle.vo.OnlineUser.java
com.anlong.msghandle.vo.ReportState.java
com.anlong.msghandle.vo.SimpleUserInfo.java
com.anlong.msghandle.vo.UserInfo.java
com.anlong.msghandle.vo.UserMessage.java