c代码如下:
void send_keeplive_msg()
{
char buf[255];
//struct protoent *protos;
struct sockaddr_in dest;
int sock_fd;
//protos=getprotobyname("udp");
//sock_fd = socket ( AF_INET, SOCK_DGRAM, protos->p_proto );
sock_fd = socket ( AF_INET, SOCK_DGRAM, 17 ); memset ( &dest, 0x00, sizeof (struct sockaddr_in));
memset ( &dest.sin_zero, 0x00, sizeof (dest.sin_zero) ); dest.sin_family = AF_INET;
dest.sin_port = htons ( DIED_PORT );
dest.sin_addr.s_addr = inet_addr ( server_address);
strcpy(buf,"192.168.0.126");//-------------问题------
sendto(sock_fd,buf,strlen(buf),0,(struct sockaddr *)&dest,sizeof(dest));}
c#代码:
private void diedlisten()
{
int port = 8092;
int ip = 127001;
UdpClient udpclient= new UdpClient ( port ) ;
IPEndPoint receivePoint= new IPEndPoint ( new IPAddress ( ip ) , port ) ;
while ( true )
{
byte[] recData = udpclient.Receive ( ref receivePoint );
ASCIIEncoding encode = new ASCIIEncoding ( ) ; string Read_str = encode.GetString ( recData ) ;
ThreadPool.QueueUserWorkItem(new WaitCallback(procClientDiedHandle), Read_str);
}}现在通信成功了,信息收的也正确,现在问题是c#端想知道运行c程序端的机器的ip地址.我现在的程序采用用发送的信息里包含ip地址,我想不这样,让c#识别出收到的信息是从哪个ip发过来的;或者采用c代码自己取得本机的ip,然后作为消息的内容发到c#机器.
请大家赐教!
void send_keeplive_msg()
{
char buf[255];
//struct protoent *protos;
struct sockaddr_in dest;
int sock_fd;
//protos=getprotobyname("udp");
//sock_fd = socket ( AF_INET, SOCK_DGRAM, protos->p_proto );
sock_fd = socket ( AF_INET, SOCK_DGRAM, 17 ); memset ( &dest, 0x00, sizeof (struct sockaddr_in));
memset ( &dest.sin_zero, 0x00, sizeof (dest.sin_zero) ); dest.sin_family = AF_INET;
dest.sin_port = htons ( DIED_PORT );
dest.sin_addr.s_addr = inet_addr ( server_address);
strcpy(buf,"192.168.0.126");//-------------问题------
sendto(sock_fd,buf,strlen(buf),0,(struct sockaddr *)&dest,sizeof(dest));}
c#代码:
private void diedlisten()
{
int port = 8092;
int ip = 127001;
UdpClient udpclient= new UdpClient ( port ) ;
IPEndPoint receivePoint= new IPEndPoint ( new IPAddress ( ip ) , port ) ;
while ( true )
{
byte[] recData = udpclient.Receive ( ref receivePoint );
ASCIIEncoding encode = new ASCIIEncoding ( ) ; string Read_str = encode.GetString ( recData ) ;
ThreadPool.QueueUserWorkItem(new WaitCallback(procClientDiedHandle), Read_str);
}}现在通信成功了,信息收的也正确,现在问题是c#端想知道运行c程序端的机器的ip地址.我现在的程序采用用发送的信息里包含ip地址,我想不这样,让c#识别出收到的信息是从哪个ip发过来的;或者采用c代码自己取得本机的ip,然后作为消息的内容发到c#机器.
请大家赐教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货