programming c sockets

24
Programming C Sockets Stefan Guna [email protected] 1

Upload: others

Post on 11-Jun-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming C Sockets

Programming  C  Sockets

Stefan  Guna

[email protected]

1

Page 2: Programming C Sockets

IntroductionWhy  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

2

Page 3: Programming C Sockets

IntroductionStream  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    

3

Page 4: Programming C Sockets

IntroductionStream  sockets

socket()

bind()

listen()

accept()

send()recv()

close()

socket()

connect()

send()recv()

close()Server Client

4

Page 5: Programming C Sockets

IntroductionStream  sockets

socket()

bind()

listen()

accept()

send()recv()

close()

socket()

connect()

send()recv()

close()

Create  the  socket

Server Client

4

Page 6: Programming C Sockets

IntroductionStream  sockets

socket()

bind()

listen()

accept()

send()recv()

close()

socket()

connect()

send()recv()

close()

Create  the  socket

Bind  to  IP  address  and  

port

Server Client

4

Page 7: Programming C Sockets

IntroductionStream  sockets

socket()

bind()

listen()

accept()

send()recv()

close()

socket()

connect()

send()recv()

close()

Create  the  socket

Bind  to  IP  address  and  

port

Start  listening…

Server Client

4

Page 8: Programming C Sockets

IntroductionStream  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

4

Page 9: Programming C Sockets

IntroductionStream  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

4

Page 10: Programming C Sockets

IntroductionStream  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

Connect  to  IP  address  and  port

Server Client

4

Page 11: Programming C Sockets

IntroductionDatagram  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

5

Page 12: Programming C Sockets

IntroductionDatagram  sockets

socket()

bind()

close()

socket()

close()

bind()

recvfrom() sendto()

recvfrom()

socket()

sendto()

close()

6

Page 13: Programming C Sockets

IntroductionDatagram  socketsCreate  

socket

socket()

bind()

close()

socket()

close()

bind()

recvfrom() sendto()

recvfrom()

socket()

sendto()

close()

6

Page 14: Programming C Sockets

IntroductionDatagram  socketsCreate  

socket

Bind  to  local  IP  address  and  port

socket()

bind()

close()

socket()

close()

bind()

recvfrom() sendto()

recvfrom()

socket()

sendto()

close()

6

Page 15: Programming C Sockets

IntroductionDatagram  socketsCreate  

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…

6

Page 16: Programming C Sockets

Example  1Hello  World!  using  TCP

clients  connects  to  server

server  transmits  “Hello  World!”

client  displays  the  message

connection  terminates

connect

"Hello World!"

7

Page 17: Programming C Sockets

Example  1server:  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  socketotherwise:  SOCK_DGRAM

using  TCPotherwise:  IPPROTO_UDP

8

Page 18: Programming C Sockets

Example  1server:  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

9

Page 19: Programming C Sockets

Example  1server:  dealing  with  clients

while(1) { struct sockaddr_in client_addr; int client_len = sizeof(client_addr); int client_fd = accept(sockfd, &client_addr, &client_len); strcpy(buf, "Hello World!"); send(client_fd, buf, strlen(buf) + 1, 0); close(client_fd); }

a  new  socket  is  created  for  each  client

10

Page 20: Programming C Sockets

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);

hostnametry  “www.google.com”

11

Page 21: Programming C Sockets

Example  1client:  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);

12

Page 22: Programming C Sockets

Example  2Hello  World!  using  UDP

client  sends  an  UDP  message  “Hello  World!”  to  server

the  server  is  almost  identical  to  the  TCP  version

the  client  does  not  need  to  call  connect

"Hello World!"

13

Page 23: Programming C Sockets

Example  2server

int sockfd = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);

struct sockaddr_in my_addr, client_addr;my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY)my_addr.sin_port = htons(MY_PORT);

/* bind it to the local address */bind(sockfd, &my_addr, sizeof(my_addr));listen(sockfd, MAX_CONNECTIONS);int client_addr_len;int len = recvfrom(sockfd, buf, MAX_BUF, 0, &client_addr, &client_addr_len);

the  receiver  should  know  who  sent  the  data

14

Page 24: Programming C Sockets

Example  2client

struct hostent *server_host;server_host = gethostbyname("localhost");

/* configure the server address */struct sockaddr_in server_addr;server_addr.sin_family = AF_INET; // IPv4memcpy(&server_addr.sin_addr, server_host->h_addr, sizeof(struct in_addr));server_addr.sin_port = htons(SERVER_PORT);

int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

/* send a message */strcpy(buf, "Hello World!");sendto(sockfd, buf, strlen(buf) + 1, 0, &server_addr, sizeof

(server_addr));

15