---
# System prepended metadata

title: 網路程式設計 HW07

---

# 網路程式設計 HW07
## 第一題
### 畫面截圖
![](https://i.imgur.com/QAEkZ5V.png)
### 程式碼
#### 接收
```python=
import socket

localIP = "127.0.0.1"
localPort = 8888
bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

while(True):
    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)#這這裡接受訊息，裡面包含Client的IP位址和所傳送的訊息
    message = bytesAddressPair[0]#Client的訊息
    address = bytesAddressPair[1]#Client的IP位址

    clientMsg = "Message from Client:{}".format(message)#字串中寫Client傳送來的訊息
    clientIP  = "Client IP Address:{}".format(address)#字串中寫Client的IP位址
    
    print(clientMsg)
    print(clientIP)
```
#### 傳送
```python=
import socket
 
msgFromClient = "How are you?"
bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("127.0.0.1", 8888)
bufferSize = 1024

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.sendto(bytesToSend, serverAddressPort)

msgFromServer = UDPClientSocket.recvfrom(bufferSize)

msg = "Message from Server {}".format(msgFromServer[0])

print(msg)
```
## 第二題
### 畫面截圖
![](https://i.imgur.com/FgdPsrT.png)
### 程式碼
#### 接收1
```python=
import socket

localIP = "127.0.0.1"
localPort = 8888
bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

while(True):
    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)#這這裡接受訊息，裡面包含Client的IP位址和所傳送的訊息
    message = bytesAddressPair[0]#Client的訊息
    address = bytesAddressPair[1]#Client的IP位址

    clientMsg = "Message from Client:{}".format(message)#字串中寫Client傳送來的訊息
    clientIP  = "Client IP Address:{}".format(address)#字串中寫Client的IP位址
    
    print(clientMsg)
    print(clientIP)
```
#### 接收2
```python=
import socket

localIP = "127.0.0.1"
localPort = 1234
bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

while(True):
    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)#這這裡接受訊息，裡面包含Client的IP位址和所傳送的訊息
    message = bytesAddressPair[0]#Client的訊息
    address = bytesAddressPair[1]#Client的IP位址

    clientMsg = "Message from Client:{}".format(message)#字串中寫Client傳送來的訊息
    clientIP  = "Client IP Address:{}".format(address)#字串中寫Client的IP位址
    
    print(clientMsg)
    print(clientIP)
```
#### 傳送
```python=
import socket
 
msgFromClient1 = "Good morning"
msgFromClient2 = "Good night"
bytesToSend1 = str.encode(msgFromClient1)
bytesToSend2 = str.encode(msgFromClient2)
serverAddressPort1 = ("127.0.0.1", 8888)
serverAddressPort2 = ("127.0.0.1", 1234)
bufferSize = 1024

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.sendto(bytesToSend1, serverAddressPort1)
UDPClientSocket.sendto(bytesToSend2, serverAddressPort2)
```
## 第三題
### 畫面截圖
![](https://i.imgur.com/1na5Gvi.png)
### 程式碼
#### 傳送(兩個相同)
```python=
import socket
 
msgFromClient = "Good Morning"
bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("127.0.0.1", 8888)
bufferSize = 1024

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.sendto(bytesToSend, serverAddressPort)
```
#### 接收
```python=
import socket

localIP = "127.0.0.1"
localPort = 8888
bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

while(True):
    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)#這這裡接受訊息，裡面包含Client的IP位址和所傳送的訊息
    message = bytesAddressPair[0]#Client的訊息
    address = bytesAddressPair[1]#Client的IP位址

    clientMsg = "Message from Client:{}".format(message)#字串中寫Client傳送來的訊息
    clientIP  = "Client IP Address:{}".format(address)#字串中寫Client的IP位址
    
    print(clientMsg)
    print(clientIP)
```
## 第四題
### 畫面截圖
![](https://i.imgur.com/fQPvxJT.png)
### 程式碼
#### 傳送
```python=
import socket
import time
 
msgFromClient = "Hello UDP Server"
bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("127.0.0.1", 8888)
bufferSize = 1024

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

while True:
    time.sleep(1)#先停止一秒鐘
    UDPClientSocket.sendto(bytesToSend, serverAddressPort)
```
#### 接收
```python=
import socket

localIP = "127.0.0.1"
localPort = 8888
bufferSize = 1024

UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort))

print("UDP server up and listening")

while(True):
    bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)#這這裡接受訊息，裡面包含Client的IP位址和所傳送的訊息
    message = bytesAddressPair[0]#Client的訊息
    address = bytesAddressPair[1]#Client的IP位址

    clientMsg = "Message from Client:{}".format(message)#字串中寫Client傳送來的訊息
    clientIP  = "Client IP Address:{}".format(address)#字串中寫Client的IP位址
    
    print(clientMsg)
    print(clientIP)
```
## 心得
這次前面的題目不算難，但是後面就不會寫了，希望老師在錄影片時，能加進解釋python程式碼的片段，因為我問了好多同學也都說python不會寫