加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 业界 > 正文

Socket技能,实现Http协媾和游戏处事器协议(Java代码)

发布时间:2019-08-02 19:03:08 所属栏目:业界 来源:今日头条
导读:在收集技能中,前后端通信是一个难点,也是一个处事器措施员必需攻陷的技能瓶颈。在两台计较神秘实现信息交互,就必要一种技能来说,而两台计较机网卡来实现,无非就是两种技能UDP和TCP,个中,两种技能因为差异的特征,行使在差异的处所,在一些不要求高

附上分包器的代码(java代码):

  1. public class EncodedMessage { 
  2.  public static int code=44434533; 
  3.  /* 
  4.  codeUnid 呼吁 
  5.  messageUnid 动静独一码 
  6.  body 包体字节约 
  7.  */ 
  8.  public static byte[] encoded(int codeUnid,int messageUnid,byte[] body){ 
  9.  //44434533 / 包头(int)4+ 呼吁编号(shot)2 + 动静独一编号(int)4 +内容长度(int)4+内容bytes / 
  10.  ByteBuffer buffer= ByteBuffer.allocate(14+body.length); 
  11.  buffer.putInt(code); 
  12.  short code=(short)codeUnid; 
  13.  buffer.putShort(code); 
  14.  buffer.putInt(messageUnid); 
  15.  buffer.putInt(body.length); 
  16.  buffer.put(body); 
  17.  return buffer.array(); 
  18.  } 
  19.  public static int byteArrayToInt(byte[] b) { 
  20.  return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24; 
  21.  } 
  22.  public static short bytesToShort(byte[] b) { 
  23.  return (short) (b[1] & 0xff | (b[0] & 0xff) << 8); 
  24.  } 

这一套通信协议都是我在以是游戏处事器回收,处事器可以很甜头理赏罚分包,不会呈现连包环境。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读