当前位置: 技术问答>linux和unix
研究了一天的鼠年的问题希望牛年能解决,请懂Linux、网络、C Socket的进来
来源: 互联网 发布时间:2016-05-16
本文导语: 小的用88分问给您在牛年来个头彩! 然后请大牛们救救我吧!弄一天了,还是不行。愁死我了 客户端是用C#开发的,接收到Linux发出的消息后马上回射给Liunx没问题。 但是windows这边发消息给linux接收正常,但回射给wind...
小的用88分问给您在牛年来个头彩!
然后请大牛们救救我吧!弄一天了,还是不行。愁死我了
客户端是用C#开发的,接收到Linux发出的消息后马上回射给Liunx没问题。
但是windows这边发消息给linux接收正常,但回射给windows却无反应
另外我是内网,上网看了看好像以前也有recvfrom接收IP混乱的现象,不过不多,请各位高手帮我看看!谢谢了!
一宿没睡就为了解决这个,请大家帮我完成鼠年的夙愿吧!给大家拜年了!
代码如下:
下面是我用tcpdump抓的包
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
07:25:04.054491 arp who-has 192.168.0.2 tell 192.168.0.123
07:25:04.097386 arp reply 192.168.0.2 is-at 00:05:5e:0f:2b:b3 (oui Unknown)
07:25:04.054615 IP 192.168.0.123.59025 > 192.168.0.2.6000: UDP, length 10
07:25:04.056186 arp who-has 192.168.0.1 tell 192.168.0.2
07:25:04.056480 arp reply 192.168.0.1 is-at 00:15:e9:0e:bf:c6 (oui Unknown)
07:25:04.056484 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 31074+ PTR?
2.0.168.192.in-addr.arpa. (42)
07:25:04.076249 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 31074
NXDomain* 0/1/0 (92)
07:25:04.076384 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 32559+ PTR?
123.0.168.192.in-addr.arpa. (44)
07:25:04.097328 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 32559
NXDomain* 0/1/0 (94)
07:25:04.097561 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 54294+ PTR?
1.0.168.192.in-addr.arpa. (42)
07:25:04.117304 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 54294
NXDomain* 0/1/0 (92)
07:25:04.117519 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 32556+ PTR?
20.0.106.202.in-addr.arpa. (43)
07:25:04.137279 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 32556 1/0/0
(75)
13 packets captured
26 packets received by filter
不知道为什么还有DNS的事,gjjline.bta.net.cn.domain是我的DNS域
win(client)端是192.168.0.123,linux(server)是192.168.0.2
然后请大牛们救救我吧!弄一天了,还是不行。愁死我了
客户端是用C#开发的,接收到Linux发出的消息后马上回射给Liunx没问题。
但是windows这边发消息给linux接收正常,但回射给windows却无反应
另外我是内网,上网看了看好像以前也有recvfrom接收IP混乱的现象,不过不多,请各位高手帮我看看!谢谢了!
一宿没睡就为了解决这个,请大家帮我完成鼠年的夙愿吧!给大家拜年了!
代码如下:
#include
#include
#include
#include
#include
#include
#define MAXLINE 80
#define SERV_PORT 6000
void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for(;;)
{
memset(mesg,0x00,sizeof(mesg));
len = clilen;
/* waiting for receive data */
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
printf("%s:%sn",inet_ntoa(pcliaddr),mesg);//这句肯定是错的,怎么写才能看到正确的IP?
sendto(sockfd,mesg,n,0,pcliaddr,len);
}
}
int main(void)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_LOCAL;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6000);
/* bind address and port to socket */
if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind error");
exit(1);
}
do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
return 0;
}
下面是我用tcpdump抓的包
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
07:25:04.054491 arp who-has 192.168.0.2 tell 192.168.0.123
07:25:04.097386 arp reply 192.168.0.2 is-at 00:05:5e:0f:2b:b3 (oui Unknown)
07:25:04.054615 IP 192.168.0.123.59025 > 192.168.0.2.6000: UDP, length 10
07:25:04.056186 arp who-has 192.168.0.1 tell 192.168.0.2
07:25:04.056480 arp reply 192.168.0.1 is-at 00:15:e9:0e:bf:c6 (oui Unknown)
07:25:04.056484 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 31074+ PTR?
2.0.168.192.in-addr.arpa. (42)
07:25:04.076249 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 31074
NXDomain* 0/1/0 (92)
07:25:04.076384 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 32559+ PTR?
123.0.168.192.in-addr.arpa. (44)
07:25:04.097328 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 32559
NXDomain* 0/1/0 (94)
07:25:04.097561 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 54294+ PTR?
1.0.168.192.in-addr.arpa. (42)
07:25:04.117304 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 54294
NXDomain* 0/1/0 (92)
07:25:04.117519 IP 192.168.0.2.32937 > gjjline.bta.net.cn.domain: 32556+ PTR?
20.0.106.202.in-addr.arpa. (43)
07:25:04.137279 IP gjjline.bta.net.cn.domain > 192.168.0.2.32937: 32556 1/0/0
(75)
13 packets captured
26 packets received by filter
不知道为什么还有DNS的事,gjjline.bta.net.cn.domain是我的DNS域
win(client)端是192.168.0.123,linux(server)是192.168.0.2
|
printf("%s:%sn",inet_ntoa(pcliaddr),mesg);//这句肯定是错的,怎么写才能看到正确的IP?
====================================================
把struct sockaddr_in cliaddr 这变量改为全局变量。然后
printf("%s:%sn", inet_ntoa(cliaddr.sin_addr), mesg);
====================================================
把struct sockaddr_in cliaddr 这变量改为全局变量。然后
printf("%s:%sn", inet_ntoa(cliaddr.sin_addr), mesg);
|
??