SlideShare a Scribd company logo
Linux System
Programming
Socket Programming
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
 A socket is a communication mechanism that allows client/server systems to
be developed either locally, on a single machine, or across networks.
e.g. A Web browser uses sockets to communicate with web server.
Network Addresses
 Every host on a network has, at a minimum, two unique addresses.
 The first unique address is a 48-bit media access control (MAC) address
that is assigned to its network interface card (NIC).
 The manufacturer of the card assigns this address. e.g. 00:50:56:8A:01:0D
 The MAC address is written in hexadecimal notation, broken into six 8-bit
numbers. The first three groupings (bytes) of this number identify the
hardware manufacturer.
Introduction to Sockets
 Run following command to see the recently resolved addresses ( Internet
addresses and their hardware address )
cat /proc/net/arp
ifconfig -a // To see MAC and IP Address of your PC
 The second unique address for a host is a 32-bit Internet Protocol (IP)
address. e.g. 192.168.10.254
 A 32-bit IP address is broken into four 8-bit numbers, each separated by a dot
(.), each of the four sub-numbers can range from 0 to 255.
 An IPv4 Internet address may be subdivided into a network and local portion.
The network portion, or netid, occupies the leftmost portion of the IP
address, and the local portion, or hostid, the rightmost portion.
 Using the leading bits of the netid value, networks can be divided into five
classes. With IPv6, the 32-bit IP addresses will become 128-bit.
Network Addresses
 In a Class A network bit 0 is 0;
 in a Class B the first two bits are 10
 In a Class C the first 3 bits are 110.
 Class D is used for multicasting.
 Class E is reserved for experimental use.
 Class E and F do not have host-addressing scheme.
IP v4 Address
IP Address: 192.168.10.200 = 1100 0000.1010 1000.0000 1010.1100 1000
Subnet Mask: 255.255.255.224/27 = 1111 1111.1111 1111.1111 1111.1110 0000
 Subnet Mask is 27-bit: A 27-bit Subnet Mask means that the first 27 bits of the IP address
are used for the network portion, leaving 32 - 27 = 5 bits for the host portion.
 Number of Subnets:
IP Address:192.168.10.200 = 1100 0000.1010 1000.0000 1010.1100 1000
Subnet Mask:255.255.255.224/27 = 1111 1111.1111 1111.1111 1111.1110 0000 &
------------------------------------------------------------------------
Network Address:192.168.10.192 = 1100 0000.1010 1000.0000 1010.1100 0000
 Usable IP Range: 11000000.10101000.00001010.11100001 (192.168.10.193) to
11000000.10101000.00001010.11111110 (192.168.10.222)
 Broadcast Address: 11000000.10101000.00000010.11111111 (192.168.10.223)
IP v4 Address
 Gateway Address: Typically, the gateway address is assigned to the first usable IP address in
the subnet. So, in this case, the gateway address would be 192.168.10.193.
 Hosts per Network: 25 – 2 = 32 – 2 = 30 hosts.
 Run Following Command in Windows: ipconfig -all
IP v4 Address
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . : Home
Description . . . . . . . . . . . : Intel(R) Dual Band Wireless-AC 7265
Physical Address. . . . . . . . . : 64-80-99-CD-D3-D1
IPv4 Address. . . . . . . . . . . : 192.168.10.4(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.10.1
DHCP Server . . . . . . . . . . . : 192.168.10.1
DNS Servers . . . . . . . . . . . : 1.1.1.1
8.8.8.8
 We often map a dotted IP address into a more easily understood symbolic
notation using the Domain Name System (DNS).
 In this schema, all Internet addresses are divided into a set of high-level
organizational and geographical domains.
 Each organizational domain (top-level domain) has an identifying code , such
as .com (commercial), .edu (education), and .gov (government).
 Each geographical domain some times consists of a two-letter country code,
such as .pk (Pakistan).
 In pc1.dee.fet.iiu.edu.pk under the domain of pk there is an educational
domain edu under that there is domain of iiu university under that there is
sub domain of fet then dee under that there is a system called pc1 using IP
Address 192.168.10.1.
Domains and Networks
 The last name in the sequence (the leftmost) is usually the name of the host.
 A DNS Server using distributed DNS database information dynamically maps a
domain-name reference to its corresponding Internet (IP) address.
 In Linux you can use hostname, nslookup and dig commands for DNS queries.
See the man pages for help.
hostname // view hostname of your system
 The following command show DNS records associated with the hostname
nslookup www.google.com.pk // shows 172.217.19.195 is the IP address
 Similarly you can use dig command
dig www.google.com // shows 172.217.17.36 is the IP address
Domains and Networks
 When we create the socket, we specify its communication domain. The two
types of socket communication domains are as follows:
 UNIX domain: In this domain, when sockets are created, they have actual
file (path) names.
 These sockets can be used only with processes that reside on the same
host.
 Internet domain: In this domain, sockets allow unrelated processes on
different hosts to communicate.
Sockets
 Sockets programming uses the layered model of packet communication
 Using socket APIs we make a program of some application that runs at
Application Layer.
 The Transport layer provides the transport protocols.
 TCP (Transmission Control Protocol) and UDP (User Datagram Protocol)
 The Network Layer provides among other things routing over the Internet.
This layer is occupied by the Internet
Protocol, or IP.
 Physical Layer driver provides the
means to introduce packets onto the
physical network.
Layered Model of Networking
Sockets Programming Paradigm
 Hosts are identified by addresses, and for
IP (Internet Protocols), these are called IP
addresses. An IPv4 address (of the
version 4 class) is defined as a 32-bit
address.
 This address is represented by four 8-bit
values. A sample address can be
illustrated as: 192.168.1.1 or 0xC0A80101
 The first value shows the more popular
form of IPv4 addresses, which is easily
readable.
 The second notation is simply the first
address in hexadecimal format (32 bits
wide).
 The Protocol specifies the details of communication over the socket.
 The two most common protocols used are the Transmission Control Protocol
(TCP) and the User Datagram Protocol (UDP). TCP is a stream-based reliable
protocol, and UDP is a datagram (message)-based protocol that can be
unreliable.
 The Port is the endpoint for a given process (interface) for a protocol. This is
the application’s interface to the Socket interface. Ports are unique on a host
for a given protocol. Ports are commonly called “bound” when they are
attached to a given socket.
 Ports are numbers that are split basically into two ranges. Port numbers below
1024 are reserved for well-known services (called well-known addresses). Port
numbers above 1024 are typically used by applications.
Sockets Programming Paradigm
Sockets Programming Paradigm
Figure: Visualization of a Socket between two hosts.
Client / Server Model
 In most Sockets applications, there
exists a server (responds to requests
and provides responses) and a client
(makes requests to the server).
 Figure : Client/server symmetry in
Sockets applications
 The first step is the creation of a sockets (communication endpoint) by the
socket call. both the server and client perform this step.
 The server requires a bit more setup as part of registering a service to the
host. The bind call binds an address and port to the server so that it’s known.
If we choose the port, we know what it is.
 Once we’ve bound our port, we call the listen function for the server. This
makes the server accessible (puts it in the listen mode).
 We establish our socket next, using connect at the client and accept at the
server.
 The connect call starts what’s known as the three-way handshake, with the
purpose of setting up a connection between the client and server.
 At the server, the accept call creates a new server-side client socket.
 Once accept finishes, a new socket connection exists between the client and
Client / Server Model
server, and data flow can occur.
 In the data transfer phase, we have an established socket for which
communication can occur. Both the client and server can send and recv data
asynchronously.
 Finally, we can close the connection between the client and server using the
close call. This can occur asynchronously, but upon one endpoint closing the
socket, the other side will automatically receive an indication of the closure.
 Let’s do an example of a very simple socket client program
 local_client.c. It creates an unnamed socket and connects it to a server
socket called local_server
Client / Server Model
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;
// 1. Remove any old sockets and create an unnamed socket
unlink("server_socket"); // for the server:
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
server_address.sun_family = AF_UNIX; // 2. Name the socket:
strcpy(server_address.sun_path, "server_socket");
server_len = sizeof(server_address);
A Simple Local Server local_server.c
bind(server_sockfd,(struct sockaddr*)&server_address, server_len);
// 3. Create a connection queue and wait for clients:
listen(server_sockfd, 5);
while(1) {
char ch;
printf("server waitingn");
// 4. Accept a connection:
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,
&client_len);
// 5. Read and write to client on client_sockfd:
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
A Simple Local Server local_server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int sockfd;
int len;
struct sockaddr_un server_address;
int result; char ch = 'A';
sockfd = socket(AF_UNIX, SOCK_STREAM, 0); // 1. Create a socket for the client
// 2. Name the socket as agreed with the server:
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
len = sizeof(server_address);
A Simple Local Client local_client.c
Address Family
AF_INET or AF_UNIX
Socket Type
SOCK_STREAM or SOCK_DGRAM
Protocol
0 for protocol
selected by OS
// 3. Connect your socket to the server’s socket:
result = connect(sockfd,(struct sockaddr*) & server_address, len);
if(result == -1) {
perror("Oops: local_client");
exit(1);
}
// 4. You can now read and write via sockfd:
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from local server = %cn", ch);
close(sockfd);
exit(0);
}
// Upon execution this program will generate a file named server_socket in the same
// directory and its file type is s (socket).
A Simple Local Client local_client.c
 The server program in this example can serve only one client at a time.
 It just reads a character from the client, increments it, and writes it back.
 When you run the server program, it creates a socket and waits for
connections.
 Now, when you run the client program, you are successful in connecting to the
server. Because the server socket exists, you can connect to it and
communicate with the server.
How it works
Socket Attributes
Socket
Attributes
Socket Domain
AF_UNIX: UNIX internal (file system sockets) AF_INET: ARPA Internet protocols (UNIX network sockets)
AF_ISO : ISO standard protocols AF_NS : Xerox Network Systems protocols
AF_IPX : Novell IPX protocol AF_APPLETALK: Appletalk DDS
Socket Type
SOCK_STREAM: A sequenced, reliable, connection-based two way byte stream provided by TCP
SOCK_DGRAM: Used to send messages of a fixed Size. no guarantee that the message will be delivered
or that messages won’t be reordered in the network.
Socket Protocols
PF_INET : IPv4 Internet protocols PF_INET6 : IPv6 Internet protocols
PF_ATMPVC: Access to raw ATM PVCs PF_APPLETALK: Appletalk
PF_AX25: Amateur radio AX.25 protocol PF_IPX: IPX - Novell protocols
 Sockets are characterized by three attributes: domain, type, and protocol.
They also have an address used as their name. The formats of the addresses
vary depending on the domain, also known as the protocol family. Each
protocol family can use one or more address families to define the address
format.
 Socket Domains: Domains specify the network medium that the socket
communication will use. The most common socket domain is AF_INET, which
refers to Internet networking that’s used on many Linux local area networks
and the Internet itself.
 The underlying protocol, Internet Protocol (IP), which only has one address
family, imposes a particular way of specifying computers on a network. This is
called the IP address.
Socket Attributes
 Socket Types: In networked domains Internet protocols provide two
communication mechanisms with distinct levels of service:
 streams and datagrams
Stream Sockets
 Stream sockets (in some ways similar to standard input/output streams)
provide a connection that is a sequenced and reliable two-way byte stream.
Thus, data sent is guaranteed not to be lost, duplicated, or reordered without
an indication that an error has occurred.
 Large messages are fragmented, transmitted, and reassembled. This is similar
to a file stream, which also accepts large amounts of data and splits it up for
writing to the low-level disk in smaller blocks. Stream sockets have predictable
behavior.
Socket Attributes
 Stream sockets, specified by the type SOCK_STREAM, are implemented in the
AF_INET domain by TCP/IP connections.
 They are also the usual type in the AF_UNIX domain. We concentrate on
SOCK_STREAM sockets in this chapter because they are more commonly used
in programming network applications.
 TCP/IP stands for Transmission Control Protocol/Internet Protocol. IP is the
low-level protocol for packets that provides routing through the network from
one computer to another.
 TCP provides sequencing, flow control, and retransmission to ensure that
large data transfers arrive with all of the data present and correct or with an
appropriate error condition reported.
Socket Attributes
Datagram Socket
 A datagram socket, specified by the type SOCK_DGRAM, doesn’t establish and
maintain a connection. There is also a limit on the size of a datagram that can
be sent. It’s transmitted as a single network message that may get lost,
duplicated, or arrive out of sequence—ahead of datagrams sent after it.
 Datagram sockets are implemented in the AF_INET domain by UDP/IP
connections and provide an unsequenced, unreliable service.
 However, they are relatively inexpensive in terms of resources, because
network connections need not be maintained.
 They’re fast because there is no associated connection setup time.
 Datagrams are useful for “single-shot” inquiries to information services, for
providing regular status information, or for performing low-priority logging.
Socket Attributes
 They have the advantage that the death of a server doesn’t unduly
inconvenience a client and would not require a client restart.
 Because datagram-based servers usually retain no connection information,
they can be stopped and restarted without disturbing their clients.
Socket Protocols
 Where the underlying transport mechanism allows for more than one protocol
to provide the requested socket type, you can select a specific protocol for a
socket.
 In this chapter, we concentrate on UNIX network and file system sockets,
which don’t require you to choose a protocol other than the default.
 Each socket domain requires its own address format. For an AF_UNIX socket,
the address is described by a structure, sockaddr_un, defined in the
Socket Attributes
 <sys/un.h> include file.
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[]; /* pathname */
};
 In the AF_INET domain, the address is specified using a structure called
sockaddr_in, defined in netinet/in.h, which contains at least these members:
struct sockaddr_in {
short int sin_family; /*AF_INET*/
unsigned short int sin_port; /*Port number*/
struct in_addr sin_addr; /*Internet address*/
};
Socket Attributes
 The IP address structure, in_addr, is defined as follows:
struct in_addr {
unsigned long int s_addr;
};
 The 4 bytes of an IP address has a single 32-bit value.
 An AF_INET socket is fully described by its domain, IP address, and
port number. From an application’s point of view, all sockets act like file
descriptors and are addressed by a unique integer value.
Socket Attributes
 To make a socket (as created by a call to socket) available for use by other
processes, a server program needs to give the socket a name.
 AF_UNIX sockets are associated with a file system pathname, as you saw in
the server1 example.
 AF_INET sockets are associated with an IP and port number.
#include <sys/socket.h>
int bind(int socket, const struct sockaddr * address, size_t
address_len);
 The bind system call assigns the address specified in the parameter, address,
to the unnamed socket associated with the file descriptor socket. The length
of the address structure is passed as address_len.
 The length and format of the address depend on the address family.
Naming a Socket
 A particular address structure pointer will need to be cast to the generic
address type (struct sockaddr *) in the call to bind.
 On successful completion, bind returns 0.
 If it fails, it returns -1 and sets errno.
Naming a Socket
 To accept incoming connections on a socket, a server program must create a
queue to store pending requests. It does this using the listen system call.
#include <sys/socket.h>
int listen(int socket, int backlog);
 A Linux system may limit the maximum number of pending connections that
may be held in a queue.
 Subject to this maximum, listen sets the queue length to backlog. Incoming
connections up to this queue length are held pending on the socket; further
connections will be refused and the client’s connection will fail.
 A value of 5 for backlog is very common.
 The listen function will return 0 on success or -1 on error.
Creating a Socket Queue
 Once a server program has created and named a socket, it can wait for
connections to be made to the socket by using the accept system call.
#include <sys/socket.h>
int accept(int socket, struct sockaddr * address, size_t *
address_len);
 The accept system call returns when a client program attempts to connect to
the socket specified by the parameter socket.
 The client is the first pending connection from that socket’s queue.
 The accept function creates a new socket to communicate with the client and
returns its descriptor. The new socket will have the same type as the server
listen socket.
Accepting Connections
 The socket must have previously been named by a call to bind and had a
connection queue allocated by listen.
 The address of the calling client will be placed in the sockaddr structure
pointed to by address.
 A null pointer may be used here if the client address isn’t of interest.
 The address_len parameter specifies the length of the client structure. If the
client address is longer than this value, it will be truncated.
 Before calling accept, address_len must be set to the expected address length.
On return, address_len will be set to the actual length of the calling client’s
address structure.
 If there are no connections pending on the socket’s queue, accept will block
(so that the program won’t continue) until a client does make connection.
Accepting Connections
 You can change this behavior by using the O_NONBLOCK flag on the socket file
descriptor, using the fcntl function in your code like this:
int flags = fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, O_NONBLOCK|flags);
 The accept function returns a new socket file descriptor when there is a client
connection pending or -1 on error.
Accepting Connections
 Client programs connect to servers by establishing a connection between an
unnamed socket and the server listen socket. They do this by calling connect.
#include <sys/socket.h>
int connect(int socket, const struct sockaddr * server_ddress,
size_t address_len);
 The socket specified by the parameter socket is connected to the server socket
specified by the parameter server_address, which is of length address_len.
 The socket must be a valid file descriptor obtained by a call to socket.
 If the connect function succeeds, it returns 0, and -1 is returned on error.
 If the connection can’t be set up immediately, connect will block for an
unspecified timeout period. Once this timeout has expired, the connection will
be aborted and connect will fail.
Requesting Connection
 As with accept, the blocking nature of connect can be altered by setting the
O_NONBLOCK flag on the file descriptor. In this case, if the connection can’t
be made immediately, connect will fail with some errno.
Requesting Connection
 You can terminate a socket connection at the server and client by calling close,
just as you would for low-level file descriptors. You should always close the
socket at both ends.
 For the server, you should do this when read returns zero. Note that the close
call may block if the socket has un-transmitted data, is of a connection-
oriented type, and has the SOCK_LINGER option set.
Closing a Socket
 Now that we have covered the basic system calls associated with sockets, let’s
take a closer look at the example programs.
 You’ll try to convert them to use a network socket rather than a file system
socket.
 The file system socket has the disadvantage that, unless the author uses an
absolute pathname, it’s created in the server program’s current directory. To
make it more generally useful, you need to create it in a globally accessible
directory (such as /tmp) that is agreed between the server and its clients.
 For network sockets, you need only choose an unused port number. For the
example, select port number 9734. This is an arbitrary choice that avoids
the standard services
Socket Communication
 You can’t use port numbers below 1024 because they are reserved for system
use).
 Other port numbers are often listed, with the services provided on them, in
the system file /etc/services.
 When you’re When you’re writing socket-based applications, always choose a
port number not listed in this configuration file.
 You’ll run your client and server across a local network, but network sockets
are not only useful on a LAN; any machine with an Internet connection (even a
modem dial-up) can use network sockets to communicate with others.
 You can even use a network-based program on a stand-alone UNIX computer
because a UNIX computer is usually configured to use a loopback network that
contains only itself.
Socket Communication
 For illustration purposes, this example uses this loopback network, which can
also be useful for debugging network applications because it eliminates any
external network problems.
 The loopback network consists of a single computer, conventionally called
localhost, with a standard IP address of 127.0.0.1. This is the local machine.
You’ll find its address listed in the network hosts file, /etc/hosts, with the
names and addresses of other hosts on shared networks.
Socket Communication
Socket Communication TCP Server
192.168.10.10 : 8080
TCP Client
192.168.10.20
socket()
bind()
listen()
accept()
read()
write()
close()
socket()
connect()
write()
read()
close()
Client sends character ‘A’
Server sends character ‘B’
Client connects to
server IP address
192.168.10.10
Port 8080
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
// 2. Create an unnamed socket for the server:
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 3. Name the socket:
server_address.sin_family = AF_INET;
TCP Server Over 127.0.0.1 server1.c
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = 9734;
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
/* Create a connection queue and wait for clients. */
listen(server_sockfd, 5);
while(1) {
char ch;
printf("Server1 is waiting for Client1 at port %dn", server_address.sin_port);
client_len = sizeof(client_address); // Accept a connection.
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address,
&client_len);
printf("A Client from Address %x was connected n",
client_address.sin_addr.s_addr);
TCP Server Over 127.0.0.1 server1.c
// We can now read/write to client on client_sockfd.
read (client_sockfd, &ch, 1);
printf("Received From Client: %cn",ch);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
TCP Server Over 127.0.0.1 server1.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int sockfd; int len; int result;
struct sockaddr_in server_address; char ch = 'A';
// 1. Create a socket for the client:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 2. Name the socket, as agreed with the server:
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = 9734; len = sizeof(server_address);
TCP Client Over 127.0.0.1 client1.c
// 3.Now connect our socket to the server's socket.
result = connect(sockfd, (struct sockaddr *)&server_address, len);
if(result == -1) {
perror("Oops: Client1"); exit(1);
}
// 4.We can now read/write via sockfd.
write(sockfd, &ch, 1);
read (sockfd, &ch, 1);
printf("char from server = %cn", ch);
close(sockfd);
exit(0);
}
/* How It Works: The client program used the sockaddr_in structure from the include
file netinet/in.h to specify an AF_INET address. It tries to connect to a server
on the host with IP address 127.0.0.1. It uses a function, inet_addr, to convert
the text representation of an IP address into a form suitable for socket
addressing.*/
TCP Client Over 127.0.0.1 client1.c
TCP Server and Client Over 127.0.0.1
 The server program creates an AF_INET domain socket and arranges to accept
connections on it. The socket is bound to your chosen port.
 The address specified determines which computers are allowed to connect.
 By specifying the loopback address, as in the client program, you are
restricting communications to the local machine.
 If you want to allow the server to communicate with remote clients, you must
specify a set of IP addresses that you are willing to allow.
 You can use the special value, INADDR_ANY, to specify that you’ll accept
connections from all of the interfaces your computer may have.
 INADDR_ANY is a 32-bit integer value that you can use in the sin_addr.s_addr
field of the address structure.
TCP Server How It Works?
 As the port numbers and addresses are communicated over socket interfaces
as binary numbers.
 Different computers use different byte ordering for integers. For example,
 Intel processor stores the 32-bit integer as four consecutive bytes in
memory in the order 1-2-3-4, where 1 is the most significant byte.
 Motorola processors would store the integer in the byte order 4-3-2-1.
 If the memory used for integers were simply copied byte-by byte, the two
different computers would not be able to agree on integer values.
 The Solution is to convert their internal integer representation to the network
ordering before transmission.
 We do this by using functions defined in netinet/in.h.
Host and Network Byte Ordering
 These are
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);
unsigned short int htons(unsigned short int hostshort);
unsigned long int ntohl(unsigned long int netlong);
unsigned short int ntohs(unsigned short int netshort);
 These functions convert 16-bit and 32-bit integers between native host format
and the standard network ordering.
 Their names are abbreviations for conversions—for example, “host to
network, long” (htonl) and “host to network, short” (htons).
Host and Network Byte Ordering
 To ensure correct byte ordering of the 16-bit port number, your server and
client need to apply these functions to the port address.
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
 You don’t need to convert the function call, inet_addr("127.0.0.1"),
because inet_addr is defined to produce a result in network order.
 The server has also been changed to allow connections from any IP address by
using INADDR_ANY.
Host and Network Byte Ordering
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
char msg[1024], reply[]="Wa Elikum Salam";
if (argc !=2){
printf("Usege:%s Port_Number n",argv[0]);
exit(0);
}
Network Server server2.c
/* Remove any old socket and create an unnamed socket for the server. */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket. */
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(atoi(argv[1]));
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
/* Create a connection queue and wait for clients. */
listen(server_sockfd, 5);
Network Server server2.c
while(1) { char ch;
printf("Server2 is waiting at Port %d n", ntohs(server_address.sin_port));
/* Accept a connection. */
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address, &client_len);
printf("A Client was connected from IP Address: %sn",
inet_ntoa(client_address.sin_addr));
/*We can now read/write to client on client_sockfd.*/
read (client_sockfd, msg, sizeof(msg));
printf("Client Says:%s n",msg);
write(client_sockfd, reply, strlen(reply)+1);
close(client_sockfd);
}
}
Network Server server2.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int sockfd; int len; char msg[512] = "Assalam O Alikum";
struct sockaddr_in address; int result;
if (argc!=3){
printf("Usage:%s Server_IP_Address Port_Numbern",
argv[0]);
printf("Usage:%s 192.168.10.7 8080n",argv[0]);
exit(0);
}
Network Client client2.c
/* Create a socket for the client. */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(argv[1]);
address.sin_port = htons(atoi(argv[2]));
len = sizeof(address);
/* Now connect our socket to the server's socket. */
result = connect(sockfd,(struct sockaddr *)&address,len);
if(result == -1) {
perror("oops: client2");
exit(1);
}
Network Client client2.c
/* We can now read/write via sockfd. */
write(sockfd, msg, strlen(msg)+1);
read (sockfd, msg, sizeof(msg));
printf("Reply from server = %sn", msg);
close(sockfd);
exit(0);
}
Network Client client2.c
#!/usr/bin/env python3
import socket
HOST = '192.168.10.7' # The server's hostname or IP address
PORT = 8080 # The port used by the server 9734
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello from Pythonx00')
data = s.recv(1024)
print('Received', repr(data))
s.close()
Client in Python Client.py
Network Server and Client
 In socket programming we used addresses and port numbers compiled into
them. For a more general server and client program, you can use network
information functions to determine addresses and ports to use.
 If you have permission to do so, you can add your server to the list of known
services in /etc/services,
 which assigns a name to port numbers so that clients can use symbolic
services rather than numbers.
 Similarly, given a computer’s name, you can determine the IP address by
calling host database functions that resolve addresses for you. They do this by
consulting network configuration files, such as /etc/hosts, or DNS Server
(Domain Name Service).
 Host database functions are declared in the interface header file netdb.h.
Network Information
#include <netdb.h>
struct hostent *gethostbyaddr(const void *addr,size_t len, inttype);
struct hostent *gethostbyname(const char *name);
 The structure returned by these functions must contain at least these
members:
struct hostent {
char *h_name; /* name of the host */
char **h_aliases; /* list of nicknames */
int h_addrtype; /* address type */
int h_length; /* length in bytes of the address */
char **h_addr_list /*list of address(network order)*/
};
Network Information
 Similarly, information concerning services and associated port numbers is
available through some service information functions.
#include <netdb.h>
struct servent *getservbyname(const char *name, const char *proto);
struct servent *getservbyport(int port, const char *proto);
 The proto parameter specifies the protocol to be used to connect to the
service,
 either “tcp” for SOCK_STREAM TCP connections
 or “udp” for SOCK_DGRAM UDP datagrams.
Network Information
 The structure servent contains at least these members:
struct servent {
char *s_name; /* name of the service */
char **s_aliases; /* list of alternative names*/
int s_port; /* The IP port number */
char *s_proto; /* The service type “tcp” or “udp” */
};
 Similarly, information concerning services and associated port numbers is
available through some service information functions.
 You can gather host database information about a computer by calling
gethostbyname and printing the results.
 Note that the address list needs to be cast to the appropriate address type and
converted from network ordering to a printable string using the inet_ntoa
Network Information
 conversion, which has the following definition:
#include <arpa/inet.h>
char *inet_ntoa(struct in_addr in)
 The function converts an Internet host address to a string in dotted quad
format. It returns -1 on error, An other function you can use is gethostname.
#include <unistd.h>
int gethostname(char *name, int namelength);
 This function writes the name of the current host into the string given by
name. The hostname will be null-terminated.
 The argument namelength indicates the length of the string name, and the
returned hostname will be truncated if it’s too long to fit.
 gethostname returns 0 on success and -1 on error.
Network Information
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char *host, **names, **addrs;
struct hostent *hostinfo;
/* Set the host in question to the argument supplied with
the getname call, or default to the user's machine. */
if(argc == 1) {
char myname[256];
gethostname(myname, 255); host = myname;
}
else
host = argv[1];
Getting Network Information get_name.c
// Make the call to gethostbyname and report an error if no information is found.
hostinfo = gethostbyname(host);
if(!hostinfo) {
fprintf(stderr, "cannot get info for host: %sn", host);
exit(1);
}
/* Display the hostname and any aliases it may have. */
printf("results for host %s:n", host);
printf("Name: %sn", hostinfo -> h_name);
printf("Aliases:");
names = hostinfo -> h_aliases;
while(*names) {
printf(" %s", *names);
names++;
}
printf("n");
Getting Network Information get_name.c
/* Warn and exit if the host in question isn't an IP host. */
if(hostinfo -> h_addrtype != AF_INET) {
fprintf(stderr, "not an IP host!n");
exit(1);
}
/* Otherwise, display the IP address(es). */
addrs = hostinfo -> h_addr_list;
while(*addrs) {
printf("IP Address : %s", inet_ntoa(*(struct in_addr *)*addrs));
addrs++;
}
printf("n");
exit(0);
}
/* How It Works The getname program calls gethostbyname to extract the host
information from the host database. It prints out the hostname, its aliases
(other names the computer is known by), and the IP addresses that the host uses
on its network interfaces.*/
Getting Network Information get_name.c
Getting Network Information
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char *host; int sockfd; int len, result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[128];
if(argc == 1)
host = "localhost";
else
host = argv[1];
//Find the host address and report an error if none is found.
hostinfo = gethostbyname(host);
Connecting to a Standard Service get_date.c
if(!hostinfo) {
fprintf(stderr, "no host: %sn", host); exit(1);
}
// Check that the daytime service exists on the host.
servinfo = getservbyname("daytime", "tcp");
if(!servinfo) {
fprintf(stderr,"no daytime servicen");
exit(1);
}
printf("daytime port is %dn", ntohs(servinfo -> s_port));
sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Create a socket. */
/* Construct the address for use with connect... */
address.sin_family = AF_INET;
address.sin_port = servinfo -> s_port;
address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;
Connecting to a Standard Service get_date.c
len = sizeof(address);
// ...then connect and get the information.
result = connect(sockfd, (struct sockaddr *) &address, len);
if(result == -1) {
perror("oops: getdate"); exit(1);
}
result = read(sockfd, buffer, sizeof(buffer));
buffer[result] = '0';
printf("read %d bytes: %s", result, buffer);
close(sockfd);
exit(0);
}
Connecting to a Standard Service get_date.c
Connecting to a Standard Service
 When you run this program, you can specify a host to connect to. The daytime
service port number is determined from the network database function
getservbyname, which returns information about network services in a similar
way to host information.
 The program getdate tries to connect to the address given first in the list of
alternate addresses for the specified host.
 If successful, it reads the information returned by the daytime service, a
character string representing the UNIX time and date.
How it Works
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
A Server for Multiple Clients server_fork.c
bind(server_sockfd, (struct sockaddr *)& server_address, server_len);
/* Create a connection queue, ignore child exit details and wait for clients.*/
listen(server_sockfd, 5); signal(SIGCHLD, SIG_IGN);
while(1) {
char ch;
printf("server waitingn");
/* Accept connection. */
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address, &client_len);
// Fork to create a process for this client and perform a
// test to see whether we're the parent or the child.
A Server for Multiple Clients server_fork.c
if(fork() == 0) {
// If we're the child, we can now read/write to the client
// on client_sockfd. The five second delay is just for
// this demonstration.
read(client_sockfd, &ch, 1);
sleep(5);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd); exit(0);
}
// Otherwise, we must be the parent and our work for this
// client is finished.
else {
close(client_sockfd);
}
}
}
A Server for Multiple Clients server_fork.c
 The server program now creates a new child process to handle each client, so you see
several server waiting messages as the main program continues to wait for new
connections.
 The main server_fork process, is waiting for new clients while the three client2 processes
are being served by three children of the server.
 After a five-second pause, all of the clients get their results and finish.
 The server program uses fork to handle multiple clients.
How it Works
 The daytime service is also available by UDP using datagrams. To use it, you
send a single datagram to the service and get a single datagram containing the
date and time in response. It’s simple. Services provided by UDP are typically
used where a client needs to make a short query of a server and expects a
single short response.
 If the cost in terms of processing time is low enough, the server is able to
provide this service by dealing with requests from clients one at a time,
allowing the operating system to hold incoming requests in a queue. This
simplifies the coding of the server.
 Because UDP is not a guaranteed service, however, you may find that your
datagram or your response goes missing. So if the data is important to you,
you would need to code your UDP clients carefully to check for errors and
retry if necessary.
Datagrams
 In practice, on a local area network, UDP datagrams are very reliable. To access
a service provided by UDP, you need to use the socket and close system calls
as before, but rather than using read and write on the socket, you use two
datagram-specific system calls, sendto and recvfrom.
 Here’s a modified version of get_date.c that gets the date via a UDP datagram
service.
Datagrams
 The sendto system call sends a datagram from a buffer on a socket using a
socket address and address length. Its prototype is essentially
int sendto(int sockfd, void *buffer, size_t len, int flags, struct
sockaddr *to, socklen_t tolen);
 In normal use, the flags parameter can be kept zero.
 The recvfrom system call waits on a socket for a datagram from a specified
address and receives it into a buffer. Its prototype is essentially
int recvfrom(int sockfd, void *buffer, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);
 Again, in normal use, the flags parameter can be kept zero.
Using Sendto and Recvfrom
UDP Server
192.168.10.10 : 8080
UDP Client
192.168.10.20
socket()
bind()
recvfrom()
sendto()
close()
gethostbyname()
sendto()
recvfrom()
close()
Data(reply)
Blocks Until
Data Received
bind()
socket()
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
static char buf[BUFSIZ]; // Buffer for messages
UDP Server udp_server.c
int main(){
int sock, n; socklen_t server_len, client_len;
struct sockaddr_in server, client;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0){
perror("SERVER socket ");
return 1;
}
memset(&server, 0, sizeof(server)); // Clear structure
server.sin_family = AF_INET; // Set address type
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(8080);
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("SERVER bind ");
return 2;
}
server_len = sizeof(server); // Obtain address length
UDP Server udp_server.c
socket()
bind()
printf("Server is using port %d n", ntohs(server.sin_port));
while (1)
{ // Loop forever
client_len = sizeof(client); //set the length
memset(buf, 0, BUFSIZ); // clear the buffer
if ((n = recvfrom(sock, buf, BUFSIZ, 0,
(struct sockaddr *)&client, &client_len)) < 0)
{
perror("SERVER recvfrom ");
close(sock);
return 4;
}
write(fileno(stdout), buf, n);
memset(buf, '0', BUFSIZ); // clear the buffer
UDP Server udp_server.c
recvfrom()
if (read(fileno(stdin), buf, BUFSIZ) != 0)
{
// get server's msg
if ((sendto(sock, buf, strlen(buf), 0,
// send to client
(struct sockaddr *)&client, client_len)) < 0)
{
perror("SERVER sendto ");
close(sock);
return 5;
}
}
}
return 0;
}
UDP Server udp_server.c
sendto()
close()
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
static char buf[BUFSIZ]; // Buffer for messages
UDP Client udp_client.c
int main(int argc, char *argv[]) {
int sock, n;
socklen_t server_len;
struct sockaddr_in server, client;
struct hostent *host; // For host information
if ( argc < 3 ){ // We need server name & port #
printf("usage: %s server_name port_no n",argv[0]);
return 1;
} // Server information
if (!(host=gethostbyname(argv[1]))){
perror("CLIENT gethostname ");
return 2;
} // Set server address info
memset(&server, 0, sizeof(server)); // Clear structure
server.sin_family = AF_INET; // Address type
memcpy(&server.sin_addr, host->h_addr,host->h_length);
server.sin_port = htons(atoi(argv[2]));
UDP Client udp_client.c
gethostbyname()
if ((sock=socket(PF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("CLIENT socket ");
return 3;
}
// Set client address info
memset(&client,0,sizeof(client)); // Clear structure
client.sin_family = AF_INET; // Address type
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons( 0 );
if (bind(sock, (struct sockaddr *) &client,sizeof(client)) < 0) {
perror("CLIENT bind "); return 4;
}
printf("Client must send first message. n");
while( read(fileno(stdin), buf, BUFSIZ) != 0 ){
// get client's msg
server_len=sizeof(server); // length of address
UDP Client udp_client.c
socket()
bind()
if (sendto( sock, buf, strlen(buf), 0,
(struct sockaddr *) &server, server_len) < 0 ){
perror("CLIENT sendto "); close(sock); return 5;
}
memset(buf,0,BUFSIZ); // clear the buffer
// get server's msg
if ((n=recvfrom(sock, buf, BUFSIZ, 0,
(struct sockaddr *) &server, &server_len)) < 0){
perror("CLIENT recvfrom "); close(sock); return 6;
}
// display msg on client
write( fileno(stdout), buf, n );
memset(buf,0,BUFSIZ); // clear the buffer
}
close(sock);
return 0;
}
UDP Client udp_client.c
sendto()
recvfrom()
close()
Using Sendto and Recvfrom
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#define UDP_PORT 13
#define BUF_SIZE 1024
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[BUF_SIZE];
time_t current_time;
UDP Time Server time_server_udp.c
// Create a UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Configure server address
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(UDP_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
// Bind socket to server address
if(bind(sockfd,(const struct sockaddr *)&server_addr,sizeof(server_addr)) ==-1) {
perror("Socket bind failed"); exit(EXIT_FAILURE);
}
printf("UDP Time Server started on port %dn", UDP_PORT);
UDP Time Server time_server_udp.c
while (1) {
socklen_t addr_len = sizeof(client_addr);
// Receive data from client
int bytes_received = recvfrom(sockfd, (char *)buffer, BUF_SIZE, 0,
(struct sockaddr *)&client_addr, &addr_len);
if (bytes_received == -1) {
perror("Receive failed"); exit(EXIT_FAILURE);
}
time(&current_time); // Get current time
char *time_str = ctime(&current_time); // Convert current time to string
// Send time string to client
if (sendto(sockfd, time_str, strlen(time_str), 0,
(const struct sockaddr *)&client_addr, addr_len) == -1) {
perror("Send failed"); exit(EXIT_FAILURE);
}
printf("Time sent to client %s:%dn", inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
} close(sockfd); return 0; }
UDP Time Server time_server_udp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1" // Change to the IP address of your UDP time server
#define SERVER_PORT 13
int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[1024];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {// Create a UDP socket
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
UDP Time Client get_date_udp.c
memset(&server_addr, 0, sizeof(server_addr)); // Configure server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_aton(SERVER_IP, &server_addr.sin_addr) == 0) {
perror("Invalid IP address"); exit(EXIT_FAILURE);
}
// Send request to server
if (sendto(sockfd, "Request", strlen("Request"), 0,
(const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Sendto failed"); exit(EXIT_FAILURE);
}
// Receive response from server
int bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL, NULL);
if (bytes_received == -1) {
perror("Receive failed"); exit(EXIT_FAILURE);
}
UDP Time Client get_date_udp.c
// Null-terminate the received data to print it as a string
buffer[bytes_received] = '0';
// Print the received time from the server
printf("Received time from server: %sn", buffer);
// Close the socket
close(sockfd);
return 0;
}
UDP Time Client get_date_udp.c
Using UDP Time Server and Client

More Related Content

Similar to Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket Programming

Networking
NetworkingNetworking
Networking
Rashmi
 
Week6 final
Week6 finalWeek6 final
Week6 final
Irfan Ali Memon
 
Lecture 2 -_understanding_networks_with_presenter_notes
Lecture 2 -_understanding_networks_with_presenter_notesLecture 2 -_understanding_networks_with_presenter_notes
Lecture 2 -_understanding_networks_with_presenter_notes
Serious_SamSoul
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
Rakesh Madugula
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
RaviRajput416403
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
RaviRajput416403
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
RaviRajput416403
 
Design an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing SoftwareDesign an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing Software
nilabarai
 
Ip Addressing Basics
Ip Addressing BasicsIp Addressing Basics
Ip Addressing Basics
tmavroidis
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
RoshniSundrani
 
Session 2 Tp 2
Session 2 Tp 2Session 2 Tp 2
Session 2 Tp 2
githe26200
 
Final networks lab manual
Final networks lab manualFinal networks lab manual
Final networks lab manual
Jaya Prasanna
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
iDhawalVaja
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
arnold 7490
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
Python networking
Python networkingPython networking

Similar to Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket Programming (20)

Networking
NetworkingNetworking
Networking
 
Week6 final
Week6 finalWeek6 final
Week6 final
 
Lecture 2 -_understanding_networks_with_presenter_notes
Lecture 2 -_understanding_networks_with_presenter_notesLecture 2 -_understanding_networks_with_presenter_notes
Lecture 2 -_understanding_networks_with_presenter_notes
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
 
Design an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing SoftwareDesign an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing Software
 
Ip Addressing Basics
Ip Addressing BasicsIp Addressing Basics
Ip Addressing Basics
 
Java Network Programming.pptx
Java Network Programming.pptxJava Network Programming.pptx
Java Network Programming.pptx
 
Session 2 Tp 2
Session 2 Tp 2Session 2 Tp 2
Session 2 Tp 2
 
Final networks lab manual
Final networks lab manualFinal networks lab manual
Final networks lab manual
 
Socket Programming by Rajkumar Buyya
Socket Programming by Rajkumar BuyyaSocket Programming by Rajkumar Buyya
Socket Programming by Rajkumar Buyya
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Python networking
Python networkingPython networking
Python networking
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 

Recently uploaded (20)

学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 

Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket Programming

  • 1. Linux System Programming Socket Programming Engr. Rashid Farid Chishti chishti@iiu.edu.pk https://youtube.com/rfchishti https://sites.google.com/site/chishti
  • 2.  A socket is a communication mechanism that allows client/server systems to be developed either locally, on a single machine, or across networks. e.g. A Web browser uses sockets to communicate with web server. Network Addresses  Every host on a network has, at a minimum, two unique addresses.  The first unique address is a 48-bit media access control (MAC) address that is assigned to its network interface card (NIC).  The manufacturer of the card assigns this address. e.g. 00:50:56:8A:01:0D  The MAC address is written in hexadecimal notation, broken into six 8-bit numbers. The first three groupings (bytes) of this number identify the hardware manufacturer. Introduction to Sockets
  • 3.  Run following command to see the recently resolved addresses ( Internet addresses and their hardware address ) cat /proc/net/arp ifconfig -a // To see MAC and IP Address of your PC  The second unique address for a host is a 32-bit Internet Protocol (IP) address. e.g. 192.168.10.254  A 32-bit IP address is broken into four 8-bit numbers, each separated by a dot (.), each of the four sub-numbers can range from 0 to 255.  An IPv4 Internet address may be subdivided into a network and local portion. The network portion, or netid, occupies the leftmost portion of the IP address, and the local portion, or hostid, the rightmost portion.  Using the leading bits of the netid value, networks can be divided into five classes. With IPv6, the 32-bit IP addresses will become 128-bit. Network Addresses
  • 4.  In a Class A network bit 0 is 0;  in a Class B the first two bits are 10  In a Class C the first 3 bits are 110.  Class D is used for multicasting.  Class E is reserved for experimental use.  Class E and F do not have host-addressing scheme. IP v4 Address
  • 5. IP Address: 192.168.10.200 = 1100 0000.1010 1000.0000 1010.1100 1000 Subnet Mask: 255.255.255.224/27 = 1111 1111.1111 1111.1111 1111.1110 0000  Subnet Mask is 27-bit: A 27-bit Subnet Mask means that the first 27 bits of the IP address are used for the network portion, leaving 32 - 27 = 5 bits for the host portion.  Number of Subnets: IP Address:192.168.10.200 = 1100 0000.1010 1000.0000 1010.1100 1000 Subnet Mask:255.255.255.224/27 = 1111 1111.1111 1111.1111 1111.1110 0000 & ------------------------------------------------------------------------ Network Address:192.168.10.192 = 1100 0000.1010 1000.0000 1010.1100 0000  Usable IP Range: 11000000.10101000.00001010.11100001 (192.168.10.193) to 11000000.10101000.00001010.11111110 (192.168.10.222)  Broadcast Address: 11000000.10101000.00000010.11111111 (192.168.10.223) IP v4 Address
  • 6.  Gateway Address: Typically, the gateway address is assigned to the first usable IP address in the subnet. So, in this case, the gateway address would be 192.168.10.193.  Hosts per Network: 25 – 2 = 32 – 2 = 30 hosts.  Run Following Command in Windows: ipconfig -all IP v4 Address Wireless LAN adapter Wi-Fi: Connection-specific DNS Suffix . : Home Description . . . . . . . . . . . : Intel(R) Dual Band Wireless-AC 7265 Physical Address. . . . . . . . . : 64-80-99-CD-D3-D1 IPv4 Address. . . . . . . . . . . : 192.168.10.4(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.10.1 DHCP Server . . . . . . . . . . . : 192.168.10.1 DNS Servers . . . . . . . . . . . : 1.1.1.1 8.8.8.8
  • 7.  We often map a dotted IP address into a more easily understood symbolic notation using the Domain Name System (DNS).  In this schema, all Internet addresses are divided into a set of high-level organizational and geographical domains.  Each organizational domain (top-level domain) has an identifying code , such as .com (commercial), .edu (education), and .gov (government).  Each geographical domain some times consists of a two-letter country code, such as .pk (Pakistan).  In pc1.dee.fet.iiu.edu.pk under the domain of pk there is an educational domain edu under that there is domain of iiu university under that there is sub domain of fet then dee under that there is a system called pc1 using IP Address 192.168.10.1. Domains and Networks
  • 8.  The last name in the sequence (the leftmost) is usually the name of the host.  A DNS Server using distributed DNS database information dynamically maps a domain-name reference to its corresponding Internet (IP) address.  In Linux you can use hostname, nslookup and dig commands for DNS queries. See the man pages for help. hostname // view hostname of your system  The following command show DNS records associated with the hostname nslookup www.google.com.pk // shows 172.217.19.195 is the IP address  Similarly you can use dig command dig www.google.com // shows 172.217.17.36 is the IP address Domains and Networks
  • 9.  When we create the socket, we specify its communication domain. The two types of socket communication domains are as follows:  UNIX domain: In this domain, when sockets are created, they have actual file (path) names.  These sockets can be used only with processes that reside on the same host.  Internet domain: In this domain, sockets allow unrelated processes on different hosts to communicate. Sockets
  • 10.  Sockets programming uses the layered model of packet communication  Using socket APIs we make a program of some application that runs at Application Layer.  The Transport layer provides the transport protocols.  TCP (Transmission Control Protocol) and UDP (User Datagram Protocol)  The Network Layer provides among other things routing over the Internet. This layer is occupied by the Internet Protocol, or IP.  Physical Layer driver provides the means to introduce packets onto the physical network. Layered Model of Networking
  • 11. Sockets Programming Paradigm  Hosts are identified by addresses, and for IP (Internet Protocols), these are called IP addresses. An IPv4 address (of the version 4 class) is defined as a 32-bit address.  This address is represented by four 8-bit values. A sample address can be illustrated as: 192.168.1.1 or 0xC0A80101  The first value shows the more popular form of IPv4 addresses, which is easily readable.  The second notation is simply the first address in hexadecimal format (32 bits wide).
  • 12.  The Protocol specifies the details of communication over the socket.  The two most common protocols used are the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). TCP is a stream-based reliable protocol, and UDP is a datagram (message)-based protocol that can be unreliable.  The Port is the endpoint for a given process (interface) for a protocol. This is the application’s interface to the Socket interface. Ports are unique on a host for a given protocol. Ports are commonly called “bound” when they are attached to a given socket.  Ports are numbers that are split basically into two ranges. Port numbers below 1024 are reserved for well-known services (called well-known addresses). Port numbers above 1024 are typically used by applications. Sockets Programming Paradigm
  • 13. Sockets Programming Paradigm Figure: Visualization of a Socket between two hosts.
  • 14. Client / Server Model  In most Sockets applications, there exists a server (responds to requests and provides responses) and a client (makes requests to the server).  Figure : Client/server symmetry in Sockets applications
  • 15.  The first step is the creation of a sockets (communication endpoint) by the socket call. both the server and client perform this step.  The server requires a bit more setup as part of registering a service to the host. The bind call binds an address and port to the server so that it’s known. If we choose the port, we know what it is.  Once we’ve bound our port, we call the listen function for the server. This makes the server accessible (puts it in the listen mode).  We establish our socket next, using connect at the client and accept at the server.  The connect call starts what’s known as the three-way handshake, with the purpose of setting up a connection between the client and server.  At the server, the accept call creates a new server-side client socket.  Once accept finishes, a new socket connection exists between the client and Client / Server Model
  • 16. server, and data flow can occur.  In the data transfer phase, we have an established socket for which communication can occur. Both the client and server can send and recv data asynchronously.  Finally, we can close the connection between the client and server using the close call. This can occur asynchronously, but upon one endpoint closing the socket, the other side will automatically receive an indication of the closure.  Let’s do an example of a very simple socket client program  local_client.c. It creates an unnamed socket and connects it to a server socket called local_server Client / Server Model
  • 17. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> int main() { int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_un server_address; struct sockaddr_un client_address; // 1. Remove any old sockets and create an unnamed socket unlink("server_socket"); // for the server: server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); server_address.sun_family = AF_UNIX; // 2. Name the socket: strcpy(server_address.sun_path, "server_socket"); server_len = sizeof(server_address); A Simple Local Server local_server.c
  • 18. bind(server_sockfd,(struct sockaddr*)&server_address, server_len); // 3. Create a connection queue and wait for clients: listen(server_sockfd, 5); while(1) { char ch; printf("server waitingn"); // 4. Accept a connection: client_len = sizeof(client_address); client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len); // 5. Read and write to client on client_sockfd: read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); } } A Simple Local Server local_server.c
  • 19. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> int main() { int sockfd; int len; struct sockaddr_un server_address; int result; char ch = 'A'; sockfd = socket(AF_UNIX, SOCK_STREAM, 0); // 1. Create a socket for the client // 2. Name the socket as agreed with the server: server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); len = sizeof(server_address); A Simple Local Client local_client.c Address Family AF_INET or AF_UNIX Socket Type SOCK_STREAM or SOCK_DGRAM Protocol 0 for protocol selected by OS
  • 20. // 3. Connect your socket to the server’s socket: result = connect(sockfd,(struct sockaddr*) & server_address, len); if(result == -1) { perror("Oops: local_client"); exit(1); } // 4. You can now read and write via sockfd: write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from local server = %cn", ch); close(sockfd); exit(0); } // Upon execution this program will generate a file named server_socket in the same // directory and its file type is s (socket). A Simple Local Client local_client.c
  • 21.
  • 22.  The server program in this example can serve only one client at a time.  It just reads a character from the client, increments it, and writes it back.  When you run the server program, it creates a socket and waits for connections.  Now, when you run the client program, you are successful in connecting to the server. Because the server socket exists, you can connect to it and communicate with the server. How it works
  • 23. Socket Attributes Socket Attributes Socket Domain AF_UNIX: UNIX internal (file system sockets) AF_INET: ARPA Internet protocols (UNIX network sockets) AF_ISO : ISO standard protocols AF_NS : Xerox Network Systems protocols AF_IPX : Novell IPX protocol AF_APPLETALK: Appletalk DDS Socket Type SOCK_STREAM: A sequenced, reliable, connection-based two way byte stream provided by TCP SOCK_DGRAM: Used to send messages of a fixed Size. no guarantee that the message will be delivered or that messages won’t be reordered in the network. Socket Protocols PF_INET : IPv4 Internet protocols PF_INET6 : IPv6 Internet protocols PF_ATMPVC: Access to raw ATM PVCs PF_APPLETALK: Appletalk PF_AX25: Amateur radio AX.25 protocol PF_IPX: IPX - Novell protocols
  • 24.  Sockets are characterized by three attributes: domain, type, and protocol. They also have an address used as their name. The formats of the addresses vary depending on the domain, also known as the protocol family. Each protocol family can use one or more address families to define the address format.  Socket Domains: Domains specify the network medium that the socket communication will use. The most common socket domain is AF_INET, which refers to Internet networking that’s used on many Linux local area networks and the Internet itself.  The underlying protocol, Internet Protocol (IP), which only has one address family, imposes a particular way of specifying computers on a network. This is called the IP address. Socket Attributes
  • 25.  Socket Types: In networked domains Internet protocols provide two communication mechanisms with distinct levels of service:  streams and datagrams Stream Sockets  Stream sockets (in some ways similar to standard input/output streams) provide a connection that is a sequenced and reliable two-way byte stream. Thus, data sent is guaranteed not to be lost, duplicated, or reordered without an indication that an error has occurred.  Large messages are fragmented, transmitted, and reassembled. This is similar to a file stream, which also accepts large amounts of data and splits it up for writing to the low-level disk in smaller blocks. Stream sockets have predictable behavior. Socket Attributes
  • 26.  Stream sockets, specified by the type SOCK_STREAM, are implemented in the AF_INET domain by TCP/IP connections.  They are also the usual type in the AF_UNIX domain. We concentrate on SOCK_STREAM sockets in this chapter because they are more commonly used in programming network applications.  TCP/IP stands for Transmission Control Protocol/Internet Protocol. IP is the low-level protocol for packets that provides routing through the network from one computer to another.  TCP provides sequencing, flow control, and retransmission to ensure that large data transfers arrive with all of the data present and correct or with an appropriate error condition reported. Socket Attributes
  • 27. Datagram Socket  A datagram socket, specified by the type SOCK_DGRAM, doesn’t establish and maintain a connection. There is also a limit on the size of a datagram that can be sent. It’s transmitted as a single network message that may get lost, duplicated, or arrive out of sequence—ahead of datagrams sent after it.  Datagram sockets are implemented in the AF_INET domain by UDP/IP connections and provide an unsequenced, unreliable service.  However, they are relatively inexpensive in terms of resources, because network connections need not be maintained.  They’re fast because there is no associated connection setup time.  Datagrams are useful for “single-shot” inquiries to information services, for providing regular status information, or for performing low-priority logging. Socket Attributes
  • 28.  They have the advantage that the death of a server doesn’t unduly inconvenience a client and would not require a client restart.  Because datagram-based servers usually retain no connection information, they can be stopped and restarted without disturbing their clients. Socket Protocols  Where the underlying transport mechanism allows for more than one protocol to provide the requested socket type, you can select a specific protocol for a socket.  In this chapter, we concentrate on UNIX network and file system sockets, which don’t require you to choose a protocol other than the default.  Each socket domain requires its own address format. For an AF_UNIX socket, the address is described by a structure, sockaddr_un, defined in the Socket Attributes
  • 29.  <sys/un.h> include file. struct sockaddr_un { sa_family_t sun_family; /* AF_UNIX */ char sun_path[]; /* pathname */ };  In the AF_INET domain, the address is specified using a structure called sockaddr_in, defined in netinet/in.h, which contains at least these members: struct sockaddr_in { short int sin_family; /*AF_INET*/ unsigned short int sin_port; /*Port number*/ struct in_addr sin_addr; /*Internet address*/ }; Socket Attributes
  • 30.  The IP address structure, in_addr, is defined as follows: struct in_addr { unsigned long int s_addr; };  The 4 bytes of an IP address has a single 32-bit value.  An AF_INET socket is fully described by its domain, IP address, and port number. From an application’s point of view, all sockets act like file descriptors and are addressed by a unique integer value. Socket Attributes
  • 31.  To make a socket (as created by a call to socket) available for use by other processes, a server program needs to give the socket a name.  AF_UNIX sockets are associated with a file system pathname, as you saw in the server1 example.  AF_INET sockets are associated with an IP and port number. #include <sys/socket.h> int bind(int socket, const struct sockaddr * address, size_t address_len);  The bind system call assigns the address specified in the parameter, address, to the unnamed socket associated with the file descriptor socket. The length of the address structure is passed as address_len.  The length and format of the address depend on the address family. Naming a Socket
  • 32.  A particular address structure pointer will need to be cast to the generic address type (struct sockaddr *) in the call to bind.  On successful completion, bind returns 0.  If it fails, it returns -1 and sets errno. Naming a Socket
  • 33.  To accept incoming connections on a socket, a server program must create a queue to store pending requests. It does this using the listen system call. #include <sys/socket.h> int listen(int socket, int backlog);  A Linux system may limit the maximum number of pending connections that may be held in a queue.  Subject to this maximum, listen sets the queue length to backlog. Incoming connections up to this queue length are held pending on the socket; further connections will be refused and the client’s connection will fail.  A value of 5 for backlog is very common.  The listen function will return 0 on success or -1 on error. Creating a Socket Queue
  • 34.  Once a server program has created and named a socket, it can wait for connections to be made to the socket by using the accept system call. #include <sys/socket.h> int accept(int socket, struct sockaddr * address, size_t * address_len);  The accept system call returns when a client program attempts to connect to the socket specified by the parameter socket.  The client is the first pending connection from that socket’s queue.  The accept function creates a new socket to communicate with the client and returns its descriptor. The new socket will have the same type as the server listen socket. Accepting Connections
  • 35.  The socket must have previously been named by a call to bind and had a connection queue allocated by listen.  The address of the calling client will be placed in the sockaddr structure pointed to by address.  A null pointer may be used here if the client address isn’t of interest.  The address_len parameter specifies the length of the client structure. If the client address is longer than this value, it will be truncated.  Before calling accept, address_len must be set to the expected address length. On return, address_len will be set to the actual length of the calling client’s address structure.  If there are no connections pending on the socket’s queue, accept will block (so that the program won’t continue) until a client does make connection. Accepting Connections
  • 36.  You can change this behavior by using the O_NONBLOCK flag on the socket file descriptor, using the fcntl function in your code like this: int flags = fcntl(socket, F_GETFL, 0); fcntl(socket, F_SETFL, O_NONBLOCK|flags);  The accept function returns a new socket file descriptor when there is a client connection pending or -1 on error. Accepting Connections
  • 37.  Client programs connect to servers by establishing a connection between an unnamed socket and the server listen socket. They do this by calling connect. #include <sys/socket.h> int connect(int socket, const struct sockaddr * server_ddress, size_t address_len);  The socket specified by the parameter socket is connected to the server socket specified by the parameter server_address, which is of length address_len.  The socket must be a valid file descriptor obtained by a call to socket.  If the connect function succeeds, it returns 0, and -1 is returned on error.  If the connection can’t be set up immediately, connect will block for an unspecified timeout period. Once this timeout has expired, the connection will be aborted and connect will fail. Requesting Connection
  • 38.  As with accept, the blocking nature of connect can be altered by setting the O_NONBLOCK flag on the file descriptor. In this case, if the connection can’t be made immediately, connect will fail with some errno. Requesting Connection
  • 39.  You can terminate a socket connection at the server and client by calling close, just as you would for low-level file descriptors. You should always close the socket at both ends.  For the server, you should do this when read returns zero. Note that the close call may block if the socket has un-transmitted data, is of a connection- oriented type, and has the SOCK_LINGER option set. Closing a Socket
  • 40.  Now that we have covered the basic system calls associated with sockets, let’s take a closer look at the example programs.  You’ll try to convert them to use a network socket rather than a file system socket.  The file system socket has the disadvantage that, unless the author uses an absolute pathname, it’s created in the server program’s current directory. To make it more generally useful, you need to create it in a globally accessible directory (such as /tmp) that is agreed between the server and its clients.  For network sockets, you need only choose an unused port number. For the example, select port number 9734. This is an arbitrary choice that avoids the standard services Socket Communication
  • 41.  You can’t use port numbers below 1024 because they are reserved for system use).  Other port numbers are often listed, with the services provided on them, in the system file /etc/services.  When you’re When you’re writing socket-based applications, always choose a port number not listed in this configuration file.  You’ll run your client and server across a local network, but network sockets are not only useful on a LAN; any machine with an Internet connection (even a modem dial-up) can use network sockets to communicate with others.  You can even use a network-based program on a stand-alone UNIX computer because a UNIX computer is usually configured to use a loopback network that contains only itself. Socket Communication
  • 42.  For illustration purposes, this example uses this loopback network, which can also be useful for debugging network applications because it eliminates any external network problems.  The loopback network consists of a single computer, conventionally called localhost, with a standard IP address of 127.0.0.1. This is the local machine. You’ll find its address listed in the network hosts file, /etc/hosts, with the names and addresses of other hosts on shared networks. Socket Communication
  • 43. Socket Communication TCP Server 192.168.10.10 : 8080 TCP Client 192.168.10.20 socket() bind() listen() accept() read() write() close() socket() connect() write() read() close() Client sends character ‘A’ Server sends character ‘B’ Client connects to server IP address 192.168.10.10 Port 8080
  • 44. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> int main(){ int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; // 2. Create an unnamed socket for the server: server_sockfd = socket(AF_INET, SOCK_STREAM, 0); // 3. Name the socket: server_address.sin_family = AF_INET; TCP Server Over 127.0.0.1 server1.c
  • 45. server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); server_address.sin_port = 9734; server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr*)&server_address, server_len); /* Create a connection queue and wait for clients. */ listen(server_sockfd, 5); while(1) { char ch; printf("Server1 is waiting for Client1 at port %dn", server_address.sin_port); client_len = sizeof(client_address); // Accept a connection. client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); printf("A Client from Address %x was connected n", client_address.sin_addr.s_addr); TCP Server Over 127.0.0.1 server1.c
  • 46. // We can now read/write to client on client_sockfd. read (client_sockfd, &ch, 1); printf("Received From Client: %cn",ch); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); } } TCP Server Over 127.0.0.1 server1.c
  • 47. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> int main(){ int sockfd; int len; int result; struct sockaddr_in server_address; char ch = 'A'; // 1. Create a socket for the client: sockfd = socket(AF_INET, SOCK_STREAM, 0); // 2. Name the socket, as agreed with the server: server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); server_address.sin_port = 9734; len = sizeof(server_address); TCP Client Over 127.0.0.1 client1.c
  • 48. // 3.Now connect our socket to the server's socket. result = connect(sockfd, (struct sockaddr *)&server_address, len); if(result == -1) { perror("Oops: Client1"); exit(1); } // 4.We can now read/write via sockfd. write(sockfd, &ch, 1); read (sockfd, &ch, 1); printf("char from server = %cn", ch); close(sockfd); exit(0); } /* How It Works: The client program used the sockaddr_in structure from the include file netinet/in.h to specify an AF_INET address. It tries to connect to a server on the host with IP address 127.0.0.1. It uses a function, inet_addr, to convert the text representation of an IP address into a form suitable for socket addressing.*/ TCP Client Over 127.0.0.1 client1.c
  • 49. TCP Server and Client Over 127.0.0.1
  • 50.  The server program creates an AF_INET domain socket and arranges to accept connections on it. The socket is bound to your chosen port.  The address specified determines which computers are allowed to connect.  By specifying the loopback address, as in the client program, you are restricting communications to the local machine.  If you want to allow the server to communicate with remote clients, you must specify a set of IP addresses that you are willing to allow.  You can use the special value, INADDR_ANY, to specify that you’ll accept connections from all of the interfaces your computer may have.  INADDR_ANY is a 32-bit integer value that you can use in the sin_addr.s_addr field of the address structure. TCP Server How It Works?
  • 51.  As the port numbers and addresses are communicated over socket interfaces as binary numbers.  Different computers use different byte ordering for integers. For example,  Intel processor stores the 32-bit integer as four consecutive bytes in memory in the order 1-2-3-4, where 1 is the most significant byte.  Motorola processors would store the integer in the byte order 4-3-2-1.  If the memory used for integers were simply copied byte-by byte, the two different computers would not be able to agree on integer values.  The Solution is to convert their internal integer representation to the network ordering before transmission.  We do this by using functions defined in netinet/in.h. Host and Network Byte Ordering
  • 52.  These are #include <netinet/in.h> unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigned long int netlong); unsigned short int ntohs(unsigned short int netshort);  These functions convert 16-bit and 32-bit integers between native host format and the standard network ordering.  Their names are abbreviations for conversions—for example, “host to network, long” (htonl) and “host to network, short” (htons). Host and Network Byte Ordering
  • 53.  To ensure correct byte ordering of the 16-bit port number, your server and client need to apply these functions to the port address. server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9734);  You don’t need to convert the function call, inet_addr("127.0.0.1"), because inet_addr is defined to produce a result in network order.  The server has also been changed to allow connections from any IP address by using INADDR_ANY. Host and Network Byte Ordering
  • 54. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; char msg[1024], reply[]="Wa Elikum Salam"; if (argc !=2){ printf("Usege:%s Port_Number n",argv[0]); exit(0); } Network Server server2.c
  • 55. /* Remove any old socket and create an unnamed socket for the server. */ server_sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Name the socket. */ server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(atoi(argv[1])); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /* Create a connection queue and wait for clients. */ listen(server_sockfd, 5); Network Server server2.c
  • 56. while(1) { char ch; printf("Server2 is waiting at Port %d n", ntohs(server_address.sin_port)); /* Accept a connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); printf("A Client was connected from IP Address: %sn", inet_ntoa(client_address.sin_addr)); /*We can now read/write to client on client_sockfd.*/ read (client_sockfd, msg, sizeof(msg)); printf("Client Says:%s n",msg); write(client_sockfd, reply, strlen(reply)+1); close(client_sockfd); } } Network Server server2.c
  • 57. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int sockfd; int len; char msg[512] = "Assalam O Alikum"; struct sockaddr_in address; int result; if (argc!=3){ printf("Usage:%s Server_IP_Address Port_Numbern", argv[0]); printf("Usage:%s 192.168.10.7 8080n",argv[0]); exit(0); } Network Client client2.c
  • 58. /* Create a socket for the client. */ sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Name the socket, as agreed with the server. */ address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr(argv[1]); address.sin_port = htons(atoi(argv[2])); len = sizeof(address); /* Now connect our socket to the server's socket. */ result = connect(sockfd,(struct sockaddr *)&address,len); if(result == -1) { perror("oops: client2"); exit(1); } Network Client client2.c
  • 59. /* We can now read/write via sockfd. */ write(sockfd, msg, strlen(msg)+1); read (sockfd, msg, sizeof(msg)); printf("Reply from server = %sn", msg); close(sockfd); exit(0); } Network Client client2.c
  • 60. #!/usr/bin/env python3 import socket HOST = '192.168.10.7' # The server's hostname or IP address PORT = 8080 # The port used by the server 9734 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello from Pythonx00') data = s.recv(1024) print('Received', repr(data)) s.close() Client in Python Client.py
  • 62.  In socket programming we used addresses and port numbers compiled into them. For a more general server and client program, you can use network information functions to determine addresses and ports to use.  If you have permission to do so, you can add your server to the list of known services in /etc/services,  which assigns a name to port numbers so that clients can use symbolic services rather than numbers.  Similarly, given a computer’s name, you can determine the IP address by calling host database functions that resolve addresses for you. They do this by consulting network configuration files, such as /etc/hosts, or DNS Server (Domain Name Service).  Host database functions are declared in the interface header file netdb.h. Network Information
  • 63. #include <netdb.h> struct hostent *gethostbyaddr(const void *addr,size_t len, inttype); struct hostent *gethostbyname(const char *name);  The structure returned by these functions must contain at least these members: struct hostent { char *h_name; /* name of the host */ char **h_aliases; /* list of nicknames */ int h_addrtype; /* address type */ int h_length; /* length in bytes of the address */ char **h_addr_list /*list of address(network order)*/ }; Network Information
  • 64.  Similarly, information concerning services and associated port numbers is available through some service information functions. #include <netdb.h> struct servent *getservbyname(const char *name, const char *proto); struct servent *getservbyport(int port, const char *proto);  The proto parameter specifies the protocol to be used to connect to the service,  either “tcp” for SOCK_STREAM TCP connections  or “udp” for SOCK_DGRAM UDP datagrams. Network Information
  • 65.  The structure servent contains at least these members: struct servent { char *s_name; /* name of the service */ char **s_aliases; /* list of alternative names*/ int s_port; /* The IP port number */ char *s_proto; /* The service type “tcp” or “udp” */ };  Similarly, information concerning services and associated port numbers is available through some service information functions.  You can gather host database information about a computer by calling gethostbyname and printing the results.  Note that the address list needs to be cast to the appropriate address type and converted from network ordering to a printable string using the inet_ntoa Network Information
  • 66.  conversion, which has the following definition: #include <arpa/inet.h> char *inet_ntoa(struct in_addr in)  The function converts an Internet host address to a string in dotted quad format. It returns -1 on error, An other function you can use is gethostname. #include <unistd.h> int gethostname(char *name, int namelength);  This function writes the name of the current host into the string given by name. The hostname will be null-terminated.  The argument namelength indicates the length of the string name, and the returned hostname will be truncated if it’s too long to fit.  gethostname returns 0 on success and -1 on error. Network Information
  • 67. #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ char *host, **names, **addrs; struct hostent *hostinfo; /* Set the host in question to the argument supplied with the getname call, or default to the user's machine. */ if(argc == 1) { char myname[256]; gethostname(myname, 255); host = myname; } else host = argv[1]; Getting Network Information get_name.c
  • 68. // Make the call to gethostbyname and report an error if no information is found. hostinfo = gethostbyname(host); if(!hostinfo) { fprintf(stderr, "cannot get info for host: %sn", host); exit(1); } /* Display the hostname and any aliases it may have. */ printf("results for host %s:n", host); printf("Name: %sn", hostinfo -> h_name); printf("Aliases:"); names = hostinfo -> h_aliases; while(*names) { printf(" %s", *names); names++; } printf("n"); Getting Network Information get_name.c
  • 69. /* Warn and exit if the host in question isn't an IP host. */ if(hostinfo -> h_addrtype != AF_INET) { fprintf(stderr, "not an IP host!n"); exit(1); } /* Otherwise, display the IP address(es). */ addrs = hostinfo -> h_addr_list; while(*addrs) { printf("IP Address : %s", inet_ntoa(*(struct in_addr *)*addrs)); addrs++; } printf("n"); exit(0); } /* How It Works The getname program calls gethostbyname to extract the host information from the host database. It prints out the hostname, its aliases (other names the computer is known by), and the IP addresses that the host uses on its network interfaces.*/ Getting Network Information get_name.c
  • 71. #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { char *host; int sockfd; int len, result; struct sockaddr_in address; struct hostent *hostinfo; struct servent *servinfo; char buffer[128]; if(argc == 1) host = "localhost"; else host = argv[1]; //Find the host address and report an error if none is found. hostinfo = gethostbyname(host); Connecting to a Standard Service get_date.c
  • 72. if(!hostinfo) { fprintf(stderr, "no host: %sn", host); exit(1); } // Check that the daytime service exists on the host. servinfo = getservbyname("daytime", "tcp"); if(!servinfo) { fprintf(stderr,"no daytime servicen"); exit(1); } printf("daytime port is %dn", ntohs(servinfo -> s_port)); sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Create a socket. */ /* Construct the address for use with connect... */ address.sin_family = AF_INET; address.sin_port = servinfo -> s_port; address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list; Connecting to a Standard Service get_date.c
  • 73. len = sizeof(address); // ...then connect and get the information. result = connect(sockfd, (struct sockaddr *) &address, len); if(result == -1) { perror("oops: getdate"); exit(1); } result = read(sockfd, buffer, sizeof(buffer)); buffer[result] = '0'; printf("read %d bytes: %s", result, buffer); close(sockfd); exit(0); } Connecting to a Standard Service get_date.c
  • 74. Connecting to a Standard Service
  • 75.  When you run this program, you can specify a host to connect to. The daytime service port number is determined from the network database function getservbyname, which returns information about network services in a similar way to host information.  The program getdate tries to connect to the address given first in the list of alternate addresses for the specified host.  If successful, it reads the information returned by the daytime service, a character string representing the UNIX time and date. How it Works
  • 76. #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> int main(){ int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9734); server_len = sizeof(server_address); A Server for Multiple Clients server_fork.c
  • 77. bind(server_sockfd, (struct sockaddr *)& server_address, server_len); /* Create a connection queue, ignore child exit details and wait for clients.*/ listen(server_sockfd, 5); signal(SIGCHLD, SIG_IGN); while(1) { char ch; printf("server waitingn"); /* Accept connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); // Fork to create a process for this client and perform a // test to see whether we're the parent or the child. A Server for Multiple Clients server_fork.c
  • 78. if(fork() == 0) { // If we're the child, we can now read/write to the client // on client_sockfd. The five second delay is just for // this demonstration. read(client_sockfd, &ch, 1); sleep(5); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); exit(0); } // Otherwise, we must be the parent and our work for this // client is finished. else { close(client_sockfd); } } } A Server for Multiple Clients server_fork.c
  • 79.  The server program now creates a new child process to handle each client, so you see several server waiting messages as the main program continues to wait for new connections.  The main server_fork process, is waiting for new clients while the three client2 processes are being served by three children of the server.  After a five-second pause, all of the clients get their results and finish.  The server program uses fork to handle multiple clients. How it Works
  • 80.  The daytime service is also available by UDP using datagrams. To use it, you send a single datagram to the service and get a single datagram containing the date and time in response. It’s simple. Services provided by UDP are typically used where a client needs to make a short query of a server and expects a single short response.  If the cost in terms of processing time is low enough, the server is able to provide this service by dealing with requests from clients one at a time, allowing the operating system to hold incoming requests in a queue. This simplifies the coding of the server.  Because UDP is not a guaranteed service, however, you may find that your datagram or your response goes missing. So if the data is important to you, you would need to code your UDP clients carefully to check for errors and retry if necessary. Datagrams
  • 81.  In practice, on a local area network, UDP datagrams are very reliable. To access a service provided by UDP, you need to use the socket and close system calls as before, but rather than using read and write on the socket, you use two datagram-specific system calls, sendto and recvfrom.  Here’s a modified version of get_date.c that gets the date via a UDP datagram service. Datagrams
  • 82.  The sendto system call sends a datagram from a buffer on a socket using a socket address and address length. Its prototype is essentially int sendto(int sockfd, void *buffer, size_t len, int flags, struct sockaddr *to, socklen_t tolen);  In normal use, the flags parameter can be kept zero.  The recvfrom system call waits on a socket for a datagram from a specified address and receives it into a buffer. Its prototype is essentially int recvfrom(int sockfd, void *buffer, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);  Again, in normal use, the flags parameter can be kept zero. Using Sendto and Recvfrom
  • 83. UDP Server 192.168.10.10 : 8080 UDP Client 192.168.10.20 socket() bind() recvfrom() sendto() close() gethostbyname() sendto() recvfrom() close() Data(reply) Blocks Until Data Received bind() socket()
  • 84. #include <sys/ioctl.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <wait.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <netdb.h> static char buf[BUFSIZ]; // Buffer for messages UDP Server udp_server.c
  • 85. int main(){ int sock, n; socklen_t server_len, client_len; struct sockaddr_in server, client; if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0){ perror("SERVER socket "); return 1; } memset(&server, 0, sizeof(server)); // Clear structure server.sin_family = AF_INET; // Set address type server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(8080); if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("SERVER bind "); return 2; } server_len = sizeof(server); // Obtain address length UDP Server udp_server.c socket() bind()
  • 86. printf("Server is using port %d n", ntohs(server.sin_port)); while (1) { // Loop forever client_len = sizeof(client); //set the length memset(buf, 0, BUFSIZ); // clear the buffer if ((n = recvfrom(sock, buf, BUFSIZ, 0, (struct sockaddr *)&client, &client_len)) < 0) { perror("SERVER recvfrom "); close(sock); return 4; } write(fileno(stdout), buf, n); memset(buf, '0', BUFSIZ); // clear the buffer UDP Server udp_server.c recvfrom()
  • 87. if (read(fileno(stdin), buf, BUFSIZ) != 0) { // get server's msg if ((sendto(sock, buf, strlen(buf), 0, // send to client (struct sockaddr *)&client, client_len)) < 0) { perror("SERVER sendto "); close(sock); return 5; } } } return 0; } UDP Server udp_server.c sendto() close()
  • 88. #include <stdio.h> #include <ctype.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <wait.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <netdb.h> static char buf[BUFSIZ]; // Buffer for messages UDP Client udp_client.c
  • 89. int main(int argc, char *argv[]) { int sock, n; socklen_t server_len; struct sockaddr_in server, client; struct hostent *host; // For host information if ( argc < 3 ){ // We need server name & port # printf("usage: %s server_name port_no n",argv[0]); return 1; } // Server information if (!(host=gethostbyname(argv[1]))){ perror("CLIENT gethostname "); return 2; } // Set server address info memset(&server, 0, sizeof(server)); // Clear structure server.sin_family = AF_INET; // Address type memcpy(&server.sin_addr, host->h_addr,host->h_length); server.sin_port = htons(atoi(argv[2])); UDP Client udp_client.c gethostbyname()
  • 90. if ((sock=socket(PF_INET, SOCK_DGRAM, 0)) < 0 ) { perror("CLIENT socket "); return 3; } // Set client address info memset(&client,0,sizeof(client)); // Clear structure client.sin_family = AF_INET; // Address type client.sin_addr.s_addr = htonl(INADDR_ANY); client.sin_port = htons( 0 ); if (bind(sock, (struct sockaddr *) &client,sizeof(client)) < 0) { perror("CLIENT bind "); return 4; } printf("Client must send first message. n"); while( read(fileno(stdin), buf, BUFSIZ) != 0 ){ // get client's msg server_len=sizeof(server); // length of address UDP Client udp_client.c socket() bind()
  • 91. if (sendto( sock, buf, strlen(buf), 0, (struct sockaddr *) &server, server_len) < 0 ){ perror("CLIENT sendto "); close(sock); return 5; } memset(buf,0,BUFSIZ); // clear the buffer // get server's msg if ((n=recvfrom(sock, buf, BUFSIZ, 0, (struct sockaddr *) &server, &server_len)) < 0){ perror("CLIENT recvfrom "); close(sock); return 6; } // display msg on client write( fileno(stdout), buf, n ); memset(buf,0,BUFSIZ); // clear the buffer } close(sock); return 0; } UDP Client udp_client.c sendto() recvfrom() close()
  • 92. Using Sendto and Recvfrom
  • 93. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <arpa/inet.h> #define UDP_PORT 13 #define BUF_SIZE 1024 int main() { int sockfd; struct sockaddr_in server_addr, client_addr; char buffer[BUF_SIZE]; time_t current_time; UDP Time Server time_server_udp.c
  • 94. // Create a UDP socket if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Socket creation failed"); exit(EXIT_FAILURE); } // Configure server address memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(UDP_PORT); server_addr.sin_addr.s_addr = INADDR_ANY; // Bind socket to server address if(bind(sockfd,(const struct sockaddr *)&server_addr,sizeof(server_addr)) ==-1) { perror("Socket bind failed"); exit(EXIT_FAILURE); } printf("UDP Time Server started on port %dn", UDP_PORT); UDP Time Server time_server_udp.c
  • 95. while (1) { socklen_t addr_len = sizeof(client_addr); // Receive data from client int bytes_received = recvfrom(sockfd, (char *)buffer, BUF_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len); if (bytes_received == -1) { perror("Receive failed"); exit(EXIT_FAILURE); } time(&current_time); // Get current time char *time_str = ctime(&current_time); // Convert current time to string // Send time string to client if (sendto(sockfd, time_str, strlen(time_str), 0, (const struct sockaddr *)&client_addr, addr_len) == -1) { perror("Send failed"); exit(EXIT_FAILURE); } printf("Time sent to client %s:%dn", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); } close(sockfd); return 0; } UDP Time Server time_server_udp.c
  • 96. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define SERVER_IP "127.0.0.1" // Change to the IP address of your UDP time server #define SERVER_PORT 13 int main() { int sockfd; struct sockaddr_in server_addr; char buffer[1024]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {// Create a UDP socket perror("Socket creation failed"); exit(EXIT_FAILURE); } UDP Time Client get_date_udp.c
  • 97. memset(&server_addr, 0, sizeof(server_addr)); // Configure server address server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); if (inet_aton(SERVER_IP, &server_addr.sin_addr) == 0) { perror("Invalid IP address"); exit(EXIT_FAILURE); } // Send request to server if (sendto(sockfd, "Request", strlen("Request"), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { perror("Sendto failed"); exit(EXIT_FAILURE); } // Receive response from server int bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL, NULL); if (bytes_received == -1) { perror("Receive failed"); exit(EXIT_FAILURE); } UDP Time Client get_date_udp.c
  • 98. // Null-terminate the received data to print it as a string buffer[bytes_received] = '0'; // Print the received time from the server printf("Received time from server: %sn", buffer); // Close the socket close(sockfd); return 0; } UDP Time Client get_date_udp.c
  • 99. Using UDP Time Server and Client