Task 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Task 3.

Socket programming (Python)

Summary:
Your CIO assigns you another project which involves setting up of connections between many clients and one
server.
Task 1 - First of all, write a program that facilitates one client communicating with the server, in separate windows.

Task 2 - Secondly, while the first client is running, start 10 other clients that connect to the same server; these
clients should most likely be started in the background with their input redirected from a file. Report what happens
to these 10 clients? Do their connect()s fail, or time out, or succeed? Do any other calls block? Now let the client
exit. Explain what happens.

Task 3 - Thirdly, modify the socket program so that each time the client sends a line to the server, the server sends
the line back to the client. The client (and server) will now have to make alternating calls recv () and send ().

Task 4 - Finally, modify the socket program so that it uses UDP as the transport protocol, rather than TCP. You will
have to change SOCK_STREAM to SOCK_DGRAM in both the client and the server. Discuss what happens when two
UDP clients simultaneously connect to the same UDP server and compare this to the TCP behaviour.

Socket Tasks:

Task 1 One client communicating with the server in separate windows

Server Code: Figure ?: Server Code


The server
creates a socket
and waits for
the client to
connect. When
the connection
is established, it
will reveal
which IP
address and
port number
the client used
for
communication.

Client Code: Figure?: Client Code


The client
creates a socket
and waits for
the server to
respond. The
name of the
user must be
entered. It will
display the
client that is
connected to
the network for
communication
and will alert
when the
connection is
established.
Screen Output:
Figure?: Server connected
Figure ? shows
the server
running and
ready to receive
requests from
the client.

Figure?: Client running


Figure ? shows
how the client
runs and
prompts the
user for
information
before binding
with the server.

Figure: Name is entered by the user


When a client-
side user enters
information.

Figure: Client~Server connected


The server-
client
connection is
formed, which
provide
information
about the port,
IP address, and
connection.

Figure : Client communication with the server


Server response to
the client.

Task 2 - Secondly, while the first client is running, start 10 other clients that connect to the same server; these
clients should most likely be started in the background with their input redirected from a file. Report what happens
to these 10 clients? Do their connect()s fail, or time out, or succeed? Do any other calls block? Now let the client
exit. Explain what happens.

Server code: Figure: Server code


The server is
threaded
with the
client in this
case. The
thread count
is used to
determine
the number
of clients
connected
to the
server. The
server will
display the
thread
number
together
with the
client's IP
address and
the port
number
used to
connect—
see Figure
20 server
code
explained.

Client Code: Figure: Client code


We have a
group of
clients that
will connect
to a single
server. The
server's
reaction to
several
clients
connecting.
In addition,
if a client
leaves. The
server's
reaction.

Clients Figure: Proof of ten clients created


created:
Figure 19
shows how
ten clients
were built to
communicat
e with a
single
server.

Figure: Server connected


Figure 21
shows a
message
waiting for
client
connections
when the
server is
running.

Figure: User input requirement to communicate with the server


In the client-
side display
message,
the server is
waiting for a
connection
and for user
input before
communicati
ng with it.

Figure: The server displays the client connection


When the
first client
connects to
the server,
the server
displays the
thread
number
along with
the client's
IP address
and port
number.
Figure: Server showing clients connection
All ten
clients are
connected
to the
server, with
the Thread
number, IP
address, and
client port
numbers
clearly
visible.

Figure: Clients send and receive messages from the server.


Figure: A client-closed error code was detected
If a client
disconnects
from the
server, an
error
message is
displayed.

Report what happens to these ten clients?


All ten clients can connect simultaneously. See figure ..

Do their connect()s fail, or time out, or succeed?


Their connection succeeds. See figure ..

Do any other calls block?


No other client calls to block.

Now let the client exit. Explain what happens.


A message / error shows on the server screen when a client quits, but it did not appear or reduce threading from
the list in the server connection area. See figure ..

Task 3 - Thirdly, modify the socket program so that each time the client sends a line to the server, the server sends
the line back to the client. The client (and server) will now have to make alternating calls recv () and send ().

Server Code: Figure : Server Code


When string data is sent,
the server communicates
with the client. Socket,
time, and thread are
imported by the server.
Communicate with
clients now, and servers
send recv () and send ()
at the same time.

Client Code: Figure: Client Code


When string data is sent,
the server talks with the
client. Socket, time, and
thread are all imported
by the server. Clients are
now communicated
with, and servers send
recv () and send () at the
same time.

Screen Output: Figure: Server Started

Client communication
Client communicates
with the server,
displaying the client
number.

Server communication
The client sends a string
to the server, which
accepts it and returns
the string to the client
with an ID number.

Screen Output continues When the client closes, the server returns an error code.
The server can handle ten simultaneous connections. We used a loop to create all connections and because of
this the client programme cannot close one connection.

Task 4 - Finally, modify the socket program so that it uses UDP as the transport protocol, rather than TCP. You will
have to change SOCK_STREAM to SOCK_DGRAM in both the client and the server. Discuss what happens when two
UDP clients simultaneously connect to the same UDP server and compare this to the TCP behaviour.

UDP Server Code Figure: Server using UDP protocol


The server will now
communicate with the
client using a different
protocol, such as UDP,
rather than TCP.

UDP Client Code: Figure: UDP Client Code


Client communicates
with the server via a
different protocol, such
as UDP, rather than
TCP UDP.

Screen Output
Figure: Server started
with UDP connection.

Client Response
Figure: Client response
with ID number, Client
IP and port address.
Figure: Server
response:

Figure: response from


both the server and the
client

Discuss what happens when two UDP clients simultaneously connect to the same UDP server and compare this to
the TCP behaviour.
 They consume too much time and cause CPU overload
 TCP connections were much easier to set up
 The data is presented as a byte array

Conclusion:
While doing the assessments, users will learn intermediate-level python socket program and in-
depth cloud computing uses.

You might also like