• Ei tuloksia

What Is A Socket?

N/A
N/A
Info
Lataa
Protected

Academic year: 2022

Jaa "What Is A Socket?"

Copied!
33
0
0

Kokoteksti

(1)

Tietoliikennesovellukset

(2)

Tietoliikennesovellukset

• Tietoliikenteeseen on yleensä jokin syy

Käyttäjälle tarjottava palvelu

Ihmisten ja organisaation tarpeita toteuttavasta prosessista aiheutuva tiedonsiirtotarve

• Tarpeiden tyydyttämiseksi suunnitellaan sovellusarkkitehtuuri

Sovellukset kommunikoivat keskenään

Prosessointi tapahtuu eri paikoissa

(3)

What Is A Socket?

• Sockets (also known as Berkeley Sockets) is an application programming interface (API) to the operating system’s TCP/IP stack

• Used on all Unixes

• Windows Sockets are similar

• Some Unixes have XTI (X/Open Transport

Interface) and TLI (Transport Layer Interface) (not covered here)

• Can be used for other protocol stacks than TCP/IP (not covered here)

(4)

Socket Functions

• Socket functions:

Allocating resources: socket, bind

Opening connections: accept, connect

Closing connections: close, shutdown

Sending and receiving: read, write, recv, send, recvfrom, sendto, recvmsg, sendmsg

Other: getsockopt, setsockopt, getsockname, getpeername

• Utilities

Network information (host names, addresses)

Byte order

(5)

Address Structures

struct in_addr {

in_addr_t s_addr; /* IPv4 address, network byte order */

};

struct sockaddr_in {

sa_family_t sin_family; /* AF_INET */

in_port_t sin_port; /* 16-bit port, network byte order*/

struct in_addr sin_addr; /* IPv4 address */

char sin_zero[8]; /* always zero */

};

(6)

Creating A Socket

• int socket(int domain, int type, int protocol)

• Domain is

AF_INET for TCP/IP protocols

AF_UNIX (AF_LOCAL) for Unix named pipes, others

• Type is

SOCK_STREAM (TCP)

SOCK_DGRAM (UDP)

SOCK_RAW (raw IPv4)

others

• Protocol is usually zero

• Returns new socket descriptor, or -1 on error

(7)

A Typical TCP Client

socket

connect

read/write

close

(8)

Example: Simple HTTP Client (1/4)

/*

* Simple HTTP client program, version 1.

* Written by Pasi.Eronen@nixu.fi.

*/

#include <arpa/inet.h>

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <unistd.h>

(9)

Example: Simple HTTP Client (2/4)

void die(const char* message) {

fprintf(stderr, "%s\n", message);

exit(1);

}

int main(int argc, char *argv[]) {

int sockfd, n;

struct sockaddr_in addr;

unsigned char buffer[4096];

if (argc != 4)

die("usage: geturl ip-address port local-url");

(10)

Example: Simple HTTP Client (3/4)

/* Open socket */

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))

== -1)

die("socket error");

/* Parse address */

memset(&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;

addr.sin_port = htons(atoi(argv[2]));

addr.sin_addr.s_addr = inet_addr(argv[1]);

if (addr.sin_addr.s_addr == -1) die("bad address");

/* Connect to remote host */

if (connect(sockfd, (struct sockaddr*) &addr, sizeof(addr)) == -1)

die("connect error");

(11)

Example: Simple HTTP Client (4/4)

/* Send HTTP request */

write(sockfd, "GET ", 4);

write(sockfd, argv[3], strlen(argv[3]));

write(sockfd, " HTTP/1.0\r\n\r\n", 13);

/* Read response */

while ((n = read(sockfd, buffer,

sizeof(buffer))) > 0) write(STDOUT_FILENO, buffer, n);

/* Close and exit */

close(sockfd);

return 0;

}

(12)

Simple HTTP Client In Action

$ ./httpclient1 192.168.3.4 80 / HTTP/1.1 200 OK

Date: Sat, 24 Apr 1999 17:08:25 GMT Server: Apache/1.3.4

Last-Modified: Fri, 26 Feb 1999 15:28:20 GMT Connection: close

Content-Type: text/html

<html><head><title>Example Inc.</title></head>

<body>

<h1>Welcome to Example Inc’s web server!</h1>

...

$

(13)

What Is A Server

• Background process

• No user interface

• Handles service requests from network

• Can also send requests, for example DNS and NTP

• Typically must handle many requests concurrently

-> some kind of multitasking needed

(14)

What Is Special In A Server

• Concurrency & network I/O

• Protocol encoding/decoding

• Application-specific logic

• System interaction: Unix daemons, Windows NT services

• Logging

• Security

(15)

Network I/O On Unix

• Sockets are file descriptors

• Important I/O operations for TCP sockets:

accept

connect

read, write

close, shutdown

• Important I/O operations for UDP sockets:

sendto

recvfrom

(16)

Binding To Specific Port

• int bind(int sockfd, struct sockaddr *my_addr, int addrlen)

• bind assigns a specific local address and port to the socket

normally used for servers (where the port must be known)

IP address (or IPADDR_ANY)

• In clients, you don’t usually need bind

a random "ephemeral" port is chosen automatically

the correct interface and IP address are chosen automatically

(17)

bind() example

• Bind socket to local port 80 (for HTTP server)

struct sockaddr_in my_addr;

memset(&my_addr, 0, sizeof(my_addr));

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(80);

my_addr.sin_addr.s_addr = INADDR_ANY;

if (bind(sockfd, (struct sockaddr*)

&my_addr, sizeof(my_addr)) == -1) die("bind failed");

(18)

TCP Connections

• When the server calls accept() it gets:

file descriptor for reading/writing data

remote IP + port (from getpeername)

local IP + port (from getsockname)

(19)

UDP "Connections"

• UDP is not really connected

• When the server calls recvfrom() it gets

packet data

remote IP + port

(20)

Iterative UDP Server

initialize

wait for packet

process request

send reply

(21)

Iterative UDP Server

• Single thread of execution

• If processing doesn’t take long, works well!

Otherwise the service is blocked

• Simple, easy to coordinate access to resources

• Must be careful not to use blocking operations:

CPU-intensive tasks

SQL database queries

gethostbyname, gethostbyaddr

(22)

Example: radiusd

• Potentially lots of requests

• Uses UDP

• Very little processing per request

• Solution: single-threaded UDP server.

(23)

Process-per-connection TCP Server

initialize

wait for connection

fork

receive data

process request

send reply

close connection & exit

(24)

Process-per-connection TCP Server

• New process started for each connection

• Good sides:

Easy to use, works!

Reliable: If one process dies, others continue

• Problems:

Starting new processes is slow

Co-operation between processes is limited or difficult

• Access to shared resources (log files, etc.) needs to be coordinated

(25)

Example: telnetd

• Each connection takes quite long, so process starting overhead is not a problem

• Asynchronous I/O would be very difficult

• Solution: process-per-connection TCP server

(26)

Process Pre-allocation

• Since starting processes is slow, start all processes at the beginning

• Memory used by unused processes is wasted.

• Only a limited number of connections concurrently.

• Used very successfully!

(27)

Threads

• Creating threads is much faster than processes

• All modern Unixes and Windows NT have threads

• Shared memory makes co-operation easy

• Access to shared memory needs to be coordinated

(28)

Asynchronous TCP Server

initialize

wait for events

receive data and process

send more reply data

accept new connection

(29)

Asynchronous TCP Server

• Single thread of execution

• Event multiplexing using poll() or select()

• Easy to coordinate access to resources

• Must avoid blocking operations

(30)

Example: Bind DNS Server

• Lots of requests

• Needs to be very fast

• Most requests are UDP, but some TCP

• Very little CPU processing

• In-memory database and cache

(hard to share between processes)

• Needs to be portable to legacy systems -> no threads

• Solution: asynchronous I/O for both UDP and TCP

(31)

Distributed Computing

Generally a view of shared computing and data resources, transparent communications between programs and access to objects located in other hosts

Sun RPC is the first popular protocol CORBA is currently somewhat popular

Both are based on an abstraction layer that hides the network

Web Services and .Net take a slightly different approach XML is used to represent all kinds of objects

Advantages are access to shared resources, transparent communications and flexibility

Disadvantages are added complexity and security risks

These technologies are also called middleware

(32)

Esimerkki: Puhe Internetissä

Voice over IP (VoIP)

Sovellus suunnitellaan tiettyyn ympäristöön

Tehdään oletuksia, jotka vaikuttavat koko elinkaaren Windows/PC, Symbian/GPRS, Java/GPRS...

NAT on/ei, IPv4/6...

Valitaan teknologiat

Olemassa olevat: merkinanto: SIP (vai H.323), siirto: RTP/UDP, puheen koodaus...

Tai tehdään omat: Skype

Tuotteistetaan, julkistetaan, markkinoidaan...

Vai tehdäänkö Push-To-Talk (PTT) over Cellular (PoC) Eri markkinasegmentti, paljon potentiaalia lisäarvolle

ARPU (Average Revenue Per User) $30-40 → $70 (Nextel)

(33)

Esimerkki: Vaatekauppa

• Erottaudutaan markkinoista tarjoamalla

mahdollisuus tutustua vaatteisiin kuvakulmaa vaihtaen ja käännellen

Boo.com, $200 M riskirahaa

Liian raskas sovellus → konkurssi

• Erottaudutaan markkinoista nopeasti vaihtuvalla mallistolla

Yhdistetään koko prosessi suunnittelusta kauppaan tietoliikenteellä, ei välivarastoja

Nykyiset halpavaatemyymälät, Zara

• Erottaudutaan markkinoista halvalla työvoimalla

Thaimaalaiset vaatturit, räätäli käy mittauskierroksella länsimaissa, tuotanto Thaimaassa, lisätilaukset verkon yli

Viittaukset

LIITTYVÄT TIEDOSTOT

awkward to assume that meanings are separable and countable.ra And if we accept the view that semantics does not exist as concrete values or cognitively stored

This account makes no appeal to special-purpose sequenc- ing principles such as Grice's maxim of orderliness or Dowty's Temporal Discourse Interpretation Principle;

On the other side, the Qurʾānic disciplines (ʿulūm al-Qurʾān) have developed a number of textual and hermeneutical tools to contextalize the text in view of the general

Subsequently, we systematise what previous publications refer to using the terms “visual intimacy” or “visual intimacies.” We thus map how visual intimacy has been examined in

It is impossible to allow dynamic shared state to It is impossible to allow dynamic shared state to change frequently and guarantee that all hosts change frequently and

The researchers involved in this study suggest that children’s own experiences of languages fundamentally affect the way in which they see and make sense of the world, in other

Following a strict family involvement definition including only formal involvement in the family businesses, these kinds of persons would not be categorised as involved in

What is sent over the socket is decided by programmer Actual communication is handled by OS, socket operations are syscalls.. Introduction and Overview Socket Programming