Lab 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

CSE4202 -001

Inha University

Computer Networks - Lab


Socket Programming in C

2. Protocols, socket creation, data transmission


Review (Lab-1):
5-Step Client/Server Connection Establishment

1. Bind
2. Socket
3. Listen
4. Connect
5. Accept

?
1. Socket
Protocol
Protocol

• What is “Protocol”?
• Network protocols are formal standards and policies comprised of rules, procedures and
formats that define communication between two or more devices over a network.
• It needs to be specified to create a socket (so socket needs to know the protocol type)
• It’s like a detailed deal between two people for co-working (or just communicating)
• The both parties can communicate with each other using the agreed terms and conditions
Domain, Type, Protocol

• Socket domain
• PF_XXX  Protocol Family XXX
• PF_INET = Protocol Family Internet (IPv4)

• PF_INET6 = Protocol Family Internet (IPv6)

• AF_XXX  Address Family XXX (each address family supports multiple protocol families)
• AF_INET = Address Family Internet (IPv4)

• AF_INET6 = Address Family Internet (IPv6)


Domain, Type, Protocol

• Socket type
• SOCK_STREAM  TCP (Transmission Control Protocol – Two-way handshaking)
• Connection oriented = stable

• SOCK_DGRAM  UDP (User Datagram Protocol – Doesn’t shake any hands)


• Connectionless = fast

• SOCK_RAW (not needed)


• SOCK_RDM (not needed)
• SOCK_SEQPACKET (not needed)

SOCK_STREAM SOCK_DGRAM

? ?
Domain, Type, Protocol

• Socket protocol (sub-protocol of the selected socket type)


• Could be an icmp, snmp, igp message  as there are various sub protocols available
for machine controlling, information fetching, and so on…
Socket Creation

• Specify the three elements: Domain, Type, Protocol


Now lets create our own socket

• “Server” Socket workflow: Socket()

Bind()

Listen()

Accept()
Now lets create our own socket

• “client” Socket workflow: Socket()

connect()

recv()
Data transmission

Server sends the data

Client reads the data


Compile & Run
the files from blackboard

* gcc –o server tcp_server.c


* gcc –o client tcp_client.c

You might also like