0% found this document useful (0 votes)
1 views6 pages

Module 5 - Chapter 4 - Network Programming

The document outlines a course module on Network Programming, focusing on concepts such as sockets, protocols, and IP addresses, specifically in the context of Java and Python. It details the structure and functionality of sockets, including methods for server and client communication, and provides example code for socket programming in Python. Additionally, it includes an exercise to create a simple chat application demonstrating client-server interaction using sockets.

Uploaded by

kayel.mess
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)
1 views6 pages

Module 5 - Chapter 4 - Network Programming

The document outlines a course module on Network Programming, focusing on concepts such as sockets, protocols, and IP addresses, specifically in the context of Java and Python. It details the structure and functionality of sockets, including methods for server and client communication, and provides example code for socket programming in Python. Additionally, it includes an exercise to create a simple chat application demonstrating client-server interaction using sockets.

Uploaded by

kayel.mess
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

Supplemental Materials and Enrichment Activity Form

Program: Course:
Integrative Programming and BSIT
Technologies 1
Batch Number: Week/s Covered : Inclusive Dates:
Jan 19, 2026 to Jan 23, 2026
Faculty Name: Yr. & Sec.: Time & Day:
MLDELOSSANTOS BSIT 1-31 10:00am-4:00pm & T

MODULE 5 (Chapter 4: Network Programming)

Topics:
1. Network Programming
2. Sockets
Course Learning Outcomes:
At the end of this lesson students should be able to:
1. Identify the parts of Network Programming; and
2. Modify a low level data communications message and queuing services

Network Programming
Network Programming involves writing programs that communicate with other programs across a
computer network.

Network:
• A network is a collection of computers and other devices that can send data to and receive data
from each other.
• A network is often connected by wires.
• However, wireless networks transmit data through infrared light and microwaves.

Node:
• Each machine on a network is called a node.
• Most nodes are computers, but printers, routers, bridges, gateways etc..can also be nodes.
• Nodes that are fully functional computers are also called hosts.

Packet:
• All modern computer networks are packet-switched networks: data traveling on the network is
broken into chunks called packets and each packet is handled separately.
• Each packet contains information about who sent it and where it's going.

Protocol:
• A protocol is a precise set of rules defining how computers communicate: the format of
• addresses, how data is split into packets.
IP:
• IP was designed to allow multiple routes between any two points andto route packets of data
around damaged routers.
TCP:
• Since there are multiple routes between two points, and the packetsthat make up a
particular data stream.
• Furthermore, they may not arrive in the order they were sent, if theyeven arrive at all.
Supplemental Materials and Enrichment Activity Form

UDP:
• UDP is an unreliable protocol that does not guarantee that packets willarrive at their destination or that
they will arrive in the same orderthey were sent.

Ports:
• Each computer with an IP address has several thousand logical ports.
• Each port is identified by a number between 1 and 65,535. Each port canbe allocated to a
particular service.
• Port numbers 1 through 255 are reserved by IP for well-known servicesIf you connect to
port 80 of a host, for instance, you may expect to findan HTTP server.

Internet:
• largest IP-based network for connecting machines together.
• Java: easy-to-use, cross-platform model for network communications.

What is a Socket?
• Sockets are a means of using IP to communicate between machines, so sockets allow Java
to interoperate with legacy systems by simply talking to existing servers using their pre-defined
protocol.
• Internet protocol: User Datagram Protocol (UDP) and Transmission Control Protocol (TCP).

Internet Addresses or IP Addresses


• Every network node has an address, a series of bytes that uniquely identify it.
• Internet addresses are manipulated in Java by the use of the InetAddress [Link]
takes care of the Domain Name System (DNS) look-up and reverselook-up;
• IP addresses can be specified by either the host name or the raw IP address. InetAddress
provides methods to getByName(), getAllByName(),getLocalHost(), getAddress(), etc.
• IP addresses are a 32-bit number, often represented as a "quad" of four 8-bit numbers
separated by periods.
• They are organized into classes (A, B, C, D, and E). For example [Link]
Client/Server Computing- Java language communicate with remote file system.

Python Socket Programming


Socket programming is a technique in which we communicate between two nodes connected in a
network where the server node listens to the incoming requests from the client nodes.

In Python, the socket module is used for socket programming. The socket module in the standard library
included functionality required for communication between server and client at hardware level.

This module provides access to the BSD socket


interface. It is available on all operating systems
such as Linux, Windows, MacOS.

What are Sockets?


Sockets are the endpoints of a bidirectional
communications channel. Sockets may
communicate within a process, between processes
on the same machine, or between processes on
different continents.

A socket is identified by the combination of IP


address and the port number. It should be properly
configured at both ends to begin communication.
Supplemental Materials and Enrichment Activity Form

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP,
and so on. The socket library provides specific classes for handling the common transports as well as a
generic interface for handling the rest.

The term socket programming implies programmatically setting up sockets to be able to send and receive
data.

There are two types of communication protocols −

• connection-oriented protocol
• connection-less protocol

Python socket Module


The socket module is used for creating and managing socket programming for the connected nodes in a
network. The socket module provides a socket class. You need to create a socket using the
[Link]() constructor.

An object of the socket class represents the pair of host name and the port numbers.

Syntax
The following is the syntax of [Link]() constructor –

[Link] (socket_family, socket_type, protocol=0)

Parameters
family − AF_INET by default. Other values - AF_INET6 (eight groups of four hexadecimal digits),
AF_UNIX, AF_CAN (Controller Area Network) or AF_RDS (Reliable Datagram Sockets).

socket_type − should be SOCK_STREAM (the default), SOCK_DGRAM, SOCK_RAW or perhaps one of


the other SOCK_ constants.

protocol − number is usually zero and may be omitted.

Return Type
This method returns a socket object.

Once you have the socket object, then you can use the required methods to create your client or server
program.

Server Socket Methods


The socket instantiated on server is called server socket. Following methods are available to the socket
object on the server −
• bind() method − This method binds the socket to specified IP address and port number.
• listen() method − This method starts server and runs into a listen loop looking for connection
request from client.
• accept() method − When connection request is intercepted by server, this method accepts it and
identifies the client socket with its address.
To create a socket on a server, use the following snippet −
import socket
server = [Link]()
[Link](('localhost',12345))
[Link]()
client, addr = [Link]()
Supplemental Materials and Enrichment Activity Form
print ("connection request from: " + str(addr))

By default, the server is bound to local machine's IP address 'localhost' listening at arbitrary empty port
number.

Client Socket Methods


Similar socket is set up on the client end. It mainly sends connection request to server socket listening at
its IP address and port number
connect() method
This method takes a two-item tuple object as argument. The two items are IP address and port number of
the server.
obj=[Link]()
[Link]((host,port))
Once the connection is accepted by the server, both the socket objects can send and/or receive data.
send() method
The server sends data to client by using the address it has intercepted.
[Link](bytes)
Client socket sends data to socket it has established connection with.
sendall() method
similar to send(). However, unlike send(),this method continues to send data from bytes until either all
data has been sent or an error occurs. None is returned on success.
sendto() method
This method is to be used in case of UDP protocol only.
recv() method
This method is used to retrieve data sent to the client. In case of server, it uses the remote socket whose
request has been accepted.
[Link](bytes)
recvfrom() method
This method is used in case of UDP protocol.

Python - Socket Server


To write Internet servers, we use the socket function available in socket module to create a socket object.
A socket object is then used to call other functions to setup a socket server.

Now call the bind(hostname, port) function to specify a port for your service on the given host.

Next, call the accept method of the returned object. This method waits until a client connects to the port
you specified, and then returns a connection object that represents the connection to that client.

Example of Server Socket

# first of all import the socket library


import socket

# next create a socket object


s = [Link]()
print ("Socket successfully created")

# reserve a port on your computer in our


# case it is 12345 but it can be anything
port = 12345

# Next bind to the port


# we have not typed any ip in the ip field
# instead we have inputted an empty string
Supplemental Materials and Enrichment Activity Form
# this makes the server listen to requests
# coming from other computers on the network
[Link](('', port))
print ("socket binded to %s" %(port))

# put the socket into listening mode


[Link](5)
print ("socket is listening")

# a forever loop until we interrupt it or


# an error occurs
while True:

# Establish connection with client.


c, addr = [Link]()
print ('Got connection from', addr )

# send a thank you message to the client. encoding to send byte type.
[Link]('Thank you for connecting'.encode())

# Close the connection with the client


[Link]()

# Breaking once connection closed


Break

Python - Socket Client


Let us write a very simple client program, which opens a connection to a given port 5001 and a given
localhost. It is very simple to create a socket client using the Python's socket module function.

The [Link](hosname, port) opens a TCP connection to hostname on the port. Once you have a
socket open, you can read from it like any IO object. When done, remember to close it, as you would
close a file.

Example of Client Socket


The following code is a very simple client that connects to a given host and port, reads any available data
from the socket, and then exits when 'q' is entered.

# Import socket module


import socket

# Create a socket object


s = [Link]()

# Define the port on which you want to connect


port = 12345

# connect to the server on local computer


[Link](('[Link]', port))

# receive data from the server and decoding to get the string.
print ([Link](1024).decode())
# close the connection
[Link]()
Supplemental Materials and Enrichment Activity Form

References:

[Link]
[Link]

Exercise 4: Socket Programming

Create a simple chat application demonstrating the client-server relationship using sockets in Python.
Attach screenshots of exchanging data.

You might also like