Docsity

TCP/IP Socket Programming: An Overview of Sockets and Their Uses in Client-Server Model, Study notes of Computer Networks

Partial preview of the text

Download TCP/IP Socket Programming: An Overview of Sockets and Their Uses in Client-Server Model and more Study notes Computer Networks in PDF only on Docsity!

TCP/IP - Socket Programming

[email protected]

sockets - overview

sockets

simple client - server model

look at tcpclient/tcpserver.c

look at udpclient/udpserver.c

tcp/udp contrasts

“normal” master/slave setup for TCP

inetd on UNIX - mother server

some details -

there are more

sockets

basic definition -

“endpoint of

communication”

allows connected streams (TCP) or discretemessages (UDP) between processes onsame machine, cross network

in o.s., really read/write data queues + TCPhas connection Queue (server side)

talk to “socket” with handle/sock descriptor

kinds of sockets

acc. to address family; i.e. how doesaddressing work

IP address family ->

IP addr, tcp/udp port

traditional BSD families

TCP/IP (AF_INET; i.e., Internet)

»

TCP/UDP/”raw” (talk to IP)

UNIX (intra-machine, pipes)

XNS, and even

APPLETALK, DECNET, IPX ...

syscalls - TCP client/simple test server

int s = socket(2)gethostbyname(3)connect(2)read/write(2)close(2)

int s = socket(2)bind(2)listen(2)accept(2)read/write(2)close(2)

init i/o

client

server

socket(2) syscall

int s = socket(family, socktype, protocol);

family = AF_INET, AF_APPLETALK, etc.

socktype = SOCK_STREAM,

SOCK_DGRAM, SOCK_RAW

protocol = 0, TCP_PROTO, IP_PROTO

example - TCP socket: s = socket(AF_INET, SOCK_STREAM, 0);

◆

used by both client/server

connect(2) - client

rc = connect(sd, struct sockaddr *sa, len);

client connects to server that can accept

normally TCP, but UDP rarely might use

client must fill in server port, ip address

TCP will attempt to connect to remotemachine

client side TCP has client TCP port - implicit bind

TCP/UDP ports

distinct 64k port spaces

client has port, server has port

o.s. may typically allocate client portdynamically

server SETS port, as “well-known” number;i.e., client sends packets to that port

server port == which service(telnet/ftp/web)

sockaddr structure

sockaddr is generic structure,

struct sockaddr_in is instance of it for INET

struct sockaddr_un for UNIX sockets

used in bind, connect, accept, sendto,recvfrom calls when ip/port # needs to bepassed to/from kernel

ip addr/port # are in NETWORK byte order

sockaddr_in - address structure

◆

struct sockaddr_in {

short sin_family; /* AF_INET ;u_short sin_port;struct in_addr sin_addr; / ip addr */char sin_zero[8];

/* pad */

◆

struct in_addr {

u_long s_addr;

accept(2)

◆

int csd = accept(lsd, struct sockaddr *sa, *len);

◆

accepts connection - paired with connect(2)

◆

blocks without select(2) call until connectionarrives,

returns connected sd

◆

now in connected state, can make read/write calls,use connected sd (not listen sd)

◆

returns client ip/port in sockaddr_in

NOTE: len is

call by value-result

accept(2) no substitute

what is the problem here? int sock;struct sockaddr_in recvSock;int len = sizeof(struct sockaddr_in);int rc = accept(sock, &recvSock, len); Can you say BOOM!!!!!!!...

read(2) / write(2)

◆

with normal TCP, read may return less data thanyou expect. Call it in a loop.

◆

example: you ask for 1024, you get two 512 bytepackets.

◆

write

will block until your data is written - don’t

need loop (unless you use non-blocking i/o)

◆

note:

read(fd, char *buf, int cc)

TCP addresses setup at connect time!

syscalls - UDP client/simple test server

int s = socket(2)gethostbyname(3)for(;;)

sendto(2)recvfrom(2) close(2)

int s = socket(2)bind(2) for(;;)

recvfrom(2)do something...sendto(2) close(2)

init i/o

client

server