Professional Documents
Culture Documents
Sockets
Sockets
message
client server
A brief tutorial other ports
10/1/2008 1 10/1/2008 2
1
Socket types Functions : creation
• The format in which an address is specified is according • Socket creation : socket system call creates sockets
to a domain: on demand.
– AF_UNIX (address format of UNIX) - a path name
sockid = socket (af, type, protocol);
within the file system,
where sockid is an int,
– AF_INET (internet format) : network address, port
number etc. af - address family , AF_INET, AF_UNIX,
AF_AAPLETALK etc.
• Communication style: stream , datagram, raw or
sequenced packets type - communication type:
• Stream : reliable, error-free, connection-oriented comm. SOCK_STREAM, SOCK_DGRAM, SOCK_RAW etc.
• Datagram: Connectionless, unreliable, message protocol - some domains have multiple protocol, use a
boundaries preserved. 0 for your appl.
Example: door1 = socket(AF_INET, SOCK_DGRAM,0);
10/1/2008 5 10/1/2008 6
2
Functions (contd.) - close Functions (contd.) - connect
close (socid); closes the specified socket. This is • A socket is created in an unconnected state, which
means that the socket is not associated with any
done by a process or thread when it no longer destination.
needs the socket connection. Premature closing • An application program should call connect to
results in “broken pipe” error. establish a connection before it can transfer data
thru’ reliable stream socket. For datagrams connect
• When a socket is created it is represented by a is not required but recommended.
special file (‘s’ in the place where d appears for connect ( sockid, destaddr, addlength);
directory files when you ls -l).The name of the Example: if (connect(sock, &server, sizeof(server)) < 0)
file is the name assigned in the socket bind ...
command. • “sendto” commend does not need “connect”
10/1/2008 9 10/1/2008 10
10/1/2008 11 10/1/2008 12
3
Functions (contd.) -listen Functions (contd.) - accept
• accept and listen are connection-oriented • accept : blocks until a connect calls the socket
communication.
associated with this connection. socket -- bind --
• These are for AF_INET, SOCK_STREAM type of
sockets. (listen) -- accept is the sequence. “connect” from
• listen: To avoid having protocols reject incoming calling process will complete the connection and
request, a server may have to specify how many unblock the process or thread that is blocked on
messages need to be queued until it has time to “accept”
process them. Example: listen(socket,length);
• accept: wait for the call. Example: accept(sockid, • Now read, write or writev can be executed to
sockaddr, sizeof sockaddr); carry out the actual communication over the
connection established.
10/1/2008 13 10/1/2008 14
Functions (contd.) -
getsockname Sockets used for datagrams
• getsockname (sockid, sockaddr, sizeof
sockaddr); : given a sockid returns the address Sending a message Receiving a message
10/1/2008 15 10/1/2008 16
4
Socket API--UDP
Sockets used for streams
bind(s, ServerAddress);
listen(s,5);
connect(s, ServerAddress)
sNew = accept(s, ClientAddress);
write(s, "message", length) n = read(sNew, buffer, amount)
10/1/2008 17 10/1/2008 18
5
Simple Example—A Time Server
//This simple server is not multithreaded
Time Server Example--Continued
#include <stdio.h>
#include <sys/types.h> /* bind address */
#include <sys/socket.h> serv_addr.sin_family = AF_INET;
#include <netinet/in.h> serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
#include <netdb.h> serv_addr.sin_port = htons(TIME_PORT);
#include <stdlib.h> if (bind(sockfd, &serv_addr, sizeof(serv_addr)) < 0) {
#include <string.h> perror(NULL);
#include<strings.h> exit(3);
#define SIZE 1024 }
char buf[SIZE]; /* specify queue */
#define TIME_PORT 2013 //server will listen on this port len = sizeof(client_addr);
int main(int argc, char *argv[]) { listen(sockfd, 5);
int sockfd, client_sockfd; for (;;) {
int nread, len; client_sockfd = accept(sockfd, &client_addr, &len);
struct sockaddr_in serv_addr, client_addr; if (client_sockfd == -1) {
time_t t; perror(NULL);
/* create endpoint */ continue;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { }
perror(NULL);
10/1/2008 exit(2); 21 10/1/2008 22
}
10/1/2008 23 10/1/2008 24
6
Time Server Example--Continued
Time Server Example --Continued
/* bind address */
serv_addr.sin_family = AF_INET; /* transfer data */
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); time(&t);
serv_addr.sin_port = htons(TIME_PORT); sprintf(buf, "%s", asctime(localtime(&t)));
if (bind(sockfd, &serv_addr, sizeof(serv_addr)) < 0) { len = strlen(buf) + 1;
perror(NULL); write(client_sockfd, buf, len);
exit(3); close(client_sockfd);
} }
/* specify queue */ }
len = sizeof(client_addr);
listen(sockfd, 5);
for (;;) {
client_sockfd = accept(sockfd, &client_addr, &len);
if (client_sockfd == -1) {
perror(NULL);
continue;
}
10/1/2008 25 10/1/2008 26
10/1/2008 27 10/1/2008 28
7
A Time Client
/* TCP client that finds the time from a server */
A Time Client
/* TCP client that finds the time from a server */
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#define SIZE 1024 #define SIZE 1024
char buf[SIZE]; char buf[SIZE];
#define TIME_PORT 2013 //server listens on this port #define TIME_PORT 2013 //server listens on this port
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int sockfd; int sockfd;
int nread; int nread;
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
struct hostent * h; struct hostent * h;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "usage: %s IPaddr\n", argv[0]); fprintf(stderr, "usage: %s IPaddr\n", argv[0]);
exit(1); exit(1);
} }
/* create endpoint */ /* create endpoint */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(NULL); perror(NULL);
exit(2); exit(2);
} }
10/1/2008 29 10/1/2008 30
10/1/2008 31 10/1/2008 32
8
Time Client--Continued Time Client--Continued
/* connect to server */ /* connect to server */
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
h = gethostbyname(argv[1]); h = gethostbyname(argv[1]);
bcopy(h->h_addr, (char *) &serv_addr.sin_addr, h->h_length); bcopy(h->h_addr, (char *) &serv_addr.sin_addr, h->h_length);
serv_addr.sin_port = htons(TIME_PORT); serv_addr.sin_port = htons(TIME_PORT);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror(NULL); perror(NULL);
exit(3); exit(3);
} }
/* transfer data */ /* transfer data */
nread = read(sockfd, buf, SIZE); nread = read(sockfd, buf, SIZE);
write(1, buf, nread); // Writes to standard output write(1, buf, nread); // Writes to standard output
close(sockfd); close(sockfd);
exit(0); exit(0);
} }
10/1/2008 33 10/1/2008 34
Time Client--Continued
/* connect to server */
serv_addr.sin_family = AF_INET;
h = gethostbyname(argv[1]);
bcopy(h->h_addr, (char *) &serv_addr.sin_addr, h->h_length);
serv_addr.sin_port = htons(TIME_PORT);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror(NULL);
exit(3);
}
/* transfer data */
nread = read(sockfd, buf, SIZE);
write(1, buf, nread); // Writes to standard output
close(sockfd);
exit(0);
}
10/1/2008 35