写在前面:小生纯业余选手,开此博仅仅是为了积累,纯当笔记来用。如有看官光临小生博客,请不要相信我的代码就是正确的。如果您发现了错误也恳请耽误您一点时间,请您在下面指出来,不胜感激!
如果发现一些笔记的说法完全是错误的请建议我删除!
有很多陌生而且复杂的API和结构体在若干网络基础问题中截图说明。
说明:第一段程序接收到第二段程序的数据之后会写一段数据。
问题:
1.接收端程序通过bind绑定了进程所在主机的ip地址与port而在发送端程序中并没有通过bind绑定进程所在主机的IP地址和port?(发送端进程不需要绑定,内核在发送数据之前会自己绑定一个IP地址与port,也就是说发送端进程没必要bind也可以正常的发送数据)
2.在接收端进程会写数据给发送端程序的时候,是怎么知道发送端进程的IP地址与port的?
(在接收端进程通过recvfrom函数接收数据的时候同时返回了发送端进程的IP地址与port)
*recvfrom不是只能从指定的IP地址接收数据,它可以从任意IP地址接收数据同时返回发送者的IP地址与port
<pre name="code" class="cpp">#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int fd;
struct sockaddr_in ad;//本机的ip地址
char buf[256];//接收数据的缓冲
struct sockaddr_in ad_snd;//发送数据端IP
socklen_t len;//发送端IP长度
fd = socket(AF_INET,SOCK_DGRAM,17);
if( fd == -1 )
{
printf("socket error:%m\n");
exit(-1);
}else
{
printf("socket complete\n");
}
ad.sin_family = AF_INET;
ad.sin_port = htons(6666);//这里式端口号,为了消除高地址、低地址区别就用了hton
inet_aton("192.168.64.128",&ad.sin_addr);
int r = bind(fd,reinterpret_cast<struct sockaddr*>(&ad),sizeof(ad));
if( r == -1 )
{
printf("bind error:%m\n");
exit(-1);
}else
{
printf("bind complete\n");
}
while(1)
{
len = sizeof(ad_snd);
r = recvfrom(fd,buf,sizeof(buf),0,
reinterpret_cast<struct sockaddr*>(&ad_snd),&len);
if(r>0)
{
buf[r] = '\0';
printf("senderIP:%s,port:%u,data:%s\n",
inet_ntoa(ad_snd.sin_addr),
ntohs(ad_snd.sin_port),buf);
sendto(fd,"feedback",strlen("feedback"),0,
reinterpret_cast<struct sockaddr*>(&ad_snd),sizeof(ad_snd));
}else if( r == 0)
{
printf("close\n");
break;
}else
{
printf("error\n");
}
}
close(fd);
return 0;
}
<pre name="code" class="cpp">#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int fd;
struct sockaddr_in ad;//本机的ip地址
char buf[256];//接收数据的缓冲
fd = socket(AF_INET,SOCK_DGRAM,17);
if( fd == -1 )
{
printf("socket error:%m\n");
exit(-1);
}else
{
printf("socket complete\n");
}
ad.sin_family = AF_INET;
ad.sin_port = htons(6666);//这里式端口号,为了消除高地址、低地址区别就用了htons
inet_aton("192.168.64.128",&ad.sin_addr);
while(1)
{
int r = read(0,buf,sizeof(buf));
if(r < 0)
break;
buf[r] = '\0';
r = sendto(fd,buf,r,0,
reinterpret_cast<struct sockaddr*>(&ad),sizeof(ad));
bzero(buf,sizeof(buf));
r = recv(fd,buf,sizeof(buf),0);
buf[r] = '\0';
printf("comefrom recver:%s\n",buf);
if( r == -1 )
break;
}
close(fd);
return 0;
}
注意:
connect + send 是否就是sendto,上面第二段程序可以改为如下
<pre name="code" class="cpp">#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int fd;
struct sockaddr_in ad;//本机的ip地址
char buf[256];//接收数据的缓冲
fd = socket(AF_INET,SOCK_DGRAM,17);
if( fd == -1 )
{
printf("socket error:%m\n");
exit(-1);
}else
{
printf("socket complete\n");
}
ad.sin_family = AF_INET;
ad.sin_port = htons(6666);//这里式端口号,为了消除高地址、低地址区别就用了htons
inet_aton("192.168.64.128",&ad.sin_addr);
//这里有一个连接过程
int r = connect(fd,reinterpret_cast<struct sockaddr*>(&ad),sizeof(ad));
while(1)
{
int r = read(0,buf,sizeof(buf)-1);
if(r < 0)
break;
buf[r] = '\0';
<span style="white-space:pre"></span><pre name="code" class="cpp" style="font-size: 18px; line-height: 26px;"><span style="white-space:pre"> </span>r = send(fd,buf,r,0);
<span style="white-space:pre"> </span>/*
r = sendto(fd,buf,r,0, reinterpret_cast<struct sockaddr*>(&ad),sizeof(ad));
<span style="white-space:pre"> </span>*/
bzero(buf,sizeof(buf));
r = recv(fd,buf,sizeof(buf),0);
buf[r] = '\0';
printf("comefrom recver:%s\n",buf);
if( r == -1 )
break;
}
close(fd);
return 0;
}