Transcript recvfrom()
UDP-echo通信のモデル
Python socket module: Just a thin wrapper over the underlying C library's socket calls.
UDP-echo通信のモデル
UDP-echo通信のモデルとプログラミングための背景知識
UDPプロトコル
UDP-echoのデータ通信手順
UDPで利用するソケット関数(Cプログラム)
TCPとUDPでのサーバソケットの役割の違い
UDPソケットの簡略化されたメッセージ送受信
UDP-echo動作テスト( Cプログラム・Client/Serverは個別の端末)
UDP-echo動作テスト( Cプログラム・Client/Serverは端末を共有)
UDP-echo動作テスト(Pythonプログラム・Client/Serverは個別の端末)
UDP-echo通信のモデル
UDPプロトコル
UDP機能
IP層にホストアドレスと
は別のアドレッシング
(ポート番号)層を追加
破損したデータグラムは、
廃棄
UDPを利用する場合は、
データグラムの紛失、順
番の入替わり有に対処
する必要有
データの信頼性の確保
はAPの責任
AP
AP
ソケット
ソケット
UDP
IP
ホスト
TCP
TCP
通信
チャネル
IP
ルータ
通信
チャネル
UDP
IP
ホスト
UDP-echo通信のモデル
UDP-echoのデータ通信手順
UDPEchoClient
UDPEchoServer
ソケットリンク接続
socket()
sendto()
socket()
bind()
Echo
ソケット作成
ソケットにポート
番号を割当る
recvfrom()
データ通信を行う
recvfrom()
Echo
sendto()
ソケットリンク開放
close()
close()
ソケット削除
UDP-echo通信のモデル
UDPで利用するソケット関数
(Cプログラム)
•
送信時、アドレスを指定(複数のアドレスへ送信可能)
宛先アドレスを指定
int sendto(int socket, const void *msg, unsigned int msgLength, int flags, struct sockaddr
*destAddr, unsigned int addrLen)
•
受信時、送信元アドレスを通知(複数のアドレスから受信可能)
int recvfrom(int socket, const void *msg, unsigned int msgLength, int flags, struct sockaddr
*srcAddr, unsigned int *addrLen)
受信メッセージの送信元アドレス
APプログラム
*srcAddr
recvfrom(..,*srcAddr,…)
システム
コール
書込む
メッセージ受信
*addrLen
UDP-echo通信のモデル
TCPとUDPでのサーバソケットの役割の違い
UDPソケットは作成後、すぐに送受信に使用できる
(接続を確立しなくてもよい)
送信時、アドレスを指定(複数のアドレスへ送信可能)
受信時、送信元アドレスを通知(複数のアドレスから受信可能)
サーバ
プログラム
サーバ
プログラム
サーバの
ソケット
listen()
accept()
sendto()
recvfrom()
accept()
サーバのソケット
クライアント2用
ソケット
TCP
クライアント1
クライアント2
UDP
クライアント1
クライアント2
UDP-echo通信のモデル
UDPソケットの簡略化されたメッセージ送受信
メッセージ境界
sendto()とrecvfrom()は1対1
recvfrom()実行時、メッセージが受信バッファ長よりも大きい
場合、受信バッファ長だけ受信
sendto()
メッセージ
recvfrom()
メッセージ
AP
プログラム
UDP
UDPのソケットオブジェクトには、
複数メッセージをまとめたり、1
メッセージを分割したりする機能
は具備されていない。
受信バッファ
recvfrom()
メッセージ
コピー
廃棄
UDP-echo通信のモデル
UDP-echo動作テスト
( Cプログラム・Client/Serverは個別の端末)
UDPEchoClient
$ gcc -o UDPEchoServer UDPEchoServer.c
DieWithError.c
$ ./UDPEchoServer 5000
Handling client 150.43.220.22
Handling client 150.43.220.22
$ gcc -o UDPEchoClient UDPEchoClient.c DieWithError.c
$ ./UDPEchoClient 150.43.220.22 “Echo" 5000
Received: Echo
$ ./UDPEchoClient 150.43.220. 22 “Hello" 5000
Received: Hello
$
UDPEchoServer
Echo
Echo
Hello
Hello
UDP-echo通信のモデル
UDP-echo動作テスト
( Cプログラム・Client/Serverは端末を共有)
UDPEchoClient
$ gcc -o UDPEchoServer UDPEchoServer.c DieWithError.c
$ gcc -o UDPEchoClient UDPEchoClient.c DieWithError.c
$ ./UDPEchoServer 5000 &
[1] 11972
$ ./UDPEchoClient 150.43.220.22 Echo 5000
Handling client 150.43.220.22
Received: Echo
$ ./UDPEchoClient 150.43.220.22 Hello 5000
Handling client 150.43.220.22
Received: Hello
$
UDPEchoServer
Echo
Echo
Hello
Hello
UDP-echo通信のモデル
UDP-echo動作テスト
( Pythonプログラム・Client/Serverは個別の端末)
prompt $N:$D:$S
E:> UDPEchoServer.py
UDPEchoServer 50007 50008
Server received data : Hello network world
Server received data : Hello network world
prompt $N:$D:$S
E:> UDPEchoClient.py
UDPEchoClient localhost 50007
Client received data : Echo>=Hello network world
E:> UDPEchoClient.py
UDPEchoClient localhost 50007
Client received data : Echo>=Hello network world
E:>
UDPEchoClient
UDPEchoServer
Hello network world
Echo>=Hello network world
Hello network world
Echo>=Hello network world