如何输写报文??? 学习中,就是如何从客户端发报文往服务器,初学网络编程方面的知识,不是很了解,比如格式是什么之类的,谢谢大家帮帮忙告诉一下.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以用SOCKET通信机制,定义好协议,服务器端开个监听,当客户端发送数据到服务器,服务器接接受了。 这是一个简单的收发报文例子,这个是客户端的代码,有高手能帮帮我把几个重要的地方加上注释么?谢谢了...#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdlib.h>int port = 5678;int main(void){int socket_descripter,s_len;int on,i;char message[256];struct sockaddr_in s_addr;s_len=sizeof(s_addr);socket_descripter=socket(AF_INET,SOCK_DGRAM,0);if(socket_descripter<0){ printf("socket created error\n"); exit(0);}/* Allow processes to use this same port:*/on=1;if(setsockopt(socket_descripter,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0){ printf("error in setsockopt:SO_REUSEADDR"); exit(0);}memset(&s_addr, 0,sizeof(s_addr));s_addr.sin_family=AF_INET;s_addr.sin_port=htons(port);s_addr.sin_addr.s_addr=inet_addr("100.0.0.123");if(bind(socket_descripter,(struct sockaddr *)&s_addr,sizeof(s_addr))<0){ printf("error in binding\n"); exit(0);}for(i=0;i<5;i++){ if(recvfrom(socket_descripter, message, 256, 0, (struct sockaddr *)&s_addr, &s_len)<0){ printf("error in recvfrom!\n"); exit(0); } else {printf("recv success\n"); sleep(1); }}} 报文其实就是字符串而已,只不过这个串由CLIENT和APPSERVER之间定下的协议而已。比如前几位是什么意思,后几位是什么意思。打个比方:比如你发个010jiezi316123456给服务器,服务器收到这个信息后,知道前三位是表示ID,后面8位表示USERNAEM,再后面6位表示密码。 原来说来说去你最后还是SOCKET通信啊,呵呵,其实很简单,就是封包而已。首先你需要知道你的服务器端要的数据格式是什么,其次呢,你就开始封,所谓封包其实就是转化成字节流,每个通信都有自己的协议规定的,你只需要按照协议进行封就OK了。比如最终封成一个Byte[] b = new Byte[80];其实就是说你把那些东西变成了80个字节,而这些是有顺序的。对于字节这东西你应该知道吧?比如整型4个字节,字符1个字节,网络字节序和主机字节序等也应该了解的,呵呵! 谢谢大家的帮忙,我听他们说把报文写成xml的形式的,这是怎么回事,不是说报文是字节流么? 晕了啊,你直接问他们什么格式的不就OK了,如果转化成XML的那么基本可以判定他们不是使用的这种方式。很可能是HTTP或者WebService,他们要的什么你搞明白了么? 把一个xml格式文件转换成一个字节流. 不好意思,一直有事才来看贴子,同事说了,是socket通信的,知道这种的报文怎么写么? 封装成一个byte[]然后send出去就OK了 服务器端:using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Sockets;namespace UDPServer{ class Program { static void Main(string[] args) { int recv; byte[] data = new byte[1024]; //构建TCP 服务器 //得到本机IP,设置TCP端口号 IPEndPoint ipep = new IPEndPoint(IPAddress.Any , 8001); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp); //绑定网络地址 newsock.Bind(ipep); Console.WriteLine("This is a Server, host name is {0}",Dns.GetHostName()); //等待客户机连接 Console.WriteLine("Waiting for a client"); //得到客户机IP IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); recv = newsock.ReceiveFrom(data, ref Remote); Console .WriteLine ("Message received from {0}: ", Remote.ToString ()); Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv )); //客户机连接成功后,发送欢迎信息 string welcome = "Welcome ! "; //字符串与字节数组相互转换 data = Encoding .ASCII .GetBytes (welcome ); //发送信息 newsock .SendTo (data ,data.Length ,SocketFlags .None ,Remote ); while (true ) { data =new byte [1024]; //发送接受信息 recv =newsock.ReceiveFrom(data ,ref Remote); Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv)); newsock .SendTo (data ,recv ,SocketFlags .None ,Remote ); } } }}客户端:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;namespace UDPClient{ class Program { static void Main(string[] args) { byte[] data = new byte[1024]; string input ,stringData; //构建TCP 服务器 Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName()); //设置服务IP,设置TCP端口号 IPEndPoint ipep = new IPEndPoint(IPAddress .Parse ("127.0.0.1") , 8001); //定义网络类型,数据连接类型和网络协议UDP Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string welcome = "Hello! "; data = Encoding.ASCII.GetBytes(welcome); server.SendTo(data, data.Length, SocketFlags.None, ipep); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; data = new byte[1024]; //对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制 //server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100); int recv = server.ReceiveFrom(data, ref Remote); Console.WriteLine("Message received from {0}: ", Remote.ToString()); Console.WriteLine(Encoding .ASCII .GetString (data,0,recv)); while (true) { input = Console .ReadLine (); if (input =="exit") break ; server .SendTo (Encoding .ASCII .GetBytes (input ),Remote ); data = new byte [1024]; recv = server.ReceiveFrom(data, ref Remote); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); } Console .WriteLine ("Stopping Client."); server .Close (); } }http://www.cnblogs.com/zhangpengshou/archive/2009/02/03/1307622.html 在datagridview显示的结果中继续查找 string转datime的方法 c#读取字节数组,快来帮忙看下 版本转换错误,请高手明示,谢谢!! FORM传值 未找到反序列化构造函数(高分求救) 求C#语言下 的ADO.NET 数据库编程的书籍或资料或例程!!! 在WinForm中, 怎样获得DataGrid中所选中的多行 天啦。现在都2008年了。我这辈子真的要废了。 冒泡排序 软件开发如何能得到政府的扶植 想用C#正则替换<%=Filename%>中替换Filename为其它怎么写?
谢谢了...
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
int port = 5678;
int main(void)
{
int socket_descripter,s_len;
int on,i;
char message[256];
struct sockaddr_in s_addr;
s_len=sizeof(s_addr);
socket_descripter=socket(AF_INET,SOCK_DGRAM,0);
if(socket_descripter<0){
printf("socket created error\n");
exit(0);
}
/* Allow processes to use this same port:*/
on=1;
if(setsockopt(socket_descripter,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0){
printf("error in setsockopt:SO_REUSEADDR");
exit(0);
}
memset(&s_addr, 0,sizeof(s_addr));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(port);
s_addr.sin_addr.s_addr=inet_addr("100.0.0.123");
if(bind(socket_descripter,(struct sockaddr *)&s_addr,sizeof(s_addr))<0){
printf("error in binding\n");
exit(0);
}
for(i=0;i<5;i++){
if(recvfrom(socket_descripter, message, 256, 0, (struct sockaddr *)&s_addr, &s_len)<0){
printf("error in recvfrom!\n");
exit(0);
}
else {printf("recv success\n");
sleep(1);
}
}
}
报文其实就是字符串而已,只不过这个串由CLIENT和APPSERVER之间定下的协议而已。
比如前几位是什么意思,后几位是什么意思。
打个比方:
比如你发个010jiezi316123456给服务器,服务器收到这个信息后,知道前三位是表示ID,后面8位表示USERNAEM,再后面6位表示密码。
首先你需要知道你的服务器端要的数据格式是什么,其次呢,你就开始封,所谓封包其实就是转化成字节流,每个通信都有自己的协议规定的,你只需要按照协议进行封就OK了。
比如最终封成一个Byte[] b = new Byte[80];
其实就是说你把那些东西变成了80个字节,而这些是有顺序的。对于字节这东西你应该知道吧?比如整型4个字节,字符1个字节,网络字节序和主机字节序等也应该了解的,呵呵!
很可能是HTTP或者WebService,他们要的什么你搞明白了么?
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace UDPServer
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024]; //构建TCP 服务器 //得到本机IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Any , 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp); //绑定网络地址
newsock.Bind(ipep); Console.WriteLine("This is a Server, host name is {0}",Dns.GetHostName()); //等待客户机连接
Console.WriteLine("Waiting for a client"); //得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console .WriteLine ("Message received from {0}: ", Remote.ToString ());
Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv )); //客户机连接成功后,发送欢迎信息
string welcome = "Welcome ! "; //字符串与字节数组相互转换
data = Encoding .ASCII .GetBytes (welcome ); //发送信息
newsock .SendTo (data ,data.Length ,SocketFlags .None ,Remote );
while (true )
{
data =new byte [1024];
//发送接受信息
recv =newsock.ReceiveFrom(data ,ref Remote);
Console .WriteLine (Encoding .ASCII .GetString (data ,0,recv));
newsock .SendTo (data ,recv ,SocketFlags .None ,Remote );
}
} }
}客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace UDPClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input ,stringData; //构建TCP 服务器 Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName()); //设置服务IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress .Parse ("127.0.0.1") , 8001); //定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string welcome = "Hello! ";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender; data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding .ASCII .GetString (data,0,recv));
while (true)
{
input = Console .ReadLine ();
if (input =="exit")
break ;
server .SendTo (Encoding .ASCII .GetBytes (input ),Remote );
data = new byte [1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console .WriteLine ("Stopping Client.");
server .Close ();
} }http://www.cnblogs.com/zhangpengshou/archive/2009/02/03/1307622.html