Sunteți pe pagina 1din 20

Network programming

Manjunath.

Administrative issues (1)


Instructors: Manjunath Mattam Moodle and Mediawiki support: Mohana Krishna Group mailing list: networkstudents@msitprogram.net Courses slides, tasks, readings, in local mediawiki. Submissions in moodle only. Text book:UNIX Network Programming Volume 1, Third Edition: The Sockets Networking API Other text books available in references section.

Administrative issues (2)


Grading: is done based on tasks, vivas, demos, presentations, home works, reading assignments. Exam/ concept test conducted, scores are reflected in final grade. The deadline for any assignment can be extended with a 10% penalty per day. Deliverables are not accepted if the scheduled week is over. Collaboration and plagiarism Students are encouraged to talk to each other, to the TA, to the mentor or to any one else about assignment. Any assistance though must be limited to discussions. Student must write/code his/her own work. Detailed plagiarism policy is available in Logistics page.

Why network programming?


Discipline of designing and implementing network programs. Single program can communicate with other computers on network.
Add power to simple programs.

How to program in network?

What is a socket?
End point, of a bidirectional inter process communication flow across internet protocol based computer network. Interface between an application and transport layer. Socket address is a combination:
IP address Port Protocol (?)
Stream sockets & Datagram sockets SOCK_STREAM , SOCK_DGRAM.

How to create a socket?


Using socket function: int socket(int domain, int type, int protocol); Example: int sockfd; Sockfd = socket(AF_INET, SOCK_STREAM,0); If protocol is set to 0, then we let socket decide what protocol to use depending on type. Socket function returns socket descriptor, if an error then returns -1.

Socket Structures
What is the size of IPv4 network address?
32 bit

Byte Order

Functions to convert from host to network byte order and vice versa. htonl, htons, ntohl, ntohs

Humans vs. sockets? Convert internet address to socket address or ascii.


inet_aton or inet_ntoa

Listens or waits for connect requests. Accept incoming requests directed to this socket.

TCP Socket
Assign local port address to socket.

Used by client to connect with server.

int listen(sockfd, int backlog);


Maximum no of connections to queue for socket.

Socket API

Socket Programming

1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

socket bind connect listen accept send,recv sendto, recvfrom close, shutdown getpeername gethostname

UDP Socket

Concurrency
Ability to handle more than one client at a time. Use inherent operating system multi tasking capabilities.

Fork-exec
# include <unistd.h> pid_t fork(void); //0 in child, process id, -1 on error.
Called once, returns twice. If success returns process ID to parent , and returns 0 to child. Child can always get parent id by getppid. Forking is copying (calling). And the clone calls exec to make newly executed program. When child is done executing it process runs exit. Parent must be alive until child is done with process / makes itself into new program.

fork

What is port? What is socket?

Thank you

S-ar putea să vă placă și