![]() |
Sockets in C Tutorials
What is a socket?See there are many types of sockets hardware sockets “like electrical sockets” etc. and software “programming sockets” like what i'm going to talk about from now onwards. See , a socket can be thought like any standard way to perform network communication through our system to another system or multiple systems . In this course I'll be only talking about establishment of connection in between two machines. Types of sockets with respect to programming:-Socket definations in </usr/include/bits/socket.h> Code:
/* Types of sockets. */
Stream sockets:-They are basically connection oriented reliable 2 way communication sockets. Having problem understanding that one .. Its Ok!! i'll make it simple. Call your friend through the phone and notice your friend receives the request in the form of a call then as he picks up both sides initiates the connection to one other and after the connection is established and each side can communicate to each other. In addition there is a confirmation that the words you speak actually reaches the other side. Now I think you'll find it easy to gasp up. Stream sockets use TCP/IP protocol a tutorial on this can be found on http://www.w3schools.com/tcpip/tcpip_intro.asp or i'll write a tutorial on it soon... Drawbacks of stream sockets:-They are two way connection sockets and they are slower in terms of sending and receiving than Datagram sockets because when we send a packet through Stream sockets it will send it and then ensure that is it reached or not then It will send back the confirmation message. Quite a long way right... To avoid this drawback our network gurus invented “THE DATAGRAM SOCKETS” yupii!!! ….LOL... We'll talk about them right down there... ok. Now let us come to the Datagram sockets … Datagram sockets:-What is a datagram sockets is that they are the one-way, non-reliable connectionless sockets . Yeah I'll make it easy.. Now imagine you are mailing [ not e-mailing ] a letter to your old friend staying overseas through not a very good company . You are not sure that will it reach to its destination or not. Yeah you are right out mailing service providers are quite reliable but the net is not. So why do we need them then... “man they are useless” no my friend these are also very important sockets in some programs .. some common programs using datagram sockets are :- Softwares dealing with network games , streaming media , etc etc....because packet loss is acceptable but loss of speed unacceptable. Disadvantages of Datagram sockets:-These are non-reliable.. and are one-way connection based. ProtocolsAs per as defined in </usr/include/bits/socket.h> there are the following types of protocols :- Code:
/* Protocol families. */ So , in all of the above the ones we are interested in are the Code:
#define PF_UNSPEC 0 /* Unspecified. */ Structs and declarations :-First of all i'll tell you what are the actual role of these structures in our programming.. See these structures are the ones who so all the stuff to send receive data in and out . We just fill out these structures and the rest is all to just sit and watch your program work! Wait! I'm not saying that all the work done is by the structs .No. These structs are accompanied by some functions which I will mention later in this guide... Struct sockaddr:- Code:
/* Structure describing a generic socket address. */ And sa_data contains a destination address and port number for the socket. This is rather unwieldy since you don't want to tediously pack the address in the sa_data by hand. The functions will do that for you... struct sockaddr_in:- Code:
struct sockaddr_inIn the above structure :- Code:
struct in_addr sin_addr; // Internet addressTo deal with Some Ipv6 addresses our NETWORK GURUS Code:
This struct is commented enough for your understanding . I'll explain this with more detail when we'll make our programs. Structure Sockaddr_storage Code:
struct sockaddr_storage { Struct addrinfo:- Code:
struct addrinfo { This structure is simply used for a function getaddrinfo() which i'll get to in a minute!!! FunctionsTo make our work easier our C network gurus created some top class functions to automate the automation … yeah! So here they come :-
ConclusionThanks for viewing this tutorial... Please try and leave a comment or thanks... |
Re: Sockets in C Tutorials
It would be gr8 if the viewers could comment!!!!!
|
Re: Sockets in C Tutorials
Most interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.
Notice that the client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established. Notice also that once a connection is established, both sides can send and receive information. The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. A socket is one end of an interprocess communication channel. The two processes each establish their own socket. The steps involved in establishing a socket on the client side are as follows: 1. Create a socket with the socket() system call 2. Connect the socket to the address of the server using the connect() system call 3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls. The steps involved in establishing a socket on the server side are as follows: 1. Create a socket with the socket() system call 2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine. 3. Listen for connections with the listen() system call 4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. 5. Send and receive data |
Re: Sockets in C Tutorials
Quote:
|
Re: Sockets in C Tutorials
Hello to all,,,,,sneha here...........I agree this comment...I like it,,,,,,,
"The steps involved in establishing a socket on the server side are as follows: 1. Create a socket with the socket() system call 2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine. 3. Listen for connections with the listen() system call 4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server. 5. Send and receive data."..........thanks for share with us,,,,,:crazy::crazy: |
Re: Sockets in C Tutorials
Quote:
|
Re: Sockets in C Tutorials
hello
i need socket.h & ... please help me |
Re: Sockets in C Tutorials
Quote:
|
Re: Sockets in C Tutorials
Quote:
with regards,,, sneha123 |
Re: Sockets in C Tutorials
Quote:
|
| All times are GMT +5.5. The time now is 08:37. |