socket通信例子_第1页
socket通信例子_第2页
socket通信例子_第3页
socket通信例子_第4页
socket通信例子_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、socket通信例子public void send()        while (true)        string sql="select top 100 * from send"    SqlDataReader datareader =sqlserver.ExecuteSqlDataReader(sql);    if (datare

2、ader.HasRows) /有记录才进行网关联接         string show;     string host;     /int port =Convert.ToInt16("8801");     /host = Dns.GetHostName();     /host

3、= "10"     socket temp=new socket();     Socket send=temp.connectSocket(this.remote_ip,this.remote_port);        if (send = null)         

4、0; this.send_listbox("打开远程SMG失败");              else           this.send_listbox("打开远程SMG成功");      byte bind_content=sg

5、ip_test.bind(count);      count_step();      byte receive=new byte256;      byte flag;      send.Send(bind_content,bind_content.Length,0); /bind      

6、send.Receive(receive,29,0); /bind_resp 读取29位      flag=sgip_test.bind_resp_result(receive);      if (flag=0)       this.send_listbox("成功联接到网关");      else  

7、;     this.send_listbox("联接网关失败");      while (datareader.Read()             string spnumber=datareader"spnumber".ToString().Trim();    

8、0;  string chargenumber=datareader"chargenumber".ToString().Trim();       string usernumber=datareader"usernumber".ToString().Trim();       string servicetype=datareader"servicetype".ToStrin

9、g().Trim();       byte feetype=Byte.Parse(datareader"feetype".ToString().Trim();       string feevalue="/datareader"feevalue".ToString().Trim();       string" conten

10、t=datareader"content".ToString().Trim();       byte submit=sgip_test.submit(spnumber,chargenumber,usernumber,servicetype,feetype,feevalue,content,CorpId,count);       send.Send(submit,submit.Length,0);/sumbmit  

11、;     count_step();       send.Receive(receive,29,0); /submit_resp       flag=sgip_test.submit_resp_result(receive);       if (flag=0)      

12、         this.send_listbox("手机号:"+usernumber+"   业务代码:"+servicetype+"  SP下行号码:"+spnumber);        this.send_listbox("消息内容:"+content);    

13、60;   this.send_listbox("消息发送成功");        string del_sql="delete from send where id="+datareader"id".ToString().Trim();        sqlserver.ExecuteSqlComand(del_sql);  

14、            else        this.send_listbox("消息发送失败");                   datareader.Close(); 

15、0;     byte unbind=sgip_test.unbind(count);      count_step();      send.Send(unbind,unbind.Length,0);/unbind      send.Receive(receive,20,0);/unbind_resp 只有消息头    &

16、#160; send.Close();      temp=null;              else    datareader.Close(); /如果没有记录的话,就马上删除联接,不然要超时出错           Thread.Sleep(100);

17、0;    数据写入字节流   public byte submit(string SPNumber,string ChargeNumber,string UserNumber,string ServiceType,byte FeeType,string FeeValue,string MessageContent,string CorpId,uint count)       /SPNumber 21字节 text SP的接入号码   

18、0; /ChargeNumber  21字节 text付费号码,手机号码前加“86”国别标志;当且仅当群发且对用户收费时为空;如果为空,则该条短消息产生的费用由UserNumber代表的用户支付;如果为全零字符串“000000000000000000000”,表示该条短消息产生的费用由SP支付。   /UserCount 1字节 int 接收短消息的手机数量,取值范围1至100   /UserNumber 21字节 text 接收该短消息的手机号,该字段重复UserCount指定的次数,手机号码前加“86”国别标志

19、   /CorpId 5字节 text 企业代码,取值范围0-99999   /ServiceType 10字节 text 业务代码,由SP定义   /FeeType 1字节 int 计费类型   /FeeValue 6字节 text 取值范围0-99999,该条短消息的收费值,单位为分,由SP定义对于包月制收费的用户,该值为月租费的值   /GivenValue 6字节 text 取值范围0-99999,赠送用户的话费,单位为分,由SP定义,特

20、指由SP向用户发送广告时的赠送话费   /AgentFlag 1字节 int 代收费标志,0:应收;1:实收   /MorelatetoMTFlag 1字节 int 引起MT消息的原因0-M O点播引起的第一条MT消息;1-MO点播引起的非第一条MT消息;2-非MO点播引起的MT消息;3-系统反馈引起的MT消息。   /Priority 1字节 int 优先级0-9从低到高,默认为0   /ExpireTime 16字节 text 短消息寿命的终止时间,如果为空,表示使用短消

21、息中心的缺省值。时间内容为16个字符,格式为”yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间   /ScheduleTime 16字节 text 短消息定时发送的时间,如果为空,表示立刻发送该短消息。时间内容为16个字符,格式为“yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间   /ReportFlag 1字节 int 状态报告标记0-该条消息只有最后出错时要返回状态报告1-该条消息无论最后是否成功都要返回状态报告2-该条消息不需要返回

22、状态报告3-该条消息仅携带包月计费信息,不下发给用户,要返回状态报告其它-保留缺省设置为0   /TP_pid 1字节 int GSM协议类型。详细解释请参考GSM03.40中的   /TP_udhi 1字节 int GSM协议类型。详细解释请参考GSM03.40中的3,仅使用1位,右对齐   /MessageCoding 1字节 int 短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码其它参见GSM3.38第4节:SMS D

23、ata Coding Scheme   /MessageType 1字节 int 信息类型:0-短消息信息其它:待定   /MessageLength 4字节 int 短消息的长度   /MessageContent Text 短消息的内容   /Reserve 8字节 text 保留,扩展用                &#

24、160; /feetype   /计费类别定义   /用户计费类别 描述   /0 “短消息类型”为“发送”,对“计费用户号码”不计信息费,此类话单仅用于核减SP对称的信道费   /1 对“计费用户号码”免费  2 对“计费用户号码”按条计信息费 3 对“计费用户号码”按包月收取信息费 4 对“计费用户号码”的收费是由SP实现      

25、          byte sp_number=Encoding.ASCII.GetBytes(o_string(SPNumber,21);       byte cou=1;    if (!(UserNumber.Substring(0,2).Equals("86")    UserNumber="86"+UserN

26、umber;   /还没有加入手机号是否正确的识别   byte user_number=Encoding.ASCII.GetBytes(o_string(UserNumber,21);   if(ChargeNumber.Length<1)  /chargenumber不写的话,默认为接收手机计费    ChargeNumber=UserNumber;   else    

27、   if (!(ChargeNumber.Substring(0,2).Equals("86")     ChargeNumber="86"+ChargeNumber;       byte charge_number=Encoding.ASCII.GetBytes(o_string(ChargeNumber,21);   byte corp_id=Encoding.A

28、SCII.GetBytes(o_string(CorpId,5);   byte service_type=Encoding.ASCII.GetBytes(o_string(ServiceType,10);   byte fee_value="/Encoding.ASCII.GetBytes(o_string(FeeValue,6);   byte" given_value="/Encoding.ASCII.GetBytes(p

29、o_string(""",6); /不填写   byte agent_flag=1; /实收   byte more_late_to_mtflag;   switch (FeeType)  /对momt_flag做处理       case 1: /对“计费用户号码”免费         

30、60;        more_late_to_mtflag=0;     break;    case 2:  /对“计费用户号码”按条计信息费     more_late_to_mtflag=0;     break;    case 3: /对“计费用户号码”按包月收取信息费

31、0;                   more_late_to_mtflag=3;       break;    default:      more_late_to_mtflag=0;       br

32、eak;      /if (feetype=1) /2按条计信息费     / more_late_to_mtflag=0; /暂时写成这个 包月时写3   byte priority=0;   byte expire_time=Encoding.ASCII.GetBytes(o_string("",16);/不填写   byte schedule_time=Encod

33、ing.ASCII.GetBytes(o_string("",16);/不填写   byte report_flag=1;/暂时写成这个   byte tp_pid=0;/暂时写成这个,具体的不知道 0 0 8 或者 15   byte tp_udhi=0;/暂时写成这个,具体的不知道   byte message_coding=0;/暂时写成这个,具体的不知道   /MessageCoding 1字节 int

34、 短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码其它参见GSM3.38第4节:SMS Data Coding Scheme   byte message_type=0;   byte message_content=Encoding.Default.GetBytes(MessageContent);  /没有验证内容长度   int message_length=0; /   /byte msg_len=Int2

35、Bytes(uint)message_length); /中转一下数据   byte reserve=Encoding.ASCII.GetBytes(o_string("",8);   int body_length=sp_number.Length+charge_number.Length+1+user_number.Length+corp_id.Length+service_type.Length+1+fee_value.Length;   body_length=

36、body_length+given_value.Length+3+expire_time.Length+schedule_time.Length+5+4+message_content.Length+reserve.Length;   /                           

37、0;           agentflag,morelatetomtflag,priority        pid,udhi.messagetype   message_length=body_length;   /byte msg_len=Int2Bytes(uint)message_length);   byte msg_len=

38、pack.Int2Bytes(uint)message_content.Length);   /消息体   int Message_Length=0;  /4字节   uint Command_ID=0x3;     /4字节   /string Sequence_Number=get_sequence_number(count);  /12字节   byte seq_num=pack.

39、get_sequence_number(count);   /byte msg_len=Int2Bytes(Message_Length);   byte com_id=pack.Int2Bytes(Command_ID);   /byte seq_num=Encoding.Default.GetBytes(Sequence_Number);   /int head_length=msg_len.Length+com_id.Length+seq_num.Length;

40、0;  int head_length=20;   /消息头   Message_Length=head_length+body_length;   byte hmsg_len=pack.Int2Bytes(uint)Message_Length);/重新计算后再写入总体长度             byte content=new byteMessage_Lengt

41、h; /定义总体submit的包长度   /写入数据流中              hmsg_len.CopyTo(content,0);  /4字节     com_id.CopyTo(content,4);   /4字节   seq_num.CopyTo(content,8);  /12字节   

42、;/消息头   sp_number.CopyTo(content,20);  /21字节   charge_number.CopyTo(content,41);  /21字节   content62=cou;  /1字节   user_number.CopyTo(content,63);/21字节   corp_id.CopyTo(content,84);/5字节   service_t

43、ype.CopyTo(content,89);/10字节   content99=FeeType;/1字节   fee_value.CopyTo(content,100);  /6字节   given_value.CopyTo(content,106);/6字节   content112=agent_flag;/1字节   content113=more_late_to_mtflag;/1字节   cont

44、ent114=priority;/1字节   expire_time.CopyTo(content,115);/16字节   schedule_time.CopyTo(content,131);/16字节   content147=report_flag; /1字节   content148=tp_pid; /1字节   content149=tp_udhi; /1字节   content150=message_co

45、ding; /1字节   content151=message_type; /1字节   msg_len.CopyTo(content,152);/4字节   message_content.CopyTo(content,156);/长度为message_length   reserve.CopyTo(content,156+message_content.Length);   /消息体封装完毕   string s

46、eq_num_string=pack.get_seq_num(seq_num); /恢复出seq_num来好写入数据库,做记录用   string add_sql="insert into log_send (spnumber,chargenumber,usernumber,servicetype,feetype,feevalue,content,seq_num) values ("   add_sql=add_sql+"'"+SPNumber+"',"

47、60;  add_sql=add_sql+"'"+ChargeNumber+"',"            add_sql=add_sql+"'"+UserNumber+"',"            add_sql=add_sql+"&

48、#39;"+ServiceType+"',"            add_sql=add_sql+"'"+FeeType.ToString()+"',"   add_sql=add_sql+"'"+FeeValue+"',"   add_sql=add_sql+"&#

49、39;"+MessageContent+"',"   add_sql=add_sql+"'"+seq_num_string+"')"   sqlserver.ExecuteSqlComand(add_sql);   /public byte submit(string SPNumber,string ChargeNumber,string UserNumber,string ServiceType,byte Fe

50、eType,string FeeValue,string MessageContent,string CorpId,uint count)   return content;     public byte unbind(uint count)     int i=0;   uint message_length=20;  /4字节   uint Command_ID=0x2; 

51、0;   /4字节   /string Sequence_Number=get_sequence_number(count);  /12字节   byte seq_num=pack.get_sequence_number(count);   /byte msg_len=Int2Bytes(Message_Length);   byte com_id=pack.Int2Bytes(Command_ID);   byte

52、 msg_len=pack.Int2Bytes(message_length);   /byte seq_num=Encoding.Default.GetBytes(Sequence_Number);   /int head_length=msg_len.Length+com_id.Length+seq_num.Length;   byte content=new bytemessage_length;   Buffer.BlockCopy(msg_len,0,content

53、,i,msg_len.Length);   i+=msg_len.Length;   Buffer.BlockCopy(com_id,0,content,i,com_id.Length);   i+=com_id.Length;   Buffer.BlockCopy(seq_num,0,content,i,seq_num.Length);   /消息头   return content;  原协议

54、  基于专用SGIP的消息定义专用SGIP协议中定义的消息由消息头和消息体组成。项目 说明消息头 消息头部分消息体 消息体部分表4-12消息的组成4.2.2消息头的格式字段 长度(字节) 类型 说明Message Length 4 Integer 消息的总长度(字节)Command ID 4 Integer 命令IDSequence Number 12 Integer 序列号表4-14消息头的格式.1Submit命令的语法字段 长度(字节) 类型 说明SPNumber 21

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论