0
0
Computer Networksknowledge~10 mins

Socket programming basics in Computer Networks - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a socket using IPv4 and TCP protocols.

Computer Networks
sock = socket.socket(socket.[1], socket.SOCK_STREAM)
Drag options to blanks, or click blank then click option'
AAF_INET6
BAF_INET
CSOCK_DGRAM
DSOCK_RAW
Attempts:
3 left
💡 Hint
Common Mistakes
Using AF_INET6 instead of AF_INET for IPv4 sockets.
Confusing socket types with address families.
2fill in blank
medium

Complete the code to bind the socket to all interfaces on port 8080.

Computer Networks
sock.bind(([1], 8080))
Drag options to blanks, or click blank then click option'
A"0.0.0.0"
B"localhost"
C"127.0.0.1"
D"255.255.255.255"
Attempts:
3 left
💡 Hint
Common Mistakes
Binding to 'localhost' limits connections to local only.
Using broadcast address '255.255.255.255' is incorrect for binding.
3fill in blank
hard

Fix the error in the code to listen for incoming connections with a backlog of 5.

Computer Networks
sock.[1](5)
Drag options to blanks, or click blank then click option'
Aconnect
Bbind
Clisten
Daccept
Attempts:
3 left
💡 Hint
Common Mistakes
Using accept() before listen().
Using bind() or connect() incorrectly here.
4fill in blank
hard

Fill both blanks to accept a connection and receive data from the client.

Computer Networks
conn, addr = sock.[1]()
data = conn.[2](1024)
Drag options to blanks, or click blank then click option'
Aaccept
Brecv
Csend
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using send() instead of recv() to receive data.
Using connect() on server side instead of accept().
5fill in blank
hard

Fill all three blanks to send a response and close the connection properly.

Computer Networks
conn.[1](b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello")
conn.[2](socket.SHUT_RDWR)
sock.[3]()
Drag options to blanks, or click blank then click option'
Asend
Bclose
Cshutdown
Drecv
Attempts:
3 left
💡 Hint
Common Mistakes
Calling close() before shutdown(), which may cause data loss.
Using recv() instead of send() to send data.