Wednesday, September 23, 2009

Networking Interview Questions and Answers

what is Socket? how it is created?

A socket is one end of a two-way communications link between two programs running on the network.

Creation of sockets is done using the socket() system call.

int socket(int address_family, int socket_type, int proto_family);

address_family defines the type of addresses we want this socket to use.

socket_type could be the type of interaction (and type or protocol) we want to use.

proto_family selects which protocol we want to socket to use. We will usually leave this value as 0 (or the constant PF_UNSPEC on some systems), and let the system choose the most suitable protocol for us. As for the protocol itself, In the Internet address family, a socket type of SOCK_STREAM will cause the protocol type to be set to TCP. A socket type of SOCK_DGRAM (Datagram socket) will cause the protocol type to be set to UDP.

The socket system call returns a file descriptor which will be used to reference the socket in later requests by the application program. If the call fails, however (due to lack of resources) the value returned will be negative (note that file descriptors have to be non-negative integers).

As an example, suppose that we want to write a TCP application. This application needs at least one socket in order to communicate across the Internet, so it will contain a call such as this:

int s; /* descriptor of socket */
s = socket(AF_INET, SOCK_STREAM, 0);

How to Associate a connection with Socket?

After a socket is created, it still needs to be told between which two end points it will communicate. It needs to be bound to a connection. There are two steps to this binding. The first is binding the socket to a local address. The second is binding it to a remote (foreign) address.

Binding to a local address could be done either explicitly, using the bind() system call, or implicitly, when a connecting is established. Binding to the remote address is done only when a connection is established. To bind a socket to a local address, we use the bind() system call, which is defined as follows:

int bind(int socket, struct sockaddr *address, int addrlen);

There are 4 possible variations of address binding that might be used when binding a socket in the Internet address family.

The first is binding the socket to a specific address, i.e. a specific IP number and a specific port. This is done when we know exactly where we want to receive messages. Actually this form is not used in simple servers, since usually these servers wish to accept connections to the machine, no matter which IP interface it came from.

The second form is binding the socket to a specific IP number, but letting the system choose an unused port number. This could be done when we don't need to use a well-known port.

The third form is binding the socket to a wild-card address called INADDR_ANY (by assigning it to the sockaddr_in variable), and to a specific port number. This is used in servers that are supposed to accept packets sent to this port on the local host, regardless of through which physical network interface the packet has arrived (remember that a host might have more than one IP address).

The last form is letting the system bind the socket to any local IP address and to pick a port number by itself. This is done by not using the bind() system call on the socket. The system will make the local bind when a connection through the socket is established, i.e. along with the remote address binding. This form of binding is usually used by clients, which care only about the remote address (where they connect to) and don't need any specific local port or local IP address. However, there are exceptions here too.

How to send and receive data from sockets?

int read(int socket, char *buffer, int buflen);
int write(int socket, char *buffer, int buflen);

The return value may be:

0 - The connection was closed by the remote host.
-1 - The read system call was interrupted, or failed for some reason.
n - The read system call put 'n' bytes into the buffer we supplied it with.

what are different types of Socket?
Stream Sockets, Datagram Sockets, Raw Sockets, Packet Socket.

what is ICMP?
Internet control message protocol is used by hosts and gateway to send notifications of datagram problems to sender. It uses echo test to check whether it is responding or not.

what is Router? How to Design a router?
Router is used to route packets from one network to other.

what is Gateway? How to Design a gateway?
Conversion from one protocol to another.

what is network protocol time?

The Network Time Protocol (NTP) is a protocol for synchronising the clocks of computer systems over packet-switched, variable-latency data networks. NTP uses UDP as its transport layer. It is designed particularly to resist the effects of variable latency.

what situation packet goes into infinite loop?
If ttl value in IP header is not properly decremented at every router.

what protocol is used to retrieve mails?
POP3 and IMAP4 are used to retrieve mails.
IMAP4 stores a copy of message on the server whereas POP3 does not.

what is IP tunneling? how it works?
IP tunneling (IP encapsulation) is a technique to encapsulate IP datagram within IP datagrams, which allows datagrams destined for one IP address to be wrapped and redirected to another IP address. IP encapsulation is now commonly used in Extranet, Mobile-IP, IP-Multicast, tunneled host or network.

what is tcp 3 way handshaking?
Client starts active open after passive open in server side in TCP. The client sends SYNC, then the server send SYNC+ACK, last client send ACK.

What are the different type of networking working devices?

Also called a regenerator, it is an electronic device that operates only at physical layer. It receives the signal in the network before it becomes weak, regenerates the original bit pattern and puts the refreshed copy back in to the link.

These operate both in the physical and data link layers of LANs of same type. They divide a larger network in to smaller segments. They contain logic that allow them to keep the traffic for each segment separate and thus are repeaters that relay a frame only the side of the segment containing the intended recipent and control congestion.

They relay packets among multiple interconnected networks (i.e. LANs of different type). They operate in the physical, data link and network layers. They contain software that enable them to determine which of the several possible paths is the best for a particular transmission.

They relay packets among networks that have different protocols (e.g. between a LAN and a WAN). They accept a packet formatted for one protocol and convert it to a packet formatted for another protocol before forwarding it. They operate in all seven layers of the OSI model.

what is frame relay? which layer it comes?
Frame relay is a packet switching technology, it comes in data link layer.

what are the names of PDU in higher layers?

Transport layer: segment.
Network Layer: packet.
Data link layer: frames.
Physical layer: bits/ bytes.

Thursday, September 10, 2009

How to Debug Using gdb


After executing the binary executable we some times get unexpected results. The errors may be of dangling pointers, Stack over flow, Divide by zero etc, Which are very difficult to debug by looking at the Source code. GDB is a tool to debug the program it supports various languages like c,c++ etc.

gdb can take core dump file or executable file. Here executable is executed in the context of gdb. To debug using gdb at compilation time we have to give -g option.

gcc -g source.c -o source

Note: Electrical fence is used to check boundary errors. like size of array is 30 and we are storing value at 40 th location.
gcc source.c -lefence

After allocating memory using malloc, Always allign the memory using memalign. To avoid type casting.

Running a executable in gdb: gdb a.out
The you will get a gdb prompt.
The following commands has to be typed at gdb prompt.

run or r is used to run the program.
step is used to step by step execution, it also oes inside the function.
next is used to avoid function debug.

passing parameters: set args fastrevision blogspot
showing the arguments: show args
debug a already running program: attach process-id.
To get information about threads: info threads

Checkpoint: Setting a book mark to return that place easily.
checkpoint [lineno][function name]
during creation of checkpoint it saves all register, Stack values , Variables etc.

After executing some code.

To return back to check point: restart checkpoint-ID.
To know information about checkpoints: info checkpoints.
To delete checkpoint: delete checkpoint checkpoint-ID.

Listing of Program

To know your program where it is currently Running.
gdb> list

Note: To know the return value of previously executed program.
echo $?
Successful execution returns 0. Abnormal termination of program returns more than 0.


To break the progrm after executing for some time. break command is used. For eg:
break lineno, function Name, Address.
while giving line numbers it always takes the first default file. To specify the file.
break file.c:

To add some condition: break 14 if ptr==0
To break the program only once: tbreak lineno
To get information about breakpoints: info breakpoints.
To ignore break point for some time: ignore breakpoint_no count.
To clear the breakpoint: clear [location][function name].
To disable break points: disable [breakpoints] [1..10]
To enable break points: enaable [breakpoints] [1..10]
We can execute some commands when it reaches a break point. By giving commands and end.
Commands is used to give commands which are in between commands and end to shell prompt.

Watch Points

To know what are the values. expression may be location,variable etc.
watch expression
rwatch expression
awatch expression
info watchpoints

To get information

ptype a: display what is the type of a.
info main
info source.

Saturday, September 5, 2009

Internet Protocol of TCP/IP

Internet Protocol:

IP packets are send by the help of TCP, UDP, ICMP etc. The IP packets are connection less and unreliable. if any problem occurs in network( like router is not responding ) it simply sends ICMP packet to the source. The reliability is provided by upper layers i.e TCP.

The IP header:

The most significant is at 0 and least significant bit at 31. Hence it is called big-endian byte order.
Data is transmitted from left to right i.e from 0 to 31.

Vers: It tells what version of IP packet it may be 4 or 6 according to the requirement.
IHL: The no of 32 bit words used in the header including any options. Normal value for this field is 5.
TOS: This tells the type of service of the packet. It tells minimum delay, maximum throughput, maximum reliability, minimize monetary cost. XXXUUUU0.

Total Length: This is the total length of IP Datagram in bytes. It is possible to send 65535 size packets, but the link layer divides in into small packets using fragmentation.

Indentification: This value is increased while sending packets continuously. This is mainly used in fragmentation. Reassembly program at the other end is used to combine the fragmented packets.

Time to live:A no which tells the no of routers through which this IP datagram is passed. When ii reaches to zero. The send is informed with an ICMP message indicating destination is not reachable.

Protocol: Which protocol gave the data to IP to send.

Option Field: This field is defined for
Security and handling restrictions.
Record route: Each router record its IP Address.
Time stamp: ( how long the router will keep the recorded IP Address).
loose Source routing : Specifies list of IP Address that must be traversed by datagram.
strict source routing : Specifies the list of fixed ip Address to be traversed. ( It is rarely used, All router may or may not use this feature).

Subnetting: The process of dividing hostid into small subnets is called subnetting.

IP Address:, subnet Mask: this means each subnet network is in the gap of 32 Addresses.
therefore subnet ID is : and host ID is: 1

How to Achieve Success in Life

Success can be many things, Success means better life, Success means finding real happiness, Remember one thing size of success is limited to size of thinking. If you think small your success is also small or if you think big and believe in yourself your success will be big.

  • Belief : Always think positive, Person belief is known by the way he talks,walks and acts. you grows small in the group of people if you don't have belief what you do. Always generate positive thoughts from your mind. you can't sell a product if you can't believe in yourself. All discoveries are made because they have self belief in themselves. Successful people are ordinary people they believe in themselves what they do.
  • This is some thing which takes time, No body going to help you, you have to work hard and achieve it.
  • Three components are important in life. What you are going to do. How you accomplish your goal and the results will be greater respects from your friends and family, greater status, Increase in income and standard of living is higher.
  • The place you work is a laboratory, It includes the people you work, you talk and yourself. This place is for you to work to achieve your goal. See the difference in successful and unsuccessful persons.
  • The more successful the person is, The more less excuses he makes. Every unsuccessful person says I don't have time I am fully busy. Every body has got 24 hours it mainly depends how they utilize it effectively. In a year you have got 8760 hours.Total hours sleeping in a year= 2738.Total hours eating in a year= 730. Total hours in phone and chatting = 900. Total hours in traveling = 730. If a person is spending this much of time in all these activities, why they are saying I don't have time. Successful people don't say i don't have time. Successful people always like to face challenges. A successful person knows success is a extra effort and hard work.
  • Successful people never tell any health excuses. No body is interested to know about your health. If you say i feel great you get more confidence in yourself.
  • Always be happy with what you have. For example if you are getting less pay, you can say at least i have job for living.
  • Never give age excuses like i am too old to do this or I am too young to do this. There are lot of people who got success after lot of age. Age is not a barrier it is an excuse. Believe that you can handle any kind of responsibility.The productive age is from 20 to 70. Invest your future time in really what you want to do.
  • The only thing you should have interest and enthusiasm. Its not important how much intelligence you have.It is most important how you use your intelligence. Attitude is more important than intelligence. Always think the better ways of doing things. Always use your mind to get solution.
  • People are measured not by their money, family support, friends or job he is doing. People are measured the way they think. Clearing all obstacles which stop you to think big. Think what are your major attributes that helps you in getting success. For example if anybody else is getting promotion compare with him what are the qualities you are lacking. Try to improve those qualities for e.g. if your colleague has good communication skills and try to improve that one to add in yours assets for getting success. you create a kind of your image in others with the words you speak. So always use positive words.
  • Practicing give value to things around you. If you give value to these things then you can also give value to peoples. Small things matters for a Successful person.
  • Also think creative. new, better and improved ways of doing things. Success at home, work and society are important.They are many creative ways of doing things.
  • Before you begin your work think how can i do better work today. Improve quality and quantity of your work.
  • Eliminate the word impossible from your thinking and speaking vocabulary. Take a task which is difficult and write down the reasons you can't do it. Try to work out on the reasons. Man goes where he wants to go. Take risk in doing the things. There is no one best way to do things. Always welcome new ideas. Expose yourself to new restaurants, New books, new friends. Do some thing different in weekends. Be progressive. Get up a hour earlier for planning the daily activities.
  • Achieving high level of success requires support of others. To achieve support of others: See things from their eyes, think as if your in the other side. Always handle the things as a human way.
  • Being a good listener is very important in life. Listening gives us more information to make important decisions. To practice listening encourage others to talk in personal or group meetings. Ask about tell me about your experiences, what you think to done about this and what you think the key point here. Do information research. Concentrate in others what they said. Always ask yourself how you can I do this better, how can i do more.
  • Fear is biggest enemy. It decreases our mental ability. It makes people sick. It closes your mouth when you want to speak in public. No one is born with confidence. Action cures fear. Before taking action think that is it correct and human way of doing things. When ever you feel fear think that what kind of action I should take to avoid fear. There are some fears which can't be avoidable in such time play with small kids, go for a movie, Put some extra work in some special project. Biggest fear is that what other people say and think about you.
  • Give value to your ideas by acting in it. Action improves your confidence. Before thinking of task. Go and do that task. for e.g. Going for a Movie. Doing exercise etc. Be active not passive. Be a doer. Don't wait until the conditions are perfect to act. Get more ideas but these ideas are valid when you act on it.
  • Successful people meets the problems and work out on difficulties when they do arise. Cross bridge when you come on it.
  • Always store positive thoughts in mind. Always think about positive things that happened with you through out the day before you go to bed. Always think positive if you get negative thoughts try to divert you mind from that.
  • Fear on other people is a big fear. To overcome that fear you have to keep the people in proper prospective. Other people are basically like you so there is no reason to fear. Other person is important as much as you are. Every person as his own problems, desires etc. Understanding attitude is very important in life if some body insults you and scolds you simply think that this person is having a bad day. Control your fire. We can change our attitude by changing our physical attitude.
  • Always sit in front seat. Always maintain eye contact. walk 25% faster. practicing speaking up. Speak up at every opportunity. Every open meeting you attend speak up. Every family gathering speak up. Make a contribution. Make a suggestion. Ask a question. Speak slowly and clearly. Smile big to improve your confidence.