CISCO - CCNA 200-120
These notes will be the basis for more detailed revision.
These "CCNA 200-120" Revision Notes consist of concise summaries or outlines of topics covered, lists of essential information needed.
ccna workbook and lab manual by NETWORKERS HOME. NETWORKERS HOME understand the importance of CCNA workbook when it comes Cisco certification which is why we offered free CCNA workbook.
This document provides an overview of client-server networking concepts in Java. It discusses elements like network basics, ports and sockets. It explains how to implement both TCP and UDP clients and servers in Java using socket classes. Sample code is provided for an echo client-server application using TCP and a quote client-server application using UDP. Exception handling for sockets is also demonstrated.
A socket represents a connection between two programs on a network and allows them to communicate. The document discusses sockets and how they enable client-server applications like a chat application. It provides code examples of how a chat client would connect to and send messages to a chat server, which would listen for incoming connections and send messages to connected clients. The key aspects are that the server binds to a port and listens for clients to connect, and then messages can be sent bidirectionally between the client and server over their socket connection.
The document provides an overview of networking basics in Java, including concepts like sockets, client-server communication, TCP and UDP protocols, HTTP protocol, domain name service (DNS), and classes used for networking in Java like InetAddress, Socket, ServerSocket, and URL. Key topics covered include how sockets work, the differences between TCP and UDP, how HTTP requests and responses are handled, and how DNS is used to map domain names to IP addresses. Example code is provided to demonstrate using InetAddress and Sockets in Java programs.
If the number of spine switches were to be merely doubled, the effect of a single switch failure is halved. With 8 spine switches, the effect of a single switch failure only causes a 12% reduction in available bandwidth. So, in modern data centers, people build networks with anywhere from 4 to 32 spine switches. With a leaf-spine network, every server on the network is exactly the same distance away from all other servers – three port hops, to be precise. The benefit of this architecture is that you can just add more spines and leaves as you expand the cluster and you don't have to do any recabling. Intuition Systems will also get more predictable latency between the nodes.
As a trend, disaggregation seems to be most useful for very large companies like Facebook and Google, or cloud providers. The technology does not necessarily have significant implications for small or medium sized businesses. Historically, however, technology has a way of trickling down from the pioneering phases of existing only within large companies with tremendous resources, to becoming more standardized across the board.
The document provides an overview of IT network design and installation topics covered in a MaxWiFi training course, including network models, IP addressing, NAT, routing, DHCP, VLANs, wireless networking, and Cisco device configuration.
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
CISCO - CCNA 200-120
These notes will be the basis for more detailed revision.
These "CCNA 200-120" Revision Notes consist of concise summaries or outlines of topics covered, lists of essential information needed.
ccna workbook and lab manual by NETWORKERS HOME. NETWORKERS HOME understand the importance of CCNA workbook when it comes Cisco certification which is why we offered free CCNA workbook.
This document provides an overview of client-server networking concepts in Java. It discusses elements like network basics, ports and sockets. It explains how to implement both TCP and UDP clients and servers in Java using socket classes. Sample code is provided for an echo client-server application using TCP and a quote client-server application using UDP. Exception handling for sockets is also demonstrated.
A socket represents a connection between two programs on a network and allows them to communicate. The document discusses sockets and how they enable client-server applications like a chat application. It provides code examples of how a chat client would connect to and send messages to a chat server, which would listen for incoming connections and send messages to connected clients. The key aspects are that the server binds to a port and listens for clients to connect, and then messages can be sent bidirectionally between the client and server over their socket connection.
The document provides an overview of networking basics in Java, including concepts like sockets, client-server communication, TCP and UDP protocols, HTTP protocol, domain name service (DNS), and classes used for networking in Java like InetAddress, Socket, ServerSocket, and URL. Key topics covered include how sockets work, the differences between TCP and UDP, how HTTP requests and responses are handled, and how DNS is used to map domain names to IP addresses. Example code is provided to demonstrate using InetAddress and Sockets in Java programs.
If the number of spine switches were to be merely doubled, the effect of a single switch failure is halved. With 8 spine switches, the effect of a single switch failure only causes a 12% reduction in available bandwidth. So, in modern data centers, people build networks with anywhere from 4 to 32 spine switches. With a leaf-spine network, every server on the network is exactly the same distance away from all other servers – three port hops, to be precise. The benefit of this architecture is that you can just add more spines and leaves as you expand the cluster and you don't have to do any recabling. Intuition Systems will also get more predictable latency between the nodes.
As a trend, disaggregation seems to be most useful for very large companies like Facebook and Google, or cloud providers. The technology does not necessarily have significant implications for small or medium sized businesses. Historically, however, technology has a way of trickling down from the pioneering phases of existing only within large companies with tremendous resources, to becoming more standardized across the board.
The document provides an overview of IT network design and installation topics covered in a MaxWiFi training course, including network models, IP addressing, NAT, routing, DHCP, VLANs, wireless networking, and Cisco device configuration.
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
The document provides information about Cisco Certified Network Associate certification and networking concepts like network types, topologies, devices, IP addressing, routing, and static route configuration. It includes definitions of LAN, WAN, bus, star, ring, mesh topologies and network devices like NIC, hub, switch, router. It also summarizes the OSI model layers, IP address classes, NAT, router components, modes, static and dynamic routing. The end includes a sample static routing configuration project.
This document discusses key concepts in communication networks including networking benefits, transmission media, network topologies, protocols, addressing, naming, and an example application. It describes how a student's request for a website is processed from the browser to the webserver by resolving hostnames to IP addresses using DNS, resolving IP addresses to MAC addresses using ARP, and using HTTP for client-server communication. The document introduces concepts to be explored in more depth in subsequent lectures.
This document provides an overview of key network terminology and concepts, including:
- Network components like network interface cards, hubs, switches, and cables.
- Network protocols and how they allow different devices to communicate.
- Operating systems' role in managing network resources and enabling sharing.
- The OSI model and what each layer is responsible for in data transmission.
- Common network addressing schemes and protocols like IP, TCP, UDP, and IPv6.
This document discusses Java networking and client/server communication. A client machine makes requests to a server machine over a network using protocols like TCP and UDP. TCP provides reliable data transmission while UDP sends independent data packets. Port numbers map incoming data to running processes. Sockets provide an interface for programming networks, with ServerSocket and Socket classes in Java. A server program listens on a port for client connections and exchanges data through input/output streams. Servlets extend web server functionality by executing Java programs in response to client requests.
This document provides an overview of networking concepts in Java including TCP/IP and UDP protocols, internet addressing, sockets, URLs, and how to implement client-server communication using TCP and UDP sockets. Key topics covered include the difference between TCP and UDP, how sockets connect applications to networks, internet addressing with IPv4 and IPv6, and examples of writing basic TCP and UDP client-server programs in Java.
The document outlines a 15 week semester plan for learning network programming. It includes 7 topics covered over the semester with associated tasks, estimated time commitments, and learning outcomes. The topics progress from an overview of networking concepts to implementing client-server applications and multithreaded network programming.
This document contains information about network programming laboratory experiments at the Global Institute of Technology. It includes the syllabus, list of experiments, course objectives, and content beyond the syllabus. The key experiments covered are:
1. Studying different network topologies, LAN equipment, and configurations
2. Writing client and server programs in C using TCP and UDP for tasks like echoing messages and estimating round trip time
3. Simulating routing algorithms like Bellman-Ford in NS2 networking simulation software
The objectives are for students to learn network programming concepts like the OSI model, TCP/IP, and socket programming to implement client-server applications over TCP and UDP.
This document provides information about network programming laboratory experiments at the Global Institute of Technology. It includes:
1. An index listing the syllabus, list of experiments, course objectives and outcomes, additional content, and experiments.
2. The syllabus lists 10 experiments covering topics like network topologies, TCP/IP programming, routing algorithms, and protocols.
3. The list of experiments provides more details on 13 experiments to be performed, covering network devices, client-server programming, chat servers, and simulation of protocols.
This document provides information about network programming and socket programming. It begins with an introduction to the client-server model and TCP connections. TCP connections provide reliable, connection-oriented communication with flow and congestion control. The document then discusses byte ordering issues that can occur when transferring multi-byte data types between systems with different architectures. It also covers IP addresses and port numbers, which are used to route and multiplex network traffic. Address structures and conversion functions are described to handle byte ordering when transferring addresses between networks.
Design an Implementation of A Messaging and Resource Sharing Softwarenilabarai
In this article it has been looked how to program using sockets by implementing an echo server along with a client that is used to send and receive string messages. It will start off by giving a quick introduction to TCP/IP fundamentals and then explain how sockets fit into the diagram. Most network application can be divided into two pieces: a client and a server. A client is the side that initiates the communication process, where as the server responds to incoming client requests. There would be contains three types of service from this software, that is Message Transfer service, Voice Transfer service and File Transfer service. All this sorts are the design issue of this software.
The document discusses IP addressing basics including MAC addresses, IP addresses, private and public IP addresses, and subnetting. It provides examples of MAC addresses, ifconfig output showing MAC and IP addresses, private IP address ranges, and how to break up a Class C network into multiple subnets using subnet masks.
This document discusses socket programming in Java. It focuses on TCP and UDP, network programming basics, identifying machines, servers and clients, ports and sockets, and data transfer using sockets. The java.net package allows Java programs to communicate over TCP or UDP networks. Sockets are endpoints for communication defined by an IP address and port. Server socket programs listen for client connections, while client programs initiate connections to servers. Data streams are used to read from and write to socket connections for communication between client and server programs.
The document discusses network design using TCP/IP. It covers IP addressing, subnet masks, default gateways, and subnetting. It also discusses network security methods like IP packet filtering, encryption, authentication, and IPSec. Optimizing the subnet design, IP performance, remote subnets, and quality of service can create an effective network infrastructure.
This document provides information about the Networks Laboratory course offered at Anjalai Ammal Mahalingam Engineering College. It includes the syllabus, list of experiments, objectives and outcomes of the course. The course aims to teach students socket programming, simulation tools, and hands-on experience with networking protocols. Some key experiments include implementing stop-and-wait and sliding window protocols, socket programming, simulating ARP/RARP, PING and traceroute, and studying routing algorithms. The course is intended to help students use simulation tools, implement protocols, and analyze network performance and routing.
This chapter discusses key concepts of network programming and socket-based communication between programs running on different computers. It introduces the java.net package and classes used for creating sockets and allowing message communication using TCP and UDP protocols. Example programs are provided to demonstrate how to create basic client-server applications using sockets in Java.
The document describes a LAN chat server that allows for chatting, voice chat, file transfer, and a bulletin board between clients on a local network. It uses sockets and networking concepts in Java to allow a server to communicate with multiple clients simultaneously through separate connections on the same port. The server forms sockets to clients, receives and forwards data between clients in loops, and uses streams to transfer various types of data between the client and server communication channels.
The document provides an overview of object oriented programming and network programming concepts. It discusses topics like IP addresses, ports, sockets, client-server programming, and the java.net and java.util packages. The java.net package contains classes for network programming in Java like Socket, ServerSocket, URL, and InetAddress. The java.util package contains general-purpose utility classes like ArrayList, HashMap, Properties and Date.
When we desire a communication between two applications possibly running on different machines, we need sockets. This presentation aims to provide knowledge of basic socket programming to undergraduate students. Basically, this presentation gives the importance of socket in the area of networking and Unix Programming. The presentation of Topic (Sockets) has designed according to the Network Programming Subject, B.Tech, 6th Semester syllabus of Punjab Technical University Kapurthala, Punjab.
This document provides an overview of network programming in Python. It discusses how Python allows both low-level socket access for building clients and servers as well as higher-level access to application protocols like FTP and HTTP. It then describes socket programming concepts like domains, types, protocols and functions for binding, listening, accepting, connecting, sending and receiving data. Simple client and server code examples are provided to demonstrate creating a basic socket connection between two programs. Finally, commonly used Python network modules for protocols like HTTP, FTP, SMTP and more are listed.
The document provides information about Cisco Certified Network Associate certification and networking concepts like network types, topologies, devices, IP addressing, routing, and static route configuration. It includes definitions of LAN, WAN, bus, star, ring, mesh topologies and network devices like NIC, hub, switch, router. It also summarizes the OSI model layers, IP address classes, NAT, router components, modes, static and dynamic routing. The end includes a sample static routing configuration project.
This document discusses key concepts in communication networks including networking benefits, transmission media, network topologies, protocols, addressing, naming, and an example application. It describes how a student's request for a website is processed from the browser to the webserver by resolving hostnames to IP addresses using DNS, resolving IP addresses to MAC addresses using ARP, and using HTTP for client-server communication. The document introduces concepts to be explored in more depth in subsequent lectures.
This document provides an overview of key network terminology and concepts, including:
- Network components like network interface cards, hubs, switches, and cables.
- Network protocols and how they allow different devices to communicate.
- Operating systems' role in managing network resources and enabling sharing.
- The OSI model and what each layer is responsible for in data transmission.
- Common network addressing schemes and protocols like IP, TCP, UDP, and IPv6.
This document discusses Java networking and client/server communication. A client machine makes requests to a server machine over a network using protocols like TCP and UDP. TCP provides reliable data transmission while UDP sends independent data packets. Port numbers map incoming data to running processes. Sockets provide an interface for programming networks, with ServerSocket and Socket classes in Java. A server program listens on a port for client connections and exchanges data through input/output streams. Servlets extend web server functionality by executing Java programs in response to client requests.
This document provides an overview of networking concepts in Java including TCP/IP and UDP protocols, internet addressing, sockets, URLs, and how to implement client-server communication using TCP and UDP sockets. Key topics covered include the difference between TCP and UDP, how sockets connect applications to networks, internet addressing with IPv4 and IPv6, and examples of writing basic TCP and UDP client-server programs in Java.
The document outlines a 15 week semester plan for learning network programming. It includes 7 topics covered over the semester with associated tasks, estimated time commitments, and learning outcomes. The topics progress from an overview of networking concepts to implementing client-server applications and multithreaded network programming.
This document contains information about network programming laboratory experiments at the Global Institute of Technology. It includes the syllabus, list of experiments, course objectives, and content beyond the syllabus. The key experiments covered are:
1. Studying different network topologies, LAN equipment, and configurations
2. Writing client and server programs in C using TCP and UDP for tasks like echoing messages and estimating round trip time
3. Simulating routing algorithms like Bellman-Ford in NS2 networking simulation software
The objectives are for students to learn network programming concepts like the OSI model, TCP/IP, and socket programming to implement client-server applications over TCP and UDP.
This document provides information about network programming laboratory experiments at the Global Institute of Technology. It includes:
1. An index listing the syllabus, list of experiments, course objectives and outcomes, additional content, and experiments.
2. The syllabus lists 10 experiments covering topics like network topologies, TCP/IP programming, routing algorithms, and protocols.
3. The list of experiments provides more details on 13 experiments to be performed, covering network devices, client-server programming, chat servers, and simulation of protocols.
This document provides information about network programming and socket programming. It begins with an introduction to the client-server model and TCP connections. TCP connections provide reliable, connection-oriented communication with flow and congestion control. The document then discusses byte ordering issues that can occur when transferring multi-byte data types between systems with different architectures. It also covers IP addresses and port numbers, which are used to route and multiplex network traffic. Address structures and conversion functions are described to handle byte ordering when transferring addresses between networks.
Design an Implementation of A Messaging and Resource Sharing Softwarenilabarai
In this article it has been looked how to program using sockets by implementing an echo server along with a client that is used to send and receive string messages. It will start off by giving a quick introduction to TCP/IP fundamentals and then explain how sockets fit into the diagram. Most network application can be divided into two pieces: a client and a server. A client is the side that initiates the communication process, where as the server responds to incoming client requests. There would be contains three types of service from this software, that is Message Transfer service, Voice Transfer service and File Transfer service. All this sorts are the design issue of this software.
The document discusses IP addressing basics including MAC addresses, IP addresses, private and public IP addresses, and subnetting. It provides examples of MAC addresses, ifconfig output showing MAC and IP addresses, private IP address ranges, and how to break up a Class C network into multiple subnets using subnet masks.
This document discusses socket programming in Java. It focuses on TCP and UDP, network programming basics, identifying machines, servers and clients, ports and sockets, and data transfer using sockets. The java.net package allows Java programs to communicate over TCP or UDP networks. Sockets are endpoints for communication defined by an IP address and port. Server socket programs listen for client connections, while client programs initiate connections to servers. Data streams are used to read from and write to socket connections for communication between client and server programs.
The document discusses network design using TCP/IP. It covers IP addressing, subnet masks, default gateways, and subnetting. It also discusses network security methods like IP packet filtering, encryption, authentication, and IPSec. Optimizing the subnet design, IP performance, remote subnets, and quality of service can create an effective network infrastructure.
This document provides information about the Networks Laboratory course offered at Anjalai Ammal Mahalingam Engineering College. It includes the syllabus, list of experiments, objectives and outcomes of the course. The course aims to teach students socket programming, simulation tools, and hands-on experience with networking protocols. Some key experiments include implementing stop-and-wait and sliding window protocols, socket programming, simulating ARP/RARP, PING and traceroute, and studying routing algorithms. The course is intended to help students use simulation tools, implement protocols, and analyze network performance and routing.
This chapter discusses key concepts of network programming and socket-based communication between programs running on different computers. It introduces the java.net package and classes used for creating sockets and allowing message communication using TCP and UDP protocols. Example programs are provided to demonstrate how to create basic client-server applications using sockets in Java.
The document describes a LAN chat server that allows for chatting, voice chat, file transfer, and a bulletin board between clients on a local network. It uses sockets and networking concepts in Java to allow a server to communicate with multiple clients simultaneously through separate connections on the same port. The server forms sockets to clients, receives and forwards data between clients in loops, and uses streams to transfer various types of data between the client and server communication channels.
The document provides an overview of object oriented programming and network programming concepts. It discusses topics like IP addresses, ports, sockets, client-server programming, and the java.net and java.util packages. The java.net package contains classes for network programming in Java like Socket, ServerSocket, URL, and InetAddress. The java.util package contains general-purpose utility classes like ArrayList, HashMap, Properties and Date.
When we desire a communication between two applications possibly running on different machines, we need sockets. This presentation aims to provide knowledge of basic socket programming to undergraduate students. Basically, this presentation gives the importance of socket in the area of networking and Unix Programming. The presentation of Topic (Sockets) has designed according to the Network Programming Subject, B.Tech, 6th Semester syllabus of Punjab Technical University Kapurthala, Punjab.
This document provides an overview of network programming in Python. It discusses how Python allows both low-level socket access for building clients and servers as well as higher-level access to application protocols like FTP and HTTP. It then describes socket programming concepts like domains, types, protocols and functions for binding, listening, accepting, connecting, sending and receiving data. Simple client and server code examples are provided to demonstrate creating a basic socket connection between two programs. Finally, commonly used Python network modules for protocols like HTTP, FTP, SMTP and more are listed.
Similar to Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket Programming (20)
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsVictor Morales
K8sGPT is a tool that analyzes and diagnoses Kubernetes clusters. This presentation was used to share the requirements and dependencies to deploy K8sGPT in a local environment.
Introduction- e - waste – definition - sources of e-waste– hazardous substances in e-waste - effects of e-waste on environment and human health- need for e-waste management– e-waste handling rules - waste minimization techniques for managing e-waste – recycling of e-waste - disposal treatment methods of e- waste – mechanism of extraction of precious metal from leaching solution-global Scenario of E-waste – E-waste in India- case studies.
Comparative analysis between traditional aquaponics and reconstructed aquapon...bijceesjournal
The aquaponic system of planting is a method that does not require soil usage. It is a method that only needs water, fish, lava rocks (a substitute for soil), and plants. Aquaponic systems are sustainable and environmentally friendly. Its use not only helps to plant in small spaces but also helps reduce artificial chemical use and minimizes excess water use, as aquaponics consumes 90% less water than soil-based gardening. The study applied a descriptive and experimental design to assess and compare conventional and reconstructed aquaponic methods for reproducing tomatoes. The researchers created an observation checklist to determine the significant factors of the study. The study aims to determine the significant difference between traditional aquaponics and reconstructed aquaponics systems propagating tomatoes in terms of height, weight, girth, and number of fruits. The reconstructed aquaponics system’s higher growth yield results in a much more nourished crop than the traditional aquaponics system. It is superior in its number of fruits, height, weight, and girth measurement. Moreover, the reconstructed aquaponics system is proven to eliminate all the hindrances present in the traditional aquaponics system, which are overcrowding of fish, algae growth, pest problems, contaminated water, and dead fish.
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...shadow0702a
This document serves as a comprehensive step-by-step guide on how to effectively use PyCharm for remote debugging of the Windows Subsystem for Linux (WSL) on a local Windows machine. It meticulously outlines several critical steps in the process, starting with the crucial task of enabling permissions, followed by the installation and configuration of WSL.
The guide then proceeds to explain how to set up the SSH service within the WSL environment, an integral part of the process. Alongside this, it also provides detailed instructions on how to modify the inbound rules of the Windows firewall to facilitate the process, ensuring that there are no connectivity issues that could potentially hinder the debugging process.
The document further emphasizes on the importance of checking the connection between the Windows and WSL environments, providing instructions on how to ensure that the connection is optimal and ready for remote debugging.
It also offers an in-depth guide on how to configure the WSL interpreter and files within the PyCharm environment. This is essential for ensuring that the debugging process is set up correctly and that the program can be run effectively within the WSL terminal.
Additionally, the document provides guidance on how to set up breakpoints for debugging, a fundamental aspect of the debugging process which allows the developer to stop the execution of their code at certain points and inspect their program at those stages.
Finally, the document concludes by providing a link to a reference blog. This blog offers additional information and guidance on configuring the remote Python interpreter in PyCharm, providing the reader with a well-rounded understanding of the process.
International Conference on NLP, Artificial Intelligence, Machine Learning an...gerogepatton
International Conference on NLP, Artificial Intelligence, Machine Learning and Applications (NLAIM 2024) offers a premier global platform for exchanging insights and findings in the theory, methodology, and applications of NLP, Artificial Intelligence, Machine Learning, and their applications. The conference seeks substantial contributions across all key domains of NLP, Artificial Intelligence, Machine Learning, and their practical applications, aiming to foster both theoretical advancements and real-world implementations. With a focus on facilitating collaboration between researchers and practitioners from academia and industry, the conference serves as a nexus for sharing the latest developments in the field.
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
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
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
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
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()
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()
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(¤t_time); // Get current time
char *time_str = ctime(¤t_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