0% found this document useful (0 votes)
24 views

Sockets

Sockets

Uploaded by

bushra.phd2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Sockets

Sockets

Uploaded by

bushra.phd2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sockets and ports

The Socket API


any port agreed port
socket socket

message
client server
A brief tutorial other ports

Internet address = 128.255.94.248 Internet address = 128.255.88.249

10/1/2008 1 10/1/2008 2

Inter Process Communication Socket Names


• IP address and port number. About 216 ports are
available for use by user processes. • Applications refer to sockets by name.
• UDP and TCP abstraction of the above is a • But within the communication domain
socket.
• Socket is associated with a protocol. sockets are referred by addresses.
• IPC is transmitting a message between a socket • Name to address translation is usually
in one process to a socket in another process.
• Messages sent to particular IP and port# can be
done outside the operating system.
received by the process whose socket is
associated with that IP and port#.
• Processes cannot share ports with other
processes within the computer. Can receive
messages on diff ports.
10/1/2008 3 10/1/2008 4

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

Functions - bind Functions -bind (contd.)


• Socket binding: A socket is created without any • Example: type sockaddr_in defines
association to local or destination address. It is localaddr format for INET family.
possible to bind the socket to a specific host and its definition, (you don’t have to define
in it a specific port number. it... it is in in.h file - include this file)
socerr = bind (sockid, localaddr, addrlength) struct sockaddr_in {
localaddr - a struct of a specific format for each short sin_family;
address domain; u_short sin_port;
addrlength - is the length of this struct; obtained struct in_addr sin_addr;
usually by sizeof function.
char sin_zero[8];
}
10/1/2008 7 10/1/2008 8

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

Functions (contd.) -sending Functions(contd.) - receiving


• Five different system calls : send, sendto, sendmsg, write, • Five different calls are available: read, readv, recv,
writev recvfrom, recvmsg
• send, write and writev work only with connected sockets.
No parameter for destination address. Prior connect should • read, readv, and recv are for connection-oriented comm.
be present for communication. • read(socdescriptor, buffer, length); Example:
• Example : write (sock, DATA, sizeof(DATA)); read(sock, buf, 1024);
• sendto (socket, message, length, flags, destaddr, • For your application (project) you may use read.
addlen); • For connectionless, datagram-kind :
• flags allow for special processing of messages. Use 0 in • recvfrom(same set of params as sendto); except that
your appln. message length and addr length return values.
• sendmsg is same as sento except that it allows for different
message structure.

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

of the socket identified by sockid. s = socket(AF_INET, SOCK_DGRAM, 0) s = socket(AF_INET, SOCK_DGRAM, 0)

• This address may be needed, for instance, by an


bind(s, ClientAddress) bind(s, ServerAddress)
accept function call.
sendto(s, "message", ServerAddress) amount = recvfrom(s, buffer, from)
• There are other functions such as
gethostbyname may be needed for internet
domain sockets. See man pages for the details. ServerAddress and ClientAddress are socket addresses

10/1/2008 15 10/1/2008 16

4
Socket API--UDP
Sockets used for streams

Requesting a connection Listening and accepting a connection

s = socket(AF_INET, SOCK_STREAM,0) s = socket(AF_INET, SOCK_STREAM,0)

bind(s, ServerAddress);
listen(s,5);
connect(s, ServerAddress)
sNew = accept(s, ClientAddress);
write(s, "message", length) n = read(sNew, buffer, amount)

ServerAddress and ClientAddress are socket addresses

10/1/2008 17 10/1/2008 18

Socket API--TCP Simple Example—A Time Server


//This simple server is not multithreaded
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include<strings.h>
#define SIZE 1024
char buf[SIZE];
#define TIME_PORT 2013 //server will listen on this port
int main(int argc, char *argv[]) {
int sockfd, client_sockfd;
int nread, len;
struct sockaddr_in serv_addr, client_addr;
time_t t;
/* create endpoint */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(NULL);
10/1/2008 19 10/1/2008 exit(2); 20
}

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
}

Time Server Example--Continued Time Server Example--Continued

/* bind address */ /* bind address */


serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(TIME_PORT); serv_addr.sin_port = htons(TIME_PORT);
if (bind(sockfd, &serv_addr, sizeof(serv_addr)) < 0) { if (bind(sockfd, &serv_addr, sizeof(serv_addr)) < 0) {
perror(NULL); perror(NULL);
exit(3); exit(3);
} }
/* specify queue */ /* specify queue */
len = sizeof(client_addr); len = sizeof(client_addr);
listen(sockfd, 5); listen(sockfd, 5);
for (;;) { for (;;) {
client_sockfd = accept(sockfd, &client_addr, &len); client_sockfd = accept(sockfd, &client_addr, &len);
if (client_sockfd == -1) { if (client_sockfd == -1) {
perror(NULL); perror(NULL);
continue; continue;
} }

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

Time Server Example --Continued Time Server Example --Continued


/* transfer data */ /* transfer data */
time(&t); time(&t);
sprintf(buf, "%s", asctime(localtime(&t))); sprintf(buf, "%s", asctime(localtime(&t)));
len = strlen(buf) + 1; len = strlen(buf) + 1;
write(client_sockfd, buf, len); write(client_sockfd, buf, len);
close(client_sockfd); close(client_sockfd);
} }
} }

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

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

You might also like