SlideShare a Scribd company logo
2.4 Write a stream –based echo server and a client sending message to it, and receiving back
each message in turn until the client terminates the connection. Hint: Modify the stream-based
TCP client and server programs in this chapter or similar programs to transfer multiple messages
back and forth.
Solution
The Transmission Control Protocol (TCP) is a stream-based method of network communication.
And one of the main protocol of Internet Protocol suite.
TCP provides an interface to network communications that is radically different from the USER
datagram Protocol(UDP).
The properties of TCP make it highly attractive to network programmers and it simplifies
network communication by removing many of the obstacles of UDP, such as ordering of packets
and packet loss.
TCP focuses instead on establishing a network connection, through which a stream of bytes may
be sent and received.
It establishes a "virtual connection" between two machines, through which stream of data may
be sent.
TCP uses a lowel-level communications protocol, the internet protocol to establish the
connection between machines.This connection provides an interface that allows stream of bytes
to be sent and received and to convert the data into IP datagram packets.
Simple Echo Server Program:
import socket
host = ' '
port = 5000
backlog = 1024
s = socket.socket(socket.AF-INET, socket.SOCK-STREAM)
s.bind ((host,port))
s.listen(backlog)
while 1:
client, address = s. accept()
data = client.recv(size)
if data;
client.send(data)
client.close()
TCP ECHO CLIENT USING STREAMS:
import asyncio
@asyncio.coroutine
def tcp-echo-client(message, loop);
reader, writer = yield from asyncio.open-connection(loop = loop)
printf("send:% message)
writer.write(message.encode())
data = yield from reader.read(100)
print('received: %r' %data.decode())
print('close the socket')
writer.close()
message = 'Hello World'
loop = asyncio.get-event-loop()
loop.run-until-complete-echo-client(message,loop)
loop.close()

More Related Content

Similar to 2.4 Write a stream –based echo server and a client sending message t.pdf (20)

PPT
Chapter 2
Videoguy
 
PDF
TCP Vs UDP
Ahmed Elnaggar
 
PPTX
Internet Internet Protocols.pptx( technology)
ujjawalr9027
 
PDF
Week10 transport
kapilpahwabnb
 
PPT
Transport Layer [Autosaved]
Ram Dutt Shukla
 
PPT
Transport Layer
Ram Dutt Shukla
 
PDF
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
PPTX
NETWORK PROTOCOLS lecture one introdection to protocols
abdusalamyahya2
 
PPTX
Networking in python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
DOCX
Identify and describe each phase (numbered) in the TCP trace- Make sur.docx
mckerliejonelle
 
DOCX
Identify and describe each phase (numbered) in the TCP trace. Make su.docx
Abhinav816839
 
PPTX
Transport layer
Mukesh Chinta
 
PDF
Internet of things protocols for resource constrained applications
Pokala Sai
 
PDF
Computer Communication Networks- TRANSPORT LAYER PROTOCOLS
Krishna Nanda
 
PPT
計概
allan3160
 
PDF
Transport layer
Sisir Ghosh
 
PPT
TCP Section5
Rammy Kuttaineh
 
PPTX
CSS (KNC-301) 2. TCP IP By Vivek Tripathi.pptx
VivekTripathi684438
 
PPTX
Java Network Programming.pptx
RoshniSundrani
 
PDF
Ajp notes-chapter-04
Ankit Dubey
 
Chapter 2
Videoguy
 
TCP Vs UDP
Ahmed Elnaggar
 
Internet Internet Protocols.pptx( technology)
ujjawalr9027
 
Week10 transport
kapilpahwabnb
 
Transport Layer [Autosaved]
Ram Dutt Shukla
 
Transport Layer
Ram Dutt Shukla
 
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
NETWORK PROTOCOLS lecture one introdection to protocols
abdusalamyahya2
 
Identify and describe each phase (numbered) in the TCP trace- Make sur.docx
mckerliejonelle
 
Identify and describe each phase (numbered) in the TCP trace. Make su.docx
Abhinav816839
 
Transport layer
Mukesh Chinta
 
Internet of things protocols for resource constrained applications
Pokala Sai
 
Computer Communication Networks- TRANSPORT LAYER PROTOCOLS
Krishna Nanda
 
計概
allan3160
 
Transport layer
Sisir Ghosh
 
TCP Section5
Rammy Kuttaineh
 
CSS (KNC-301) 2. TCP IP By Vivek Tripathi.pptx
VivekTripathi684438
 
Java Network Programming.pptx
RoshniSundrani
 
Ajp notes-chapter-04
Ankit Dubey
 

More from excellentmobiles (20)

PDF
How is the spleen like a lymph node How is it unlike a lymph node .pdf
excellentmobiles
 
PDF
Give two advantages and two disadvantages of placing IO function in.pdf
excellentmobiles
 
PDF
Frances drop in output per capita relative to U.S. output per capi.pdf
excellentmobiles
 
PDF
Describe the effect of cyanide ion, CN^-, on oxidative phosphorylatio.pdf
excellentmobiles
 
PDF
Describe synchronization techniques used by programmers who develop .pdf
excellentmobiles
 
PDF
Barney had a bag of cookies. He ate 35 in the first 10 minutes. He a.pdf
excellentmobiles
 
PDF
11. In the United States, financial statement audits of non-public co.pdf
excellentmobiles
 
PDF
When you make the following dihybrid testcross MmPp x mmpp, what is .pdf
excellentmobiles
 
PDF
Why do you think nursing core competencies were selectedWhy do .pdf
excellentmobiles
 
PDF
Which of the following is true of water It is a phospholipid It is.pdf
excellentmobiles
 
PDF
Which of the following are considered to be alive and whyProteins.pdf
excellentmobiles
 
PDF
Which of the following are actually vector spaces over the real numbe.pdf
excellentmobiles
 
PDF
What is the change of entropy of water (Lf = 0.333 MJkg, Lv = 2.26 .pdf
excellentmobiles
 
PDF
What are the epidemiological characteristics of infection with avian.pdf
excellentmobiles
 
PDF
What benefits if any, are inherent with interactive communication fo.pdf
excellentmobiles
 
PDF
What are the features of linear and charter What are the featur.pdf
excellentmobiles
 
PDF
W hy is D the answer Why does it not indicate the same relations.pdf
excellentmobiles
 
PDF
what are handles, WS private, WS shared and Working set used for in .pdf
excellentmobiles
 
PDF
USA Today conducted a survey asking readers, “What is the most hilar.pdf
excellentmobiles
 
PDF
The distance between two successive minima of a transverse wave i.pdf
excellentmobiles
 
How is the spleen like a lymph node How is it unlike a lymph node .pdf
excellentmobiles
 
Give two advantages and two disadvantages of placing IO function in.pdf
excellentmobiles
 
Frances drop in output per capita relative to U.S. output per capi.pdf
excellentmobiles
 
Describe the effect of cyanide ion, CN^-, on oxidative phosphorylatio.pdf
excellentmobiles
 
Describe synchronization techniques used by programmers who develop .pdf
excellentmobiles
 
Barney had a bag of cookies. He ate 35 in the first 10 minutes. He a.pdf
excellentmobiles
 
11. In the United States, financial statement audits of non-public co.pdf
excellentmobiles
 
When you make the following dihybrid testcross MmPp x mmpp, what is .pdf
excellentmobiles
 
Why do you think nursing core competencies were selectedWhy do .pdf
excellentmobiles
 
Which of the following is true of water It is a phospholipid It is.pdf
excellentmobiles
 
Which of the following are considered to be alive and whyProteins.pdf
excellentmobiles
 
Which of the following are actually vector spaces over the real numbe.pdf
excellentmobiles
 
What is the change of entropy of water (Lf = 0.333 MJkg, Lv = 2.26 .pdf
excellentmobiles
 
What are the epidemiological characteristics of infection with avian.pdf
excellentmobiles
 
What benefits if any, are inherent with interactive communication fo.pdf
excellentmobiles
 
What are the features of linear and charter What are the featur.pdf
excellentmobiles
 
W hy is D the answer Why does it not indicate the same relations.pdf
excellentmobiles
 
what are handles, WS private, WS shared and Working set used for in .pdf
excellentmobiles
 
USA Today conducted a survey asking readers, “What is the most hilar.pdf
excellentmobiles
 
The distance between two successive minima of a transverse wave i.pdf
excellentmobiles
 
Ad

Recently uploaded (20)

PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
community health nursing question paper 2.pdf
Prince kumar
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Ad

2.4 Write a stream –based echo server and a client sending message t.pdf

  • 1. 2.4 Write a stream –based echo server and a client sending message to it, and receiving back each message in turn until the client terminates the connection. Hint: Modify the stream-based TCP client and server programs in this chapter or similar programs to transfer multiple messages back and forth. Solution The Transmission Control Protocol (TCP) is a stream-based method of network communication. And one of the main protocol of Internet Protocol suite. TCP provides an interface to network communications that is radically different from the USER datagram Protocol(UDP). The properties of TCP make it highly attractive to network programmers and it simplifies network communication by removing many of the obstacles of UDP, such as ordering of packets and packet loss. TCP focuses instead on establishing a network connection, through which a stream of bytes may be sent and received. It establishes a "virtual connection" between two machines, through which stream of data may be sent. TCP uses a lowel-level communications protocol, the internet protocol to establish the connection between machines.This connection provides an interface that allows stream of bytes to be sent and received and to convert the data into IP datagram packets. Simple Echo Server Program: import socket host = ' ' port = 5000 backlog = 1024 s = socket.socket(socket.AF-INET, socket.SOCK-STREAM) s.bind ((host,port)) s.listen(backlog) while 1: client, address = s. accept() data = client.recv(size) if data; client.send(data) client.close()
  • 2. TCP ECHO CLIENT USING STREAMS: import asyncio @asyncio.coroutine def tcp-echo-client(message, loop); reader, writer = yield from asyncio.open-connection(loop = loop) printf("send:% message) writer.write(message.encode()) data = yield from reader.read(100) print('received: %r' %data.decode()) print('close the socket') writer.close() message = 'Hello World' loop = asyncio.get-event-loop() loop.run-until-complete-echo-client(message,loop) loop.close()