寫在前面:小生純業餘選手,開此博僅僅是爲了積累,純當筆記來用。如有看官光臨小生博客,請不要相信我的代碼就是正確的。如果您發現了錯誤也懇請耽誤您一點時間,請您在下面指出來,不勝感激!
如果發現一些筆記的說法完全是錯誤的請建議我刪除!
有很多陌生而且複雜的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;
}