Без темы
<<  Sociology 2: Class 17: Globalization, Culture, Identity Soil Erosion in Nepal  >>
Socket Programming: a Primer
Socket Programming: a Primer
Why does one need sockets
Why does one need sockets
So what exactly does a socket do
So what exactly does a socket do
Types of Sockets
Types of Sockets
How should one define a socket
How should one define a socket
How to create a socket
How to create a socket
How to define address
How to define address
Bind a Socket
Bind a Socket
How to convert addresses
How to convert addresses
Using a Datagram Socket
Using a Datagram Socket
Using a Stream Socket
Using a Stream Socket
Using a Stream Socket (contd)
Using a Stream Socket (contd)
A Quick Summary: Datagram
A Quick Summary: Datagram
A Quick Summary: Stream
A Quick Summary: Stream
Sample Codes: Datagram Client
Sample Codes: Datagram Client
Sample Code: Datagram Client
Sample Code: Datagram Client
Sample Code: Datagram Server
Sample Code: Datagram Server
Sample Code: Datagram Server
Sample Code: Datagram Server
Sample Code: Stream Client
Sample Code: Stream Client
Sample Code: Stream Client
Sample Code: Stream Client
Sample Code: Stream Server
Sample Code: Stream Server
Sample Code: Stream Server
Sample Code: Stream Server
Further Reading
Further Reading

Презентация: «Socket Programming: a Primer». Автор: Linhai He. Файл: «Socket Programming: a Primer.ppt». Размер zip-архива: 67 КБ.

Socket Programming: a Primer

содержание презентации «Socket Programming: a Primer.ppt»
СлайдТекст
1 Socket Programming: a Primer

Socket Programming: a Primer

2 Why does one need sockets

Why does one need sockets

application

network

network protocol

Feb. 23, 2001

EE122, UCB

2

3 So what exactly does a socket do

So what exactly does a socket do

It is an API between applications and network protocol software Functions it provides: Define an “end-point” for communication Initiate and accept a connection Send and receive data Terminate a connection gracefully Supports multiple protocol families Examples: Unix inter-process communication, TCP/IP Only Internet sockets will be covered in this lecture

Feb. 23, 2001

EE122, UCB

3

4 Types of Sockets

Types of Sockets

Two different types of sockets: stream vs. datagram Stream socket: (a.k.a. connection-oriented socket) It provides reliable, connected networking service Error free; no out-of-order packets (uses TCP) applications: telnet, http, … Datagram socket: (a.k.a. connectionless socket) It provides unreliable, best-effort networking service Packets may be lost; may arrive out of order (uses UDP) applications: streaming audio/video, …

Feb. 23, 2001

EE122, UCB

4

5 How should one define a socket

How should one define a socket

To define an end-point of communication, one needs to specify the family of protocol it uses (Internet vs. others) addressing information (IP address + port number) the type of service it provides (stream vs. datagram) Done in three steps create a socket define address and port number associate address with the socket

Feb. 23, 2001

EE122, UCB

5

6 How to create a socket

How to create a socket

Notice that the socket descriptor is just a regular int ! So it has the same usage as a file descriptor in Unix…

# include <sys/types.h> # include <sys/socket.h> int sock; sock = socket (AF_INET, SOCK_STREAM, 0); /* for stream */ sock = socket (AF_INET, SOCK_DGRAM, 0); /* for datagram */

Feb. 23, 2001

EE122, UCB

6

7 How to define address

How to define address

struct sockaddr { u_short sa_family; char sa_data[14]; }

Feb. 23, 2001

EE122, UCB

7

8 Bind a Socket

Bind a Socket

bind( ): associate a socket descriptor with an address putting everything together

int bind (int sockfd, struct sockaddr *addr, int len);

int sockfd; struct sockaddr_in addr; sockfd=socket(AF_INET,SOCKE_STREAM, 0); addr.sin_family=AF_INET; addr.sin_port=htons(5000); /* 0: randomly assigned by OS */ addr.sin_addr.s_addr=htonl(INADDR_ANY); /* local address */ bzero(&(addr.sin_zero),8); /* pad zeros */ bind(sockfd,(struct sockaddr *)&addr, sizeof(struct sockaddr));

Feb. 23, 2001

EE122, UCB

8

9 How to convert addresses

How to convert addresses

You also need to define address for the other end If you know its IP address addr.sin_addr.s_addr=inet_addr(“128.32.138.240”); If you know its name only: need to perform a DNS lookup

struct hostent *gethostbyname(char *name)

struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0] }

Feb. 23, 2001

EE122, UCB

9

10 Using a Datagram Socket

Using a Datagram Socket

Sending data int sendto(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *dest, int dest_len); Receiving data int recvfrom(int sockfd, void *msg, int msg_len, u_short flags, struct sockaddr *src, int src_len);

Feb. 23, 2001

EE122, UCB

10

11 Using a Stream Socket

Using a Stream Socket

establishing a connection

server

client

listen ()

int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *addr, int addr_len); int accept(int sockfd, void *addr, int *addrlen );

Feb. 23, 2001

EE122, UCB

11

12 Using a Stream Socket (contd)

Using a Stream Socket (contd)

Sending data int send(int sockfd, void *msg, int msg_len, u_short flags); Receiving data int recv(int sockfd, void *msg, int msg_len, u_short flags); Notice that no address is required!

Feb. 23, 2001

EE122, UCB

12

13 A Quick Summary: Datagram

A Quick Summary: Datagram

server

client

socket() to create socket

bind() to a receiving port

recvfrom() sendto ()

recvfrom() sendto ()

socket() to create socket

bind() to any port

Feb. 23, 2001

EE122, UCB

13

14 A Quick Summary: Stream

A Quick Summary: Stream

server

client

socket()

bind() to a receiving port

listen () to socket

accept () connection

send () recv ()

connect () To server

bind() to any port

send () recv ()

socket()

Feb. 23, 2001

EE122, UCB

14

15 Sample Codes: Datagram Client

Sample Codes: Datagram Client

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define Bfsize 1024 main(int argc, char *argv[]) { int sock; struct sockaddr_in client, server; struct hostent *host, *gethostname(); sock=socket(AF_INET, SOCK_DGRAM, 0); /* open socket */ client.sin_family=AF_INET; client.sin_addr.s_addr=htonl(INADDR_ANY); /* local addr */ client.sin_port=htons(0); /* any port # */ bind(sock,(struct sockaddr *)&client,sizeof(client));

Feb. 23, 2001

EE122, UCB

15

16 Sample Code: Datagram Client

Sample Code: Datagram Client

Don’t forget error handling when calling these functions in your programs!

host=gethostbyname(argv[1]); /* get host name */ memcpy((char *)&server.sin_addr, (char *)host->h_addr, host->h_length); server.sin_family=AF_INET; server.sin_port=htons(atoi(argv[2])); sendto(sock,msg,sizeof(msg),0,(struct sockaddr *)&server, sizeof(server)); close(sock); }

Feb. 23, 2001

EE122, UCB

16

17 Sample Code: Datagram Server

Sample Code: Datagram Server

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define Bfsize 1024 main(int argc, char *argv[]) { int sock, length, count; struct sockaddr_in server, client; char buffer[Bfsize]; sock=socket(AF_INET, SOCK_DGRAM,0); server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(atoi(argv[1])); /* listening port */ bind(sock,(struct sockaddr *)&server,sizeof(server));

Feb. 23, 2001

EE122, UCB

17

18 Sample Code: Datagram Server

Sample Code: Datagram Server

count=recvfrom(sock, buffer, Bfsize, 0, (struct sockaddr *)&client,&length); printf("---> %s\n", buffer); close(sock); }

Feb. 23, 2001

EE122, UCB

18

19 Sample Code: Stream Client

Sample Code: Stream Client

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define msg “hello ee122“ main(int argc, char *argv[]) { int sock; struct sockaddr_in client, server; struct hostent *host, *gethostname(); host=gethostbyname(argv[1]); memcpy((char *)&server.sin_addr,(char *)host->h_addr,host->h_length); server.sin_family=AF_INET; server.sin_port=htons(atoi(argv[2])); /* no bind is needed! */ sock=socket(AF_INET, SOCK_STREAM,0); connect(sock,(struct sockaddr *)&server, sizeof(server));

Feb. 23, 2001

EE122, UCB

19

20 Sample Code: Stream Client

Sample Code: Stream Client

send(sock,msg,sizeof(msg),0); close(sock); }

Feb. 23, 2001

EE122, UCB

20

21 Sample Code: Stream Server

Sample Code: Stream Server

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define Bfsize 1024 main(int argc, char *argv[]) { int new_sock, sock, length, count; struct sockaddr_in server, client; struct hostent *host, *gethostbyname(); char buffer[Bfsize]; sock=socket(AF_INET, SOCK_STREAM,0); server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(atoi(argv[1])); bind(sock, (struct sockaddr *)&server, sizeof(server)); listen(sock,1);

Feb. 23, 2001

EE122, UCB

21

22 Sample Code: Stream Server

Sample Code: Stream Server

new_sock = accept(sock,(struct sockaddr *)&client,&length); count=recv(new_sock,buffer,Bfsize,0); printf("---> %s\n", buffer); close(new_sock); close(sock); }

Feb. 23, 2001

EE122, UCB

22

23 Further Reading

Further Reading

W. Richard Stevens. Unix Network Programming, Prentice Hall. The Unix Socket FAQ. http://www.ibrado.com/sock-faq

Feb. 23, 2001

EE122, UCB

23

«Socket Programming: a Primer»
http://900igr.net/prezentacija/anglijskij-jazyk/socket-programming-a-primer-60718.html
cсылка на страницу
Урок

Английский язык

29 тем
Слайды