Introduction
This Tutorial is On Socket Programming In C Language for Linux. Instructions Give Below will only work On Linux System not in windows.
Socket API In windows is called Winsock and there we can discuss about that in another tutorial.
What is Socket?
Sockets are a method for communication between a client program and a server program in a network.
A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API).
The most common sockets API is the Berkeley UNIX C interface for sockets.
Sockets can also be used for communication between processes within the same computer.
The overview on Socket Programming that will lead you how to program with a short example of programming which is in slide. With the help of this you can understand about network programming.
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.
The overview on Socket Programming that will lead you how to program with a short example of programming which is in slide. With the help of this you can understand about network programming.
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.
IP multicast is a method of sending Internet Protocol (IP) datagrams to a group of interested receivers in a single transmission. It is often employed for streaming media applications on the Internet and private networks.(wikipedia)
Webinar topic: Socket Programming with Python
Presenter: Achmad Mardiansyah
In this webinar series, Socket Programming with Python
Please share your feedback or webinar ideas here: http://bit.ly/glcfeedback
Check our schedule for future events: https://www.glcnetworks.com/en/schedule/
Follow our social media for updates: Facebook, Instagram, YouTube Channel, and telegram also discord
Recording available on Youtube
https://youtu.be/KtR4mIGnRNY
The Network Layer is concerned about getting packets from source to destination, no matter how many hops it may take. It’s all about routing.
5.1 Network Layer Design Issues
What do we need to think about in this layer?
5.2 Routing Algorithms
Strategies for getting from source to destination.
5.3 Congestion Control Algorithms
How do we keep from bottlenecking from too many packets?
5.4 Internetworking
Working with multiple networks and protocols in order to deliver packets.
5.5 The Network Layer in the Internet
Gluing together a collection of subnets.
IPv4 (Internet Protocol Version 4). This silde will give u all information about IPv4.
Hope so you like it Freinds.
and
Sorry if i can fulfill ur wish in the given IPv4 Presentation.
IP multicast is a method of sending Internet Protocol (IP) datagrams to a group of interested receivers in a single transmission. It is often employed for streaming media applications on the Internet and private networks.(wikipedia)
Webinar topic: Socket Programming with Python
Presenter: Achmad Mardiansyah
In this webinar series, Socket Programming with Python
Please share your feedback or webinar ideas here: http://bit.ly/glcfeedback
Check our schedule for future events: https://www.glcnetworks.com/en/schedule/
Follow our social media for updates: Facebook, Instagram, YouTube Channel, and telegram also discord
Recording available on Youtube
https://youtu.be/KtR4mIGnRNY
The Network Layer is concerned about getting packets from source to destination, no matter how many hops it may take. It’s all about routing.
5.1 Network Layer Design Issues
What do we need to think about in this layer?
5.2 Routing Algorithms
Strategies for getting from source to destination.
5.3 Congestion Control Algorithms
How do we keep from bottlenecking from too many packets?
5.4 Internetworking
Working with multiple networks and protocols in order to deliver packets.
5.5 The Network Layer in the Internet
Gluing together a collection of subnets.
IPv4 (Internet Protocol Version 4). This silde will give u all information about IPv4.
Hope so you like it Freinds.
and
Sorry if i can fulfill ur wish in the given IPv4 Presentation.
Data Link Layer Terms and Services such as frame, node, medium.
Creating a frame.
Data Link Layer sublayers: Logical Link Control and Media Access Control.
Media Access control methods for both of shared and non-sahred media.
Physical Topology: is an arrangement of the nodes and the physical connections between them.
Logical Topology: is the way a network transfers frames from one node to the next.
Data link layer addressing requirements.
Ethernet Protocol for LANs, Point-to-Point Protocol for WANs and Wireless Protocol for LANs.
Overview of UDP protocol.
UDP (User Datagram Protocol) is a simple extension of the Internet Protocol services. It basically provides simple packet transport service without any quality of service functions.
Unlike TCP, UDP is connection-less and packet-based. Application PDUs (application packets) sent over a UDP socket are delivered to the receiving host application as is without fragmentation.
UDP is mostly used by applications with simple request-response communication patterns like DNS, DHCP, RADIUS, RIP or RPC.
Since UDP does provide any error recovery such as retransmission of lost packets, the application protocols have to take care of these situations.
PHYSICAL COMPUTING WITH RGB LED OR MATRIX
Today we enter a topic in programming called
embedded computing with the internet; we code a
RGB LED light on a Arduino board with a breadboard
on which we switch off or on the light by a browser
on an android device with our own web server and
their COM or socket protocols too.
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
Network programming in java - PPT with Easy Programs and examples of Java InetAddress Class and java socket programming example.
Learn more @ http://java2all.com/technology/network-programming
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems.
The Presentation given at Guru Gobind Singh Polytechnic, Nashik for Third Year Information Technology and Computer Engineering Students on 08/02/2011.
Topic: Java Network Programming
Pushing the limits of ePRTC: 100ns holdover for 100 daysAdtran
At WSTS 2024, Alon Stern explored the topic of parametric holdover and explained how recent research findings can be implemented in real-world PNT networks to achieve 100 nanoseconds of accuracy for up to 100 days.
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
UiPath Test Automation using UiPath Test Suite series, part 5DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 5. In this session, we will cover CI/CD with devops.
Topics covered:
CI/CD with in UiPath
End-to-end overview of CI/CD pipeline with Azure devops
Speaker:
Lyndsey Byblow, Test Suite Sales Engineer @ UiPath, Inc.
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
20 Comprehensive Checklist of Designing and Developing a WebsitePixlogix Infotech
Dive into the world of Website Designing and Developing with Pixlogix! Looking to create a stunning online presence? Look no further! Our comprehensive checklist covers everything you need to know to craft a website that stands out. From user-friendly design to seamless functionality, we've got you covered. Don't miss out on this invaluable resource! Check out our checklist now at Pixlogix and start your journey towards a captivating online presence today.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
Building RAG with self-deployed Milvus vector database and Snowpark Container...Zilliz
This talk will give hands-on advice on building RAG applications with an open-source Milvus database deployed as a docker container. We will also introduce the integration of Milvus with Snowpark Container Services.
In the rapidly evolving landscape of technologies, XML continues to play a vital role in structuring, storing, and transporting data across diverse systems. The recent advancements in artificial intelligence (AI) present new methodologies for enhancing XML development workflows, introducing efficiency, automation, and intelligent capabilities. This presentation will outline the scope and perspective of utilizing AI in XML development. The potential benefits and the possible pitfalls will be highlighted, providing a balanced view of the subject.
We will explore the capabilities of AI in understanding XML markup languages and autonomously creating structured XML content. Additionally, we will examine the capacity of AI to enrich plain text with appropriate XML markup. Practical examples and methodological guidelines will be provided to elucidate how AI can be effectively prompted to interpret and generate accurate XML markup.
Further emphasis will be placed on the role of AI in developing XSLT, or schemas such as XSD and Schematron. We will address the techniques and strategies adopted to create prompts for generating code, explaining code, or refactoring the code, and the results achieved.
The discussion will extend to how AI can be used to transform XML content. In particular, the focus will be on the use of AI XPath extension functions in XSLT, Schematron, Schematron Quick Fixes, or for XML content refactoring.
The presentation aims to deliver a comprehensive overview of AI usage in XML development, providing attendees with the necessary knowledge to make informed decisions. Whether you’re at the early stages of adopting AI or considering integrating it in advanced XML development, this presentation will cover all levels of expertise.
By highlighting the potential advantages and challenges of integrating AI with XML development tools and languages, the presentation seeks to inspire thoughtful conversation around the future of XML development. We’ll not only delve into the technical aspects of AI-powered XML development but also discuss practical implications and possible future directions.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
GridMate - End to end testing is a critical piece to ensure quality and avoid...ThomasParaiso2
End to end testing is a critical piece to ensure quality and avoid regressions. In this session, we share our journey building an E2E testing pipeline for GridMate components (LWC and Aura) using Cypress, JSForce, FakerJS…
2. P a g e | 1
Introduction
This Tutorial is On Socket Programming In C Language for Linux. Instructions Give Below will only work
On Linux System not in windows.
Socket API In windows is called Winsock and there we can discuss about that in another tutorial.
What is Socket?
Sockets are a method for communication between a client program and a server program in a network.
A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of
programming requests or "function calls" sometimes called the sockets application programming
interface (API).
The most common sockets API is the Berkeley UNIX C interface for sockets.
Sockets can also be used for communication between processes within the same computer.
Forming Internet (IPv4) Socket Addresses
struct sockaddr_in {
sa_family_t sin_family; /* Address Family */
uint16_t sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
unsigned char sin_zero[8]; /* Pad bytes */
};
Struct sockaddr {
unsigned short sa_family /*Family Protocol*/
char sa_data[14]; /*actually longer; address value*/
};
3. P a g e | 2
/ * Structure of DNS entries * /
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
The members of the hostent structure are:
h_name - The official name of the host.
h_aliases - An array of alternative names for the host, terminated by a NULL pointer.
h_addrtype - The type of address; always AF_INET or AF_INET6 at present.
h_length - The length of the address in bytes.
h_addr_list - An array of pointers to network addresses for the host (in network byte order),
terminated by a NULL pointer.
h_addr - The first address in h_addr_list for backward compatibility.
Sockets are Virtual parts of each type of communication network made by the two hosts on a link.
For example, when you type www.google.com in your browser, it opens socket and then makes the
connection to google.com to compare with the website and then connects back to you.
The same thing happens in gtalk or skype. Any Communication networks wandering for socket
connection.
Socket API In Linux is similar to socket in BSD/UNIX. Although in Socket API is Different in Some Systems.
And now with New Standard is POSIX sockets API which is similar to the BSD sockets.
To understand this tutorial you should have little knowledge of language C and pointers.You must have
gcc compilers which are installed on your Linux system. An idea that together with gcc would be better. I
would recommend you Geany because you can edit and run a program in without much configuration.
In Ubuntu you can write in terminal apt-get install Geany. We all tutorial are codes that demonstrate
some concepts. You can guide these codes in Geany and immediately see the results for you better
understand concepts.
4. P a g e | 3
Create Socket
The first thing you should do is to create a socket. Socket function is according to the following code:
1 #include<stdio.h>
2 #include<sys/socket.h>
3
4 int main(int argc , char *argv[])
5 {
6 int socket_desc;
7 socket_desc = socket(AF_INET , SOCK_STREAM , 0);
8
9 if (socket_desc == -1)
10 {
11 printf("Cannot create a socket ");
12 }
13
14 return 0;
15}
Function socket () creates a socket and returns a narrative which can be used in other functions. Above
code will create a socket with these properties.
Address family - AF_INET (Only for version IPv4)
Type - SOCK_STREAM (This means that is the type of TCP socket)
Protocol - 0 [or IPPROTO_IP IP Protocol]
Then may need to connect to a server using socket. We can connect to www.google.com site
Note:-
Besides the type SOCK_STREAM is another type which is called SOCK_DGRAM which is linked
with the UDP protocol. This type of non-Socket called a socket connection. In this tutorial will
adhere socket type SOCK_STREAM or TCP.
5. P a g e | 4
Connecting socket server
We can connect to a remote server to a specific port. So we need two things, IP address and port
number to which will connect. To connect to a remote server we need to perform some action. The first
is to create a structure with clear values.
struct sockaddr_in server;
See the following code.
// IPv4 AF_INET sockets:
struct sockaddr_in {
short sin_family; // AF_INET, AF_INET6
unsigned short sin_port; // htons(3490)
struct in_addr sin_addr; // structure looks in_addr, below
char sin_zero[8]; // Make 0 if you want.
};
struct in_addr {
unsigned long s_addr; // Upload inet_pton ()
};
struct sockaddr {
unsigned short sa_family; // Family address, AF_xxx
char sa_data[14]; // 14 byte address the protocol
};
Sockaddr_in structure has a member called in_addr sin_addr type which has a s_addr of CLIA is nothing
but an important sentence.
It contains the IP addresses in a long format.
server.sin_addr.s_addr = inet_addr("74.125.235.20");
So you must know the IP address of the server is which will connect. Here we used the IP address of
google.com as an example.
6. P a g e | 5
Later in need to see how to find the IP address of a domain provided by the user or designated program.
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
// Creating Socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
}
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
//Lidhu ne nje remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("Error in connection ");
return 1;
}
puts("Connection top ");
return 0;
}
This cannot be made simpler than that. Create a Socket and then connects to the remote server. If you
start the application it must provide "Connected". You need to start the program with root privileges.
If gives error regarding then you do not have access to the Internet or have not started the program
with root privileges. To launch the program in the root you must enter: sudo ./a.
Try out to connect to a port other than 80 and thus you will not link you are provided which provides
that the port is not open for about
Ok, now we are connected. Let's do something else, to transmit some data in remote server. Links are
acceptable only TCP Socket. The concept of "connectivity" applies the type of socket -> SOCK_STREAM /
TCP. Connecting through this stream in is reliable.
Suppose this were a slip of not interrupted by other date.
The different Socket such as UDP, ICMP, ARP does not have this concept of connection.
It has non-binding based on communication. Which means you can cost date and receive packages from
anyone.
7. P a g e | 6
Transfer data over the network
The function send () will simplify working to transfer date, this will descriptors Socket, the data will
transfer and its size. Here you are given a very clear example for the transfer of certain data to a
google.com ip:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *message;
//Krijo soketin
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Connection can not be performed”);
}
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
// Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("Error in connection ");
return 1;
}
puts("I connected n ");
// By transferring some data message = "GET / HTTP/1.1rnrn";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
puts("Sending data failed ");
return 1;
}
puts("Data were sent n ");
return 0;
}
8. P a g e | 7
In the example above, we start to connect to an IP address and then send the message to type the string
"GET / HTTP / 1.1 r n r n" server.
This message actually a protocol http commands to test the main page of a site. Now that we have led
some data, it is time to get reply from the server. So let us make this program a devoted in this way.
Note:-
When you transfer data to a socket we are writing data in this String. This is similar to the type of data in
a file. So you can use the function write () to send data to a String I desired. Later in this tutorial we will
need to use the function write () to send data.
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *message , server_reply[2000];
//creating socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Cannot create socket ");
}
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
// Connecting to remote server socket
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("Link not top ");
return 1;
}
puts("The connection was made. n");
9. P a g e | 8
// Send some data
message = "GET / HTTP/1.1rnrn";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
puts("sending failed ");
return 1;
}
puts("Data were sent successfully n");
// Receiving a response from the server
if( recv(socket_desc, server_reply , 2000 , 0) < 0)
{
puts("Making failed ");
}
puts("Making finished successfully n");
puts(server_reply);
return 0;
}
Acquisition of data in Socket
Function recv () is used to receive data from the socket. In the example below we will need to send the
same message as in the example takes up and to get a response from the server
This is the output of the code above:
Connected
10. P a g e | 9
We can see that this response is sent from the server.
This format is displayed in the form HTML, because in reality it is sent HTML.Google.com site content
that we asked for. Very simple!
Note:-
When receiving data in a String, we read the data correctly if we are this String. This feature is similar to
the read data from a file. So we can use the function read () to read the data in a String. For example :-
1 read (socket_desc, server_reply, 2000);
Now that we got our answer, it is time to close the socket.
Closing the socket
The function close () is used to close a Socket. We have to include library <unistd.h> for this feature.
1 close (socket_desc);
11. P a g e | 10
Summary
In the example above we learned how to:
1. Create a Socket
2. To connect to a remote server
3. Send some data
4. Take Reply
Your browser thus acts exactly at the moment when you go to a webpage. This type of activity presents
Socket called client Socket. A client is an application which connects to the remote system to obtain the
desired data.
The other type is called server Socket. A server is a system which uses about socket to get authorized by
the application. It is the opposite of Client. So www.google.com is the server and the browser is the
client. Or in a more technical www.google.com is a HTTP server and the browser is the client HTTP
Now is the time to do some tasks using socket server. But before we go any further with some subjects
are affected in cases where you will be.
Displays the IP address of a given host
When you connect to a remote host, it is necessary to have its IP address.
#include<stdio.h> //printf
#include<string.h> //strcpy
#include<sys/socket.h>
#include<netdb.h> //hostent
#include<arpa/inet.h>
int main(int argc , char *argv[])
{
char *hostname = "www.google.com";
When you connect to a remote host, it is necessary to have its IP address. The function gethostbyname
() is used for this purpose.This function takes the domain name as a parameter and returns the goads
type structure. This structure IP.Kjo structure information included in library <netdb.h>.Let us take a
look at this structure. h_addr_list no ip addresses. So we now have some codes that you use these.
char ip[100];
struct hostent *he;
12. P a g e | 11
struct in_addr **addr_list;
int i;
if ( (he = gethostbyname( hostname ) ) == NULL)
{
//gethostbyname deshtuesi
herror("gethostbyname");
return 1;
}
//Hedh h_addr_list ne in_addr , qe kur h_addr_list ka gjithashtu
Adresat ip ne format te gjate.
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Kthe te parin
strcpy(ip , inet_ntoa(*addr_list[i]) );
}
printf("%s u shpetua ne IP : %s" , hostname , ip);
return 0;
}
Output of the code would look like :
www.google.com resolved to : 74.125.235.20
So the above code is used to find the IP address of a given domain. IP addresses can then be used to
perform the connection to a Socket I set.
Inet_ntoa function () will convert the IP address in the format long format with the item. This is the
opposite of inet_addr ().
13. P a g e | 12
The longest we have seen significant structures which are used. Let we see the following:
1. sockaddr_in - Information linkage. Were using the function connect (), send (), recv ().
2. in_addr - IP Address long form.
3. sockaddr
4. goads - IP address of a host. I used by gethostbyname.
In the later part we will see how to create using socket. Servers are opposite the client, instead of
this being connected with others, they wait for incoming connections
Servers Socket
Ok we are now things of the servers. Socket servers operate in a different way.
Create a Server
Relationship of a address (and port).
Listening for incoming connections.
The acceptance of connections
Read / Submit
We have learned how to open and to learn a Socket also to close it. So we must make the
connection with convinces function ().
Connecting socket to a port
Function convince () can be used to connect to a Soken in an "address and port" combination. This
should sockaddr_in structure with function connect ().
14. P a g e | 13
int socket_desc;
struct sockaddr_in server;
// Creating Socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
}
// Prepares sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Lidhja
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connection Problem ");
}
puts("Connection top ");
Now that the connection is done, it is time to do socket to listen incoming connections
(connections).
We connect the socket to an IP address and a specific port. By doing this we ensure that all the data
that are coming to head in this port being accepted by the relevant application to perform this
action.
This makes it clear that 2 socket cannot relate to the same port.
Listen for incoming connections in Socket
After connecting to a Socket in a port, next thing you should do is to listen for connections .For this
we need to use the socket in the way of listening. The function list () is used to insert socket in the
way listeners. Just add rows below after entering:
15. P a g e | 14
1 // Listen
2 listen (socket_desc, 3);
That is all. Now we come to the main part of accepting new connections.
Accept Connections
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
//Lidhja e soketit
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
}
// Prepares sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
// Relationship or Convict, blindness
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connection Problem ");
}
puts("Lidhja u krye");
//Degjo
listen(socket_desc , 3);
// Acceptance of incoming connections
puts("Waiting for connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("connection failed ");
}
puts("Binding was applied ");
return 0;
16. P a g e | 15
}
Accept function () is used to accept incoming connections.
Production of the Program
The launch of the program. This should be shown as follows.
Binding was applied
Waiting for incoming connections. . .
So far the program is waiting for incoming connections on port 8888. Do not close the program,
keep the charge. Now the client can be connected in this Socket at the port specified. Will need to
use telnet client to test this. Open Terminal and type
Terminal you should receive
And the server will show
Binding was applied
Waiting for incoming connections ...
Connection charge.
So we can now see that the client is connected to the server. Try the above process until you become
more perfect.
Get the IP address and connect with the client
You can get the IP address of a client in a connection from the port using sockaddr_in structure followed
by the function Accept (). Is very simple:
17. P a g e | 16
1char * client_ip = inet_ntoa (client.sin_addr);
2int client_port = ntohs (client.sin_port);
We accepted an incoming connection but were closed immediately. This is not productive. There are
many different things that can happen from that when the network connection is established. Although
the link is established for the purpose of communication. So let us return one client response:
In more simplicity can use the function write () to write something in a Socket for incoming connections
and the client will see it. Here is an example:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;
// Creating socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
}
// Prepares sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
// connection
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connection Problem ");
return 1;
}
18. P a g e | 17
puts("Connection top ");
// Listen
listen(socket_desc , 3);
// Accept incoming connections
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client,
(socklen_t*)&c);
if (new_socket<0)
{
perror("Application failed ");
return 1;
}
puts("Binding was applied ");
// Responding to the operating client
message = "Hello Clientn";
write(new_socket , message , strlen(message));
return 0;
}
Start the above code in a terminal. And then connect the kte server using telnet client from another
terminal and you should see.
So the client (telnet) received a reply from the server.
We can see that the connection was closed immediately after this message because the server is closed
after accepts and sends the message.
A server as www.google.com is always open for incoming connections
19. P a g e | 18
So we mean that the server is assumed to continue at all times relevant to the Internet. So we have to
keep our server open non-stop.
The simplest way to do this is to introduce Accept function () in a loop that so he can receive replies to
incoming connections in real-time.
Live Server
Thus a Live Server should always remain so. Let's see the code below:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;
// Creating socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
}
// Prepares sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
// hardening
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connection Problem ");
return 1;
}
puts("Connection top ");
// Listen
listen(socket_desc , 3);
// Application of incoming connections
puts("Waiting for incoming connections...");
20. P a g e | 19
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(socket_desc, (struct sockaddr *)&client,(socklen_t*)&c)))
{
puts("Relationship accepted ");
// Customer response
message = "Hello Client n";
write(new_socket , message , strlen(message));
}
if (new_socket<0)
{
perror("Application failed ");
return 1;
}
return 0;
}
Now start the program in terminal 1 and terminal 3 different step. From each of these three terminals
connect to the server on port specified.
Each terminal should be shown as follows:
The server should be shown:
Connection charge.
Waiting for connection...
Binding was applied
Binding was applied
21. P a g e | 20
Binding was applied
So now the server is going nonstop and telnet client terminals are also connected nonstop. All terminals
with telnet will display "Connection closed by foreign host."
Much better so far. But still there is effective communication between server and client.
The server accepts connections in a loop and then sends them a message, and then he does nothing
with them. Also he is not able to hold more than one connection per time.
So now is the time to create a server that can hold more relationships at the same time.
Keeping more connections to the server with "threads"
To keep any links to share the code to act for the main server time accepting any authorized connection.
One way is by keeping them using themes or "threads". The main server receives connections and
creates a new topic to maintain communication for connecting fixed and then turn the server to accept
new connections
Linux threads library can be performed with pthread (POSIX threads). It will be good if we read for this
tutorial small library if you do not know anything about them.
However, use is not very complicated
#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
#include<pthread.h> //for threading , link with lpthread
void *connection_handler(void *);
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c , *new_sock;
struct sockaddr_in server , client;
char *message;
// Creating socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Socket can not be created ");
22. P a g e | 21
}
// Preparation of the structure sockaddr_in
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
// Relationship or blindness
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connection Problem ");
return 1;
}
puts("Connection top ");
// Listen
listen(socket_desc , 3);
// Acceptance of incoming connections
puts("Waiting for connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)))
{
puts("Relationship accepted ");
// Sending a message to the client
message = "Hello Clientn";
write(new_socket , message , strlen(message));
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = new_socket;
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("can not create topic ");
return 1;
}
//pthread_join( sniffer_thread , NULL);
puts("The holder was appointed ");
}
if (new_socket<0)
{
perror("Application failed ");
return 1;
}
23. P a g e | 22
return 0;
}
/*
This will keep connections for each customer connected
*/
void *connection_handler(void *socket_desc)
{
// Get the description of the socket
int sock = *(int*)socket_desc;
char *message;
//Send messages to clienti
message = " Hello, I'm your retainer connections n";
write(sock , message , strlen(message));
message = " It is my duty to communicate the with you ";
write(sock , message , strlen(message));
//point socket
free(socket_desc);
return 0;
}
Start the server with up and step 3 terminal as before. Now the server will create themes for each
customer that will be connected to the
Terminals with telnet will display.
It looks very good, but the holder of communication is also a bit lazy. After greeting he closes the
program. He must stay alive and to communicate with the client. One way is to keep the connection
made to wait for some messages from the client for as long it is associated. If the client's bolts, holder of
the links must be completed.
24. P a g e | 23
So linkage holder can reset as follows: Now we need to use threads to create the means for each
connection that the server will accept. The example below I coded in C language for the creation of the
server with the following topics:
/*
This will keep for each client connection
*/
void *connection_handler(void *socket_desc)
{
// Get the description of the socket
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
// Send message to the client
message = " Hello, I'm your retainer connectionsn";
write(sock , message , strlen(message));
message = " Now press and something should turn back to you n”;
write(sock , message , strlen(message));
// Receives a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
// Send message back to the client
write(sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client timed out ");
fflush(stdout);
}
else if(read_size == -1)
{
perror("marking failed");
}
//pointer socket
free(socket_desc);
return 0;
}
25. P a g e | 24
Holder above link will take some data from the client and will return it back to the same. Simple! .now
given below production of code
$ Telnet localhost 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello Client
Hello, I'm your retainer connections
Now press something should turn back to you Hello
Hello
How are you
How are you
i am very well
i am very well
Relationship with pthread library
When compile programs that use pthread library we should link with this library. Below is given the way
he can perform the connection.
$ gcc program.c -lpthread
Conclusion
By now you should have learned the simple things in socket programming in C. You may try to do several
experiments like to write a program that acts as a chat or something similar to this.
Thank you