# TCP/UDP
###### tags: `Networking` `TCP` `UDP` `python`
[TOC]
## TCP client
Library
```python
import socket
```
socket is a kind of interface which uses to communicate between the server and client.
## Create the socket object
```python
#Create the socket object
ClientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
```
AF means Address Family, INET use Standard IPv4
## Establish a connection
```python
#Establish connection to the tagetHost
ClientSocket.connect((targetHost,targetPort))
```
## Send a message
```python
#Send a message
ClientSocket.send(b"GET /HTTP/1.1\r\nHost: google.com\r\n\r\n")
```
## Receive a message
```python
#Receive a message
response =ClientSocket.recv(4096)
```
## Output the message
```python
print(response.decode())
ClientSocket.close
```
Notice: we do some supposes,first ,every connection we're going to success, second, the server will wait for us before we send the message,third , the server always can response us immediately.
## Source Code
```python
import socket
targetHost="www.google.com"
targetPort=80
#Create the socket object
ClientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#Establish connection to the tagetHost
ClientSocket.connect((targetHost,targetPort))
#Send a message
ClientSocket.send(b"GET /HTTP/1.1\r\nHost: google.com\r\n\r\n")
#Receive a message
response =ClientSocket.recv(4096)
print(response.decode())
ClientSocket.close
```
---
## UDP Client
Source code
```python
import socket
targetHost="meowhecker.com"
targetPort=80
#Create a socket object
clientSocket= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#send a message
clientSocket.sendto(b"meowhecker",(targetHost,targetPort))
#receive a message
data, reHostPort = clientSocket.recvfrom(4096)
print(data.decode())
clientSocket.close()
```
## TCP Server
The standard multi-threading TCP server
Create socket object
```python
#Create a socket object
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
```
## Listen on ......
```python
serverSocket.bind((ip,Port))
serverSocket.listen(5)
#Show the message
print(f'Listening on {ip} : {Port}')
```
Get clientSocket and ClientINFO
```python
clientSocket, clientINFO=serverSocket.accept()
#Show the client messages
print(f'Accepted connection form {clientINFO[0]}:{clientINFO[1]}')
```
## Create Multi Threading
```python
#Create multithreading
clientHandler = threading.Thread(target=clientJob, args=(clientSocket,))
#Start Multithreading
clientHandler.start()
```
## Job
```python
def clientJob(clientSocket):
with clientSocket as Sock:
request = Sock.recv(4096)
decodeRequest = request.decode()
print(f"Received: {decodeRequest}")
Sock.send(b"ACK")
```
Source code
```python
import socket
import threading
ip = "meowhecker.com"
Port=6969
def main():
#Create a socket object
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#listen on .....
serverSocket.bind((ip,Port))
serverSocket.listen(5)
#Show the message
print(f'Listening on {ip} : {Port}')
#Establish Connections
while 1:
clientSocket, clientINFO=serverSocket.accept()
#Show the client messages
print(f'Accepted connection form {clientINFO[0]}:{clientINFO[1]}')
#Create multithreading
clientHandler = threading.Thread(target=clientJob, args=(clientSocket,))
#Start Multithreading
clientHandler.start()
def clientJob(clientSocket):
with clientSocket as Sock:
request = Sock.recv(4096)
decodeRequest = request.decode()
print(f"Received: {decodeRequest}")
Sock.send(b"ACK")
if __name__ == '__main__':
main()
#there are two ways to use it
# Directely use it
# import to another programe
```
## Test: Server <----->Client
Server

---
Client

---