0% found this document useful (0 votes)
94 views5 pages

Pemrograman Jaringan Kisi Kisi

The document discusses sending and receiving data over UDP and TCP in Python. It includes code examples for a UDP broadcaster and receiver, a TCP client and server for single connections, and a multithreaded TCP server that can handle multiple clients simultaneously using select. The server code examples demonstrate how to accept connections, receive data from clients, send responses, and close connections.

Uploaded by

Yogi Mahatma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views5 pages

Pemrograman Jaringan Kisi Kisi

The document discusses sending and receiving data over UDP and TCP in Python. It includes code examples for a UDP broadcaster and receiver, a TCP client and server for single connections, and a multithreaded TCP server that can handle multiple clients simultaneously using select. The server code examples demonstrate how to accept connections, receive data from clients, send responses, and close connections.

Uploaded by

Yogi Mahatma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Pengiriman dengan UDP broadcast

Pengirim.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
dest = ('255.255.255.255', 8760)
msg = chr(10) + chr(0) + chr(255)
s.sendto(msg, dest)
s.close()

Penerima.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 8760))
try:
while True:
data = s.recv(4096)
msg = ''
for i in range(0,len(data)):
msg = msg + str(ord(data[i]))
print msg
except:
raise

Pengiriman dengan TCP


client.py
import socket, sys
# Server IP address
SERVER_IP = '127.0.0.1'

# Port number used by server


PORT = 6666
# Initialize socket object with TCP/Stream type
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Address before sending:', s.getsockname()
# Initiate a CONNECTION
s.connect((SERVER_IP, PORT))
# Send the message
s.sendall('This is my message')
print 'Address after sending', s.getsockname()
# Read message stream from server with specific buffer size
data = s.recv(4096)
print 'The server says', repr(data)
s.close()

server.py
import socket, sys
# Port number
PORT = 6666
# Initialize socket object with TCP/Stream type
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind to a certain IP and PORT use '' to accept incoming packet from anywhere
s.bind(('', PORT))
# Listen the incoming connection
s.listen(10)
print 'Listening at', s.getsockname()
while True:
# Accept connection, return client socket and address
conn, addr = s.accept()
# Read the message stream from client with specific buffer size
data = conn.recv(4096)
print 'The client says', repr(data)
# Send back the message to client
conn.sendall('OK '+data)
# Close connection
conn.close()

server.py
# Method for receiving stream of data
def recv_all(sock):
data = ''
while True:

# Receive message
buf = sock.recv(1000)
# If stream contains termination character, return
if "\r\n" in buf:
buf = buf.replace("\r\n", "")
data += buf
return data
# Append data
data += buf
return data

Multiple Client
Thread
serverthread.py
import socket, sys
from thread import start_new_thread
# Port number
PORT = 6666
# Initialize socket object with TCP/Stream type
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind to a certain IP and PORT use '' to accept incoming packet from
anywhere
sock.bind(('', PORT))
# Listen the incoming connection
sock.listen(10)
def clientthread(conn):
#infinite loop so that function do not terminate and thread do not
end.
while True:
try :
# Read the message stream from client
data = conn.recv(4096)
# Check if receive data is not empty
if data :
print 'The client from', addr
print 'The client says ', data
# Send back the message to client
conn.sendall('OK '+data)
# Empty string means connection closed
else :
break

except socket.error, e :
break
#came out of loop
print "Connection closed by client"
conn.close()
print 'Listening at', sock.getsockname()
while True:
try :
# Accept connection, return client socket and address
conn, addr = sock.accept()
# Read the message stream from client with specific buffer
size
start_new_thread(clientthread ,(conn,))
except KeyboardInterrupt :
break

serverselect.py
import socket, sys
import select
# Port number
PORT = 6666
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',PORT))
s.listen(10)
#input = [s,sys.stdin] <-- Unix/Linux
input = [s]
print 'Listening at', s.getsockname()
while True:
try :
inputready,outputready,exceptready = select.select(input,[],[])
for conn in inputready:
# Handle the server socket
if conn == s:
conn, address = s.accept()
input.append(conn)
# Handle standard input
#elif conn == sys.stdin:
#
junk = sys.stdin.readline()
#
running = 0
# Handle client socket
else:
data, address = conn.recvfrom(4096)
# Check if receive data is not empty
try :
if data :
print 'The client says ', data
# Send back the message to client
conn.sendall('OK '+data)

# Empty string means connection closed


else :
conn.close()
input.remove(conn)
print "Connection closed by client"
except socket.error, e :
conn.close()
input.remove(conn)
print "Connection closed by client"
except KeyboardInterrupt :
break
s.close()

You might also like