Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

Assignment of Network Programming (IT-520)

The document discusses socket programming and different network models. It describes sockets as bidirectional endpoints for communication between two applications that allow software to communicate over networks. It covers key socket system calls like socket(), bind(), connect(), listen(), accept(), send(), recv(), and close(). It also explains the client-server and peer-to-peer network models, contrasting how they differ in terms of resource sharing and security.

Uploaded by

Gurpreet Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Assignment of Network Programming (IT-520)

The document discusses socket programming and different network models. It describes sockets as bidirectional endpoints for communication between two applications that allow software to communicate over networks. It covers key socket system calls like socket(), bind(), connect(), listen(), accept(), send(), recv(), and close(). It also explains the client-server and peer-to-peer network models, contrasting how they differ in terms of resource sharing and security.

Uploaded by

Gurpreet Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment Of Network Programming [IT-520]

Submitted To: Ms. Harpreet Kaur (Subject Incharge)

Submitted By: Gurpreet Kaur L-2010-AE-10-MCA

School of Electrical Engg. & Information Technology P.A.U, Ludhiana.

Assignment 2

Socket and Socket Programming A socket is one of the most fundamental technologies of computer networking. Sockets allow applications to communicate using standard mechanisms built into network hardware and operating systems. Software applications that rely on the Internet and other computer networks continue to grow in popularity. Many of today's most popular software packages -- including Web browsers, instant messaging applications and peer to peer file sharing systems -- rely on sockets. Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Sometimes the one application that initiates communication is termed the client and the other application the server, but this terminology leads to confusion in non-client/server systems and should generally be avoided. Sockets provide an interface for programming networks at the transport layer. Network communication using Sockets is very much similar to performing file I/O. In fact, socket handle is treated like file handle. The streams used in file I/O operation are also applicable to socket-based I/O. Socket-based communication is independent of a programming language used for implementing it. That means, a socket program written in Java language can communicate to a program written in non-Java (say C or C++) socket program. A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server listens to the socket for a client to make a connection request (see Fig. 13.4a). If everything goes well, the server accepts the connection (see Fig. 13.4b). Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client.

In a nutshell, a socket represents a single connection between exactly two pieces of software. More than two pieces of software can communicate in client/server or distributed systems (for example, many Web browsers can simultaneously communicate with a single Web server) but multiple sockets are required to do this. Socket-based software usually runs on two separate computers on the network, but sockets can also be used to communicate locally (interprocess) on a single computer. Types Socket interfaces and Socket Programming can be divided into three categories. 1. TCP (stream socket) 2. UDP (datagram socket) 3. Raw Socket Perhaps the most commonly-used type, the stream socket, implements "connection-oriented" semantics. Essentially, a "stream" requires that the two communicating parties first establish a socket connection, after which any data passed through that connection will be guaranteed to arrive in the same order in which it was sent. Datagram sockets offer "connection-less" semantics. With datagram, connections are implicit rather than explicit as with streams. Either party simply sends datagram as needed and waits for the other to respond; messages can be lost in transmission or received out of order, but it is the application's responsibility and not the socket's to deal with these problems. Implementing datagram sockets can give some applications a performance boost and additional flexibility compared to using stream sockets, justifying their use in some situations. The third type of socket -- the so-called raw socket -- bypasses the library's built-in support for standard protocols like TCP and UDP. Raw sockets are used for custom low-level protocol development. System Calls for Socket Programming
1.

socket()

It simply returns to you a socket descriptor that you can use in later system calls, or -1 on error. int socket(int domain, int type, int protocol); bind() Once you have a socket, you might have to associate that socket with a port on your local machine. The port number is used by the kernel to match an incoming packet to a certain process's socket descriptor. If you're going to only be doing a connect(), this may be unnecessary.

bind() also returns -1 on error and sets errno to the error's value. Another thing to watch out for when calling bind(): don't go underboard with your port numbers. All ports below 1024 are RESERVED (unless you're the superuser)! You can have any port number above that, right up to 65535 (provided they aren't already being used by another program.) int bind(int sockfd, struct sockaddr *my_addr, int addrlen); sockfd is the socket file descriptor returned by socket(). my_addr is a pointer to a struct sockaddr that contains information about your address, namely, port and IP address. addrlen can be set to sizeof(struct sockaddr). connect() It is used to connect to the remote machine i.e. If you are a telnet application, then after socket(), the user tells you to connect to "10.12.110.57" on port "23" (the standard telnet port.) At that time you use connect() call. int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); listen() if you dont want to connect to a remote host. Say that you want to wait for incoming connections and handle them in some way. The process is two steps. First step is listen(). It allocates space to queue incoming calls for the case that several clients try to connect at the same time. int listen(int sockfd, int backlog); sockfd is the usual socket file descriptor from the socket() system call. backlog is the number of connections allowed on the incoming queue. accept() To block waiting for an incoming connection the server executes an accept()primitive. It returns a normal file descriptor which can be used for reading and writing in a standard way,the same as for files. send() and recv() These two functions are for communicating over stream sockets or connected datagram sockets. If you want to use regular unconnected datagram sockets, youll need to see the section on sendto() and recvfrom() system calls. Close()

Connection release with the socket is symmetric. When both sides have executed a close() primitive,the connection is released. System calls allows you to access the network functionality of a UNIX box. When you call one of these functions, the kernel takes over and does all the work for you automatically. The order in which the system calls are made is very important.

Client Server Model The client/server model is a computing model that acts as distributed application which partitions tasks or workloads between the providers of a resource or service, called servers, and service requesters, called clients. Often clients and servers communicate over a computer network on separate hardware, but both client and server may reside in the same system. A server machine is a host that is running one or more server programs which share their resources with clients. A client does not share any of its resources, but requests a server's content or service function. Clients therefore initiate communication sessions with servers which await incoming requests. The client/server characteristic describes the relationship of cooperating programs in an application. The server component provides a function or service to one or many clients, which initiate requests for such services. Functions such as email exchange, web access and database access, are built on the client/server model. Users accessing banking services from their computer use a web browser client to send a request to a web server at a bank. That program may in turn forward the request to its own database client program that sends a request to a database server at another bank computer to retrieve the account information. The balance is returned to the bank database client, which in turn serves it back to the web browser client displaying the results to the user. The clientserver model has become one of the central ideas of network computing. Many business applications being written today use the clientserver model. So do the Internet's main application protocols, such as HTTP, SMTP, Telnet, and DNS. Peer to Peer model Peer-to-peer networks involve two or more computers pooling individual resources such as disk drives, CD-ROMs and printers.[2] These shared resources are available to every computer in the network, while each two of them communicate in a session. Each computer acts as both the client and the server which means all the computers on the network are equals, that is where the term peer-to-peer comes from. The advantage of peer-to-peer networking is the easier control concept not requiring any additional coordination entity and not delaying transfers by routing via server entities. However, the collision of session may be larger than with routing via server nodes. In the peer to peer network, software applications can be installed on the single computer and shared by every computer in the network. They are also cheaper to set up because most desktop operating systems have the software required for the network installed by default. On the other

hand, the client/server model works with any size or physical layout of LAN and doesn't tend to slow down with a heavy use.[3] Peer-to-peer networks are typically less secure than a client/server networks because security is handled by the individual computers, not controlled and supervised on the network as a whole. The resources of the computers in the network can become congested as they have to support not only the workstation user, but also the requests from network users. It may be difficult to provide systemwide services when the client operating system typically used in this type of network is incapable of hosting the service.

You might also like