Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to TCP/UDP Sockets and Network Programming, Thesis of Computer Networks

An introduction to the fundamental concepts of tcp and udp sockets, which are the building blocks of network programming. It covers the differences between connection-oriented (tcp) and connectionless (udp) sockets, and explains the key functions and steps involved in setting up and using both types of sockets. Two example programs that demonstrate how to implement a simple 'hello world' application using tcp and udp sockets, respectively. By studying this document, readers can gain a solid understanding of the core principles and practical implementation of network communication using sockets, which is a crucial skill for any developer working on networked applications.

Typology: Thesis

2023/2024

Uploaded on 11/30/2023

harsh-marke
harsh-marke 🇮🇳

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction
Why)do)we)need)them?
TCP'and'UDP'are'the'basis'of'
any'networked'application
IP'is'standardized'by'RFCs'and'
implemented'in'the'operating'
systems
a'socket'='the'endEpoint'of'a'
processEtoEprocess'
communication'flow'across'an'
IP'based'network
communication:
connectionEoriented
connectionEless'
IP
hardware
TCP UDP
application
sockets
IP
hardware
TCP UDP
application
sockets
2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Introduction to TCP/UDP Sockets and Network Programming and more Thesis Computer Networks in PDF only on Docsity!

Why do we need them?

 TCP and UDP are the basis of

any networked application

 IP is standardized by RFCs and

implemented in the operating

systems

 a socket = the end-­‐point of a

process-­‐to-­‐process

communication flow across an

IP based network

 communication:

 connection-­‐oriented

 connection-­‐less

IP hardware TCP UDP application sockets IP hardware TCP UDP application sockets

Stream sockets

 stream socket = virtual circuit between two processes

 the connection is:

 sequenced

 reliable

 bi-­‐directional

 uniquely identified by the IP addresses and port

numbers (remote and local)

 setup by:

 a server that awaits for connections

 a client that connects to a server

Stream sockets

socket() bind() listen() accept() send() recv() close() socket() connect() send() recv() close()

Create the
socket
Server Client

Stream sockets

socket() bind() listen() accept() send() recv() close() socket() connect() send() recv() close()

Create the
socket
Bind to IP
address and
port
Server Client

Stream sockets

socket() bind() listen() accept() send() recv() close() socket() connect() send() recv() close()

Create the
socket
Bind to IP
address and
port
Start
listening…
Blocking
wait for
connection
Server Client

Stream sockets

socket() bind() listen() accept() send() recv() close() socket() connect() send() recv() close()

Create the
socket
Bind to IP
address and
port
Start
listening…
Blocking
wait for
connection
Create the
socket
Server Client

Datagram sockets

 (^) no distinction between server and client  (^) no connection and unreliable  (^) the same socket can be used to send/receive datagrams to/from multiple processes

Datagram sockets

socket() bind() close() socket() close() bind() recvfrom() sendto() recvfrom() socket() sendto() close()

Create Datagram sockets

socket
Bind to local
IP address
and port

socket() bind() close() socket() close() bind() recvfrom() sendto() recvfrom() socket() sendto() close()

Create Datagram sockets

socket
Bind to local
IP address
and port

socket() bind() close() socket() close() bind() recvfrom() sendto() recvfrom() socket() sendto() close()

No binding
here, only
sending
data…

Example 1

server : creating the socket

include <sys/socket.h>

include <netinet/in.h>

include <sys/types.h>

int main () { int sockfd; /* create server-side socket */ sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

protocol
family
a stream socket
otherwise: SOCK_DGRAM
using TCP
otherwise: IPPROTO_UDP

Example 1

server : binding the address

struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; /*IPv4 */ my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons(2000); bind(sockfd, &my_addr, sizeof (my_addr)); listen(sockfd, MAX_CONNECTIONS);

any local
address

Example 1

client : looking up the server

struct hostent server_host; server_host = gethostbyname("localhost"); / configure the server address */ struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; memcpy(&server_addr.sin_addr, server_host->h_addr, sizeof ( struct in_addr)); server_addr.sin_port = htons(PORT);

hostname
try
“www.google.com”

Example 1

client : the other steps

/* create client-side socket */

sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

/* connect to server */

connect(sockfd, &server_addr, sizeof (server_addr));

char buf[MAX_BUF];

int len = recv(sockfd, buf, MAX_BUF, 0);

printf("received %d bytes: '%s'\n", len, buf);

close(sockfd);