0% found this document useful (0 votes)
2 views11 pages

TCP/UDP Socket Programming Guide

The document provides various Python socket programming examples for establishing one-way and two-way connections using TCP and UDP protocols. It includes client and server programs for sending and receiving messages, as well as file sharing functionalities. Additionally, it demonstrates URL parsing using the urllib library.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

TCP/UDP Socket Programming Guide

The document provides various Python socket programming examples for establishing one-way and two-way connections using TCP and UDP protocols. It includes client and server programs for sending and receiving messages, as well as file sharing functionalities. Additionally, it demonstrates URL parsing using the urllib library.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER NETWORKS

GET IP USING SOCKET:


7
import socket

system= [Link]()
ip_address = [Link](system)

print("ip address is + ip_address)


"

utput ip addicw us12.o.o.1


ONE WAY CONNECTION USING TCP
CLIENT PROGRAM
import socket
Sock =[Link](s0cket.AF_INET,socket.SOCK_STREAM)
hostname =[Link]()
host = [Link](hostname)

port= 4444
print("waiting for server accept the connection...")
Sock_connect = [Link]((host, port))

print("connected")
while True:

try:
data [Link](1024)
print([Link]('utf-8'))
except Exception as e:
print("you are disconnected")
break

Sock.close0
SERVER PROGRAM

import socket
Sock = [Link](socket.AF_INET, [Link] STREAM)

hostname = [Link]()

host= [Link](hostname)
port = 4444

[Link]((host, port))
print("waiting for connection.")
[Link](1)
conn, address = [Link]()

print("connected to "+addressf0])
while True:

try:
connection = [Link]()

i fconnection == -1:

print("client disconnected")
break
data =
input("enter the message:")
byte_data = [Link]("utf-8")

[Link](byte_data)
except Exception as e:

print("error as occurd")

break
[Link]()
TWO WAY CONNECTION USING TCP
CLIENT PROGRAM
import socket

print("wellcome to two way communication using TCP")

sock [Link](socket.AF_INET,socket.SOCK_STREAM)
hostname= [Link]()
host= [Link](hostname)
port = 4848

user_name = input("enter your name:")

print("waiting for accept the connection...")

[Link]((host,port))
[Link](user_name.encode('utf-8))
server_name = [Link](1024)

print(user_namet+" you are connected to "+server_name.decode('utf-8'))

while True:
data = [Link](1024)

print(server_name.decode('utf-8')+">"+[Link]('utf-8'))

message= input(user_name+">")

[Link]([Link](utf-8))

[Link]()
ateu
p tet tet
lelceut Jo
'Lne
Cuter ccu a n e

Jo aCcep 7 CChncc freap. ..


SERVER PROGRAM
import socket

print("wellcome to two way communication using TCP")


sock [Link](socket.AF_INET,socket.SOCK_STREAM)
hostname = [Link]()

host = [Link](hostname)

port =4848

user_name =

input("enter your name:")


[Link](([Link])
print("waiting for connection..")
[Link](2)
conn,addr = [Link]()

[Link](user_name.encode('utf-8'))
client_name = [Link](1024)

print(user_name+" you are connected to "+client_name.decode('utf-8"))


while True:
message = input(user name+">")

[Link]([Link]('utf-8))
data = [Link](l024)

print(client_name.decode('utf-8)+">"+str([Link]('utf-8))
[Link])

lelcetus do tevo a y Cowerteer C trou utteYe?


LiCuco

:ttre b
ONE WAY CONNECTION USING UDP
CLIENT PROGRAM
import socket

sock =

[Link](socket.AF_INET, socket.SOCK_DGRAM)
hostname = [Link]()

host = [Link](hostnamne)

port = 4444

print("waiting for accept the connection...")

[Link]("connect".encode('utf-8'), (host, port))


print("connected")
while True:
data = [Link](1024)

print([Link]('utf-8")
if not data:

break

[Link]()

A accett
SERVER PROGRAM
import socket
S C

sOCk [Link](socket.AF_INET,socket.SOCK_DGRAM)

hostname = [Link])
host = [Link](hostname)

port = 4444

print("waiting for new connection...")


[Link]((host, port))
data = [Link](1024)

print(data)
print("connected")
while True:
try:
message =
input("Enter message to send:")
[Link]([Link]('utf-8), data[1)
except Exception as e:
print("Error occured")
break

sock.close0

,1, 61a, 7131)

A
TWO WAY CONNECTION USING UDP
CLIENT PROGRAM
import socket

sock = [Link](socket.AF_INET, [Link] _DGRAM)

hostname = [Link]()

host = [Link](hostname)

port= 4444
print("waiting for accept the connection...")

[Link]("connect".encode('utf-8), (host, port))


print("connected")
while True:
data = [Link](1024)

print(dataf0].decode('utf-8'))
message= input("Enter the message to send:")

[Link]([Link]('utf-8"), data[1])
if not data:

break
[Link])
SERVER PROGRAM
import socket

sock [Link](socket.AF_INET,socket.SOCK_DGRAM)
hostname = [Link]()

host = [Link](hostname)

port=4444
print("waiting for new connection...")
[Link]((host, port))
print("connected")
while True:
try:
data= [Link](1024)

print(dataf0].decode(utf-8'))
message= input("Enter message to send:")
[Link]([Link]('utf-8'), data[1])
except Exception as e:
print("an error as occured")
break

[Link])

oul
URL PARSE

from urllib import parse

url = [Link]([Link]

scheme = [Link]

port [Link]
location [Link]
neturl = [Link])

print(scheme)
print(port)
print(location)
print(neturl)

Oplo Cou 80/9 ral


FILE SHARING

CLIENT PROGRAM

import socket

sock [Link](socket.AF_INET, [Link] STREAM)


hostname = [Link]()

host [Link](hostname)
port= 4444

[Link](host,port))
print("connected")
while True:
data = [Link](1024)

with open(received_file.txt,'wb') as f:
[Link] data)
[Link])
print("file saved")

[Link]()

Ctecectro
SERVER PROGRAM
import socket

import os
sock = [Link]([Link] INET,socket. SOCK STREAM)

hostname = [Link]()

host= [Link](hostname)
port = 4444

[Link]((host,port))
[Link](10)
conn,addr = [Link]()

print("connected")
print("sending file.")
with open('[Link], rb') as f.

file [Link](1024)
[Link](file)
f.close0
[Link]()

erterclbco

You might also like