socket通信例子

上传人:仙*** 文档编号:154922652 上传时间:2022-09-22 格式:DOC 页数:10 大小:55.50KB
返回 下载 相关 举报
socket通信例子_第1页
第1页 / 共10页
socket通信例子_第2页
第2页 / 共10页
socket通信例子_第3页
第3页 / 共10页
点击查看更多>>
资源描述
socket通信例子public void send()while (true) string sql=select top 100 * from send;SqlDataReader datareader =sqlserver.ExecuteSqlDataReader(sql);if (datareader.HasRows) /有记录才进行网关联接string show;string host;/int port =Convert.ToInt16(8801);/host = Dns.GetHostName();/host = 192.168.123.110;socket temp=new socket();Socket send=temp.connectSocket(this.remote_ip,this.remote_port);if (send = null)this.send_listbox(打开远程SMG失败);elsethis.send_listbox(打开远程SMG成功);byte bind_content=sgip_test.bind(count);count_step();byte receive=new byte256;byte flag;send.Send(bind_content,bind_content.Length,0); /bindsend.Receive(receive,29,0); /bind_resp 读取29位flag=sgip_test.bind_resp_result(receive);if (flag=0)this.send_listbox(成功联接到网关);elsethis.send_listbox(联接网关失败);while (datareader.Read()string spnumber=datareaderspnumber.ToString().Trim();string chargenumber=datareaderchargenumber.ToString().Trim();string usernumber=datareaderusernumber.ToString().Trim();string servicetype=datareaderservicetype.ToString().Trim();byte feetype=Byte.Parse(datareaderfeetype.ToString().Trim();string feevalue=/datareaderfeevalue.ToString().Trim();string content=datareadercontent.ToString().Trim();byte submit=sgip_test.submit(spnumber,chargenumber,usernumber,servicetype,feetype,feevalue,content,CorpId,count);send.Send(submit,submit.Length,0);/sumbmitcount_step();send.Receive(receive,29,0); /submit_respflag=sgip_test.submit_resp_result(receive);if (flag=0)this.send_listbox(手机号:+usernumber+ 业务代码:+servicetype+ SP下行号码:+spnumber);this.send_listbox(消息内容:+content);this.send_listbox(消息发送成功);string del_sql=delete from send where id=+datareaderid.ToString().Trim();sqlserver.ExecuteSqlComand(del_sql);elsethis.send_listbox(消息发送失败); datareader.Close();byte unbind=sgip_test.unbind(count);count_step();send.Send(unbind,unbind.Length,0);/unbindsend.Receive(receive,20,0);/unbind_resp 只有消息头send.Close();temp=null; elsedatareader.Close(); /如果没有记录的话,就马上删除联接,不然要超时出错 Thread.Sleep(100);数据写入字节流 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的接入号码 /ChargeNumber 21字节 text付费号码,手机号码前加“86”国别标志;当且仅当群发且对用户收费时为空;如果为空,则该条短消息产生的费用由UserNumber代表的用户支付;如果为全零字符串“000000000000000000000”,表示该条短消息产生的费用由SP支付。/UserCount 1字节 int 接收短消息的手机数量,取值范围1至100/UserNumber 21字节 text 接收该短消息的手机号,该字段重复UserCount指定的次数,手机号码前加“86”国别标志/CorpId 5字节 text 企业代码,取值范围0-99999/ServiceType 10字节 text 业务代码,由SP定义/FeeType 1字节 int 计费类型/FeeValue 6字节 text 取值范围0-99999,该条短消息的收费值,单位为分,由SP定义对于包月制收费的用户,该值为月租费的值/GivenValue 6字节 text 取值范围0-99999,赠送用户的话费,单位为分,由SP定义,特指由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 短消息寿命的终止时间,如果为空,表示使用短消息中心的缺省值。时间内容为16个字符,格式为”yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间/ScheduleTime 16字节 text 短消息定时发送的时间,如果为空,表示立刻发送该短消息。时间内容为16个字符,格式为“yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间/ReportFlag 1字节 int 状态报告标记0-该条消息只有最后出错时要返回状态报告1-该条消息无论最后是否成功都要返回状态报告2-该条消息不需要返回状态报告3-该条消息仅携带包月计费信息,不下发给用户,要返回状态报告其它-保留缺省设置为0/TP_pid 1字节 int GSM协议类型。详细解释请参考GSM03.40中的9.2.3.9/TP_udhi 1字节 int GSM协议类型。详细解释请参考GSM03.40中的9.2.3.23,仅使用1位,右对齐/MessageCoding 1字节 int 短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码其它参见GSM3.38第4节:SMS Data Coding Scheme/MessageType 1字节 int 信息类型:0-短消息信息其它:待定/MessageLength 4字节 int 短消息的长度/MessageContent Text 短消息的内容/Reserve 8字节 text 保留,扩展用 /feetype/计费类别定义/用户计费类别描述/0“短消息类型”为“发送”,对“计费用户号码”不计信息费,此类话单仅用于核减SP对称的信道费/1对“计费用户号码”免费 2对“计费用户号码”按条计信息费 3对“计费用户号码”按包月收取信息费 4对“计费用户号码”的收费是由SP实现 byte sp_number=Encoding.ASCII.GetBytes(pack.into_string(SPNumber,21); byte cou=1; if (!(UserNumber.Substring(0,2).Equals(86)UserNumber=86+UserNumber; /还没有加入手机号是否正确的识别byte user_number=Encoding.ASCII.GetBytes(pack.into_string(UserNumber,21);if(ChargeNumber.Length1) /chargenumber不写的话,默认为接收手机计费ChargeNumber=UserNumber;elseif (!(ChargeNumber.Substring(0,2).Equals(86)ChargeNumber=86+ChargeNumber; byte charge_number=Encoding.ASCII.GetBytes(pack.into_string(ChargeNumber,21);byte corp_id=Encoding.ASCII.GetBytes(pack.into_string(CorpId,5);byte service_type=Encoding.ASCII.GetBytes(pack.into_string(ServiceType,10);byte fee_value=/Encoding.ASCII.GetBytes(pack.into_string(FeeValue,6);byte given_value=/Encoding.ASCII.GetBytes(pack.into_string(,6); /不填写byte agent_flag=1; /实收byte more_late_to_mtflag;switch (FeeType) /对momt_flag做处理case 1: /对“计费用户号码”免费 more_late_to_mtflag=0;break;case 2: /对“计费用户号码”按条计信息费more_late_to_mtflag=0;break;case 3: /对“计费用户号码”按包月收取信息费 more_late_to_mtflag=3; break;default: more_late_to_mtflag=0; break;/if (feetype=1) /2按条计信息费 / more_late_to_mtflag=0; /暂时写成这个 包月时写3byte priority=0;byte expire_time=Encoding.ASCII.GetBytes(pack.into_string(,16);/不填写byte schedule_time=Encoding.ASCII.GetBytes(pack.into_string(,16);/不填写byte report_flag=1;/暂时写成这个byte tp_pid=0;/暂时写成这个,具体的不知道 0 0 8 或者 15byte tp_udhi=0;/暂时写成这个,具体的不知道byte message_coding=0;/暂时写成这个,具体的不知道/MessageCoding 1字节 int 短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码其它参见GSM3.38第4节:SMS Data Coding Schemebyte message_type=0;byte message_content=Encoding.Default.GetBytes(MessageContent); /没有验证内容长度int message_length=0; /byte msg_len=Int2Bytes(uint)message_length); /中转一下数据byte reserve=Encoding.ASCII.GetBytes(pack.into_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=body_length+given_value.Length+3+expire_time.Length+schedule_time.Length+5+4+message_content.Length+reserve.Length;/ agentflag,morelatetomtflag,priority pid,udhi.messagetypemessage_length=body_length;/byte msg_len=Int2Bytes(uint)message_length);byte msg_len=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.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;int head_length=20;/消息头Message_Length=head_length+body_length;byte hmsg_len=pack.Int2Bytes(uint)Message_Length);/重新计算后再写入总体长度 byte content=new byteMessage_Length; /定义总体submit的包长度/写入数据流中 hmsg_len.CopyTo(content,0); /4字节 com_id.CopyTo(content,4); /4字节seq_num.CopyTo(content,8); /12字节/消息头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_type.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字节content114=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_coding; /1字节content151=message_type; /1字节msg_len.CopyTo(content,152);/4字节message_content.CopyTo(content,156);/长度为message_lengthreserve.CopyTo(content,156+message_content.Length);/消息体封装完毕string seq_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+,;add_sql=add_sql+ChargeNumber+,; add_sql=add_sql+UserNumber+,; add_sql=add_sql+ServiceType+,; add_sql=add_sql+FeeType.ToString()+,;add_sql=add_sql+FeeValue+,;add_sql=add_sql+MessageContent+,;add_sql=add_sql+seq_num_string+);sqlserver.ExecuteSqlComand(add_sql);/public byte submit(string SPNumber,string ChargeNumber,string UserNumber,string ServiceType,byte FeeType,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; /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 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,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;原协议 基于专用SGIP的消息定义专用SGIP协议中定义的消息由消息头和消息体组成。项目说明消息头消息头部分消息体消息体部分表4-12消息的组成4.2.2消息头的格式字段长度(字节)类型说明Message Length4Integer消息的总长度(字节)Command ID4Integer命令IDSequence Number12Integer序列号表4-14消息头的格式4.2.3.3.1Submit命令的语法字段长度(字节)类型说明SPNumber21TextSP的接入号码ChargeNumber21Text付费号码,手机号码前加“86”国别标志;当且仅当群发且对用户收费时为空;如果为空,则该条短消息产生的费用由UserNumber代表的用户支付;如果为全零字符串“000000000000000000000”,表示该条短消息产生的费用由SP支付。UserCount1Integer接收短消息的手机数量,取值范围1至100UserNumber21Text接收该短消息的手机号,该字段重复UserCount指定的次数,手机号码前加“86”国别标志CorpId5Text企业代码,取值范围0-99999ServiceType10Text业务代码,由SP定义FeeType1Integer计费类型FeeValue6Text取值范围0-99999,该条短消息的收费值,单位为分,由SP定义对于包月制收费的用户,该值为月租费的值GivenValue6Text取值范围0-99999,赠送用户的话费,单位为分,由SP定义,特指由SP向用户发送广告时的赠送话费AgentFlag1Integer代收费标志,0:应收;1:实收MorelatetoMTFlag1Integer引起MT消息的原因0-MO点播引起的第一条MT消息;1-MO点播引起的非第一条MT消息;2-非MO点播引起的MT消息;3-系统反馈引起的MT消息。Priority1Integer优先级0-9从低到高,默认为0ExpireTime16Text短消息寿命的终止时间,如果为空,表示使用短消息中心的缺省值。时间内容为16个字符,格式为”yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间ScheduleTime16Text短消息定时发送的时间,如果为空,表示立刻发送该短消息。时间内容为16个字符,格式为“yymmddhhmmsstnnp” ,其中“tnnp”取固定值“032+”,即默认系统为北京时间ReportFlag1Integer状态报告标记0-该条消息只有最后出错时要返回状态报告1-该条消息无论最后是否成功都要返回状态报告2-该条消息不需要返回状态报告3-该条消息仅携带包月计费信息,不下发给用户,要返回状态报告其它-保留缺省设置为0TP_pid1IntegerGSM协议类型。详细解释请参考GSM03.40中的9.2.3.9TP_udhi1IntegerGSM协议类型。详细解释请参考GSM03.40中的9.2.3.23,仅使用1位,右对齐MessageCoding1Integer短消息的编码格式。0:纯ASCII字符串3:写卡操作4:二进制编码8:UCS2编码15: GBK编码其它参见GSM3.38第4节:SMS Data Coding SchemeMessageType1Integer信息类型:0-短消息信息其它:待定MessageLength4Integer短消息的长度MessageContentMessage LengthText短消息的内容Reserve8Text保留,扩展用表4-17 Submit消息格式开始才做这方面的通信程序的时候觉得很难.无处下手.后来查了一资料与测试后.就慢慢的把程序写出来了.这里帖一些关键的代码出来.希望给大家一点思路. Socket通信就是一个网络通信过程.了解过程后.什么都好办了.
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!