Vous êtes sur la page 1sur 34

TRAINING PROGRAM

AT

NIIT, JAIPUR
A Project On

MULTI-CLIENT CHAT

SUBMITTED TO: DR. M.C. GOVIL


PROF. CSE & IT DEPT MNIT, JAIPUR

SUBMITTED BY: PANKAJ JINDAL


VII Sem , B.Tech. 020923

INFORMATION TECH.
NEERAJ KUMAR GUPTA VII Sem , B.Tech. 020927

INFORMATION TECH.

MALVIYA NATIONAL INSTITUTE OF TECHNOLGY JAIPUR-302017 2005

ACKNOLEDGEMENT
It is very difficult for any project schedule to be satisfactorily completed without the co-operation of and the benefit of advice from a large number of persons whether they are engineers or experts in their field of specialization. The satisfaction that accompanies the successful completion of any task would be incomplete without the mention of people whose ceaseless cooperation made it possible, whose constant guidance and encouragement crown all efforts with success. I am very grateful to Ms. Seema Dagur, Manager of NIIT Ltd, CScheme, Jaipur, for the help provided in completing the project assigned to me. I owe my thanks to Mr. Manish Kumar, Career Advisor of the Company. They gave their full support for having provided me with the opportunity to work as a trainee in their Company. Last but not least, I pay my sincere thanks and gratitude to all the Staff Members for making my training valuable and fruitful. I am also thankful to all my colleagues for their co-operation and support.

Date: 1 August, 2005

PANKAJ JINDAL NEERAJ GUPTA

CONTENTS
1. Introduction
1.1.

About NIIT About Project

6 7 8 11 19 28 30 34 34

1.2.

2. Object Oriented Approach


3.

JAVA Features Used

4. Networking Concepts Used 5. Design & Implementation 6. User Manual


7. Conclusion

8. References

1. Introduction 1.1 About NIIT


NIIT, the global IT Learning Solutions Corporation, is known for its pioneering work in the field of IT education and training. Our strong research orientation has helped us continuously innovate in the areas of instructional design methodologies, and curricula Development that is cutting-edge. NIITs vast education delivery network spread over 30 countries in the Americas, Europe, Asia, Middle East, Africa and Australia/Oceania, blends classroom and on-line learning. The company provides a comprehensive education environment to individuals and enterprises, offerings training that is customized to the varied needs of audiences with diverse backgrounds Founded in 1981, with the mission of "Bringing People and Computers Together...Successfully," NIIT services enterprises and individuals in 42 countries with its wide ranging Learning Solutions. The NIIT Group is also positioned strongly in the software and services space through NIIT Technologies.

Research and Development is central to NIITs IT learning philosophy. It is a core strength that helps us identify the emerging market needs, leads us to create new learning methodologies and techniques, helps us refine our curriculum, making it cutting-edge, and enables us to improve our education delivery. Our pioneering efforts in the understanding of human cognition has added to the learning effectiveness of our offerings. NIIT scientists are creating solutions capable of dealing with the new learning environment thrown open by the Internet, that addresses anytime, anywhere learning. Besides making our IT learning more state-of-the-art, through innovations in Cognitive Multimedia, Constructivist Education and the Human-Computer Interface, our R&D initiatives have also led to several breakthroughs in areas such as NetCentric Computing and e-Commerce. NIITs R&D is poised to shape the usage of IT, creating solutions for tomorrows world.

1.2 About Project


Communication among computers is an important aspect in the era of modern computing. And to solve this purpose client-server programming pay very important role, By keeping that point in mind, we targeted our project on Networking. Although client server programming can be done using Perl, C, C++ etc. But we opted Java to develop the project in Networking. Due to its special features which make it easier to develop a client-server programming in form of sockets. Java is most suitable language to implement a project on networking because it have a whole package on networking named as java.net. In this package it have many inbuilt classes and functions which gives us a better approach to design a project. In this project we have used JAVA Swing and
7

AWT classes for designing User interface. The another reason to opt Java as a programming language to develop this project is the current aspects of Java. We realize that there is a need to develop our skills of programming in Java and it is the most suitable way to do this. We try to implement a simple demonstration of networking using Java. We can establish this project on any two connected computers. In this project we have some protocols between the client and server. Every time when an user give an input then the client sends this input to server then server gives the response according to the input and send it to another client as desire of the requested client. The Minimum Software requirements are: 1 Windows 98/2000/NT/XP 2 Java Virtual Machine (JVM) Installed Using : 1. Java Development Kit (JDK 1.2/1.3/1.4) The Minimum Hardware requirements are: Two Computer Systems with Configuration: 1. Pentium-I or equivalent processor or all higher processors. 2. 64MB RAM. 3. 10MB of memory.

2. Object Oriented Programming Approach(OOP).


OOP is invented to remove some of the flaws encountered in the procedural approach. OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. OOP
8

ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. Some specialties of OOP are given as: 1 Emphasis is on data rather than procedure.
2 3

Programs are divided into what are known as objects. Data structures are designed to characterize the objects. together in the data structure.

4 Functions that operate on the data of an object are tied 5 Data is hidden and cannot be accessed by external functions. 6 Objects may communicate with each other through function. 7 New data and functions can be easily added 8 Follows bottom-up approach in designing program Basic Concepts of Object-Oriented Programming: Objects: Objects are the basic run-time entities in the object-oriented system. They may represent basic data types or any other user-defined data type. Programming problem is analyzed in terms of objects and the nature of communication between them. Objects are generally chosen such that they match closely with the real-world objects. Objects take up space in the memory and have an associated address like a structure in C. When a program is executed, the objects interact by sending messages to one another.

10

Classes: Classes are user defined data types which helps in defining the entire set of data and the code of an object .A class is also known as collection of objects of same type.

Encapsulation:
The wrapping up of data and the function into a single unit is known as

encapsulation. The data is isolated from direct access by the program is known as Data Hiding or Information Hiding. Inheritance: It is the process by which objects of one class acquire the properties of other class. It provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. The concept behind this sort of division is that each derived class shares common characteristics with the class from which it is derived. Polymorphism: Is a reek word which means ability to take more than one form. It is a concept which tells us that an operation may exhibit different behaviors s in different instances. The behavior depends upon the types of data used in the operation. A general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. It also helps in implementation of Inheritance.

10

11

Concept of Object Oriented Programming

So, we choose the Object-Oriented Programming methodology for development of our project to grasp all the advantages of it.
11

12

3. JAVA features Used


As we described earlier that the Object Oriented Programming is a better methodology to develop a real world software. Therefore, we used OOP to develop this project. And to design this project we elect an Object Oriented language JAVA because of the numerous suitable features provided by it to handle the Real World Problems. Now it is better to give some introduction and historical facts about Java at this time. Introduction to JAVA: Java language was developed by Sun Microsystems in 1991 as a part of the Green Project, a research group working to develop software to control consumer devices by one of the team member, James Gosling. In starting he named it as Oak as he used to see an Oak tree outside his office. Later on due to some legal problems, Sun renamed it as JAVA. Java is a platform independent language and works on the principle Write Once, Run Everywhere, Any Time, Forever. Therefore, it is frequently be used on Network type application, Internet etc. Java is totally an object oriented language. Everything in Java can only be written inside the classes. An important thing to remember about Java is that Java is a Strongly Typed Language and part of Javas Safety and Robustness comes from this fact. JAVA Versions and Environments: Until now, Java has undergone three significant version changes. Java was first launched as Java1.0, then came Java1.1, which had language enhancements, an improved user interface, and event handling.
12

13

Since then Java has undergone many more changes and finally Sun has come up with the latest version as Java1.2.2 that is calls Java 2. Java is available in many environments. Java software is most easily available as JDK (Java Development Kit) version 1.0, 1.1, 1.2, 1.3 or 1.4. JDK can be easily downloaded at the site http://java.sun.com.products/jdk free of cost. Features of JAVA: Java, apart from being object oriented, and providing various features for internet programming has many more features which makes it distinctively an appealing language. We describe them in nutshell below:

1.

Simple and Easy to Learn: One with clear basic concepts of object oriented programming easily able to write productive Java programs. Java has tried to keep mainly essential features emphasized upon their usefulness and tried to avoid complexity. Java is an easy to learn language because in Java, there are a small number of clearly defined ways to accomplish a given task. 2. Object-Oriented: As we have said, earlier that Java is a true object oriented language because the outermost level of data structure in Java is the object. Almost everything in Java is an object. All program code and data reside within objects and classes. Java comes with an extensive set of classes, arranged in packages, which we can use in our programs by inheritance. The object model in Java is simple and easy to extend.

13

14

3. Robust: Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and run time checking for data types. It is designed as a garbage-collected language relieving the programmers virtually all memory management problems. Java also incorporates the concepts of exception handling which captures series errors and eliminates any risk of crashing the system. 4. Architecture Neutral: Java has gained a huge popularity because of its platform independent nature. It has established itself as a high performance cross platform language. A Java program can run on any kind of client machine on the internet without making any changes to the source code. And this is achieved through JVM (Java Virtual Machine). Java program (.java file) execute in two stages. It is compiled and converted to byte code (.class file) at the developers end, uploaded on the internet server, then it is interpreted again and then executed. The byte code is a series of instructions in the form of bytes. These are not specific to any machine, but are written for JVM. 5. Secure: Java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in Java ensures that programs cannot gain access to memory locations without proper authorization. Java works as a firewall between a networked application and users computer. 6. Multithreaded:
14

15

Java supports multithreaded programming, which allows us to that do many things simultaneously to meet the real-world requirement of creating interactive networked program or graphical application. 7. Distributed: Java is designed as a distributed language for creating applications on networks. It has the ability to share both data and programs. Java applications can open and access remote objects on Internet as easily as they can do in a local system. This enables multiple programmers at multiple remote locations to collaborate and work together on a single project. 8. High Performance: Java performance is impressive for an interpreted language, mainly due to the use of intermediate bytecode. Java architecture is also designed to reduce overheads during runtime. Speed of Java is comparable to the native C/C++. The incorporation of multithreading enhances the overall execution speed of Java program. 9. Dynamic: Java is a dynamic language. Java is capable of dynamically linking in new class libraries, methods, and objects. Java can also determine the type of class through a query, making it possible to either dynamically link or abort the program, depending on the response. JAVA Standard Library: Most programming language rely on pre-built libraries to support certain functionality. As we have said earlier, that Java comes in this chain of

15

16

languages because of its fervent library. This Library includes hundreds of classes and methods broadly categorized in six functional packages as described below:

1 Language Support Package: One of the most important packages in Java class library is the java.lang package. This Language package contains the languages main support classes. It is virtually impossible to write a Java program without using the Language package. Now we are given the more important classes contained in the Language package: Object Class Type Wrapper Classes Math Class
16

17

String Buffer Class System Class 2 The Utility Package: The java.util package contains various utility classes and interfaces that are crucial for Java development. For the most part, they aid the developer in designing different types of data structures. The more important classes written in this package are: Calendar Date Dictionary Hash table Simple Time Zone Stack String Tokenizes Vector The Utility Package also declares three interfaces. These interfaces are also important part of this package. These are: Enumeration EventListener Observable The I/O Package: The java.io package provides support for reading and writing data to and from different devices. The classes in this package can be divided into three different packages. These packages are:
17

18

Input stream classes Output stream classes File classes

The Network Package: The java.net package provides support for communicating with other computers. Classes in this package supports developer to deal with TCP/IP, UDP and to do socket programming. The main classes are: Socket Server Socket Inet Address Datagram Socket I-net Socket Address Socket Address URL URL Class Loader Abstract Window Toolkit Package: The java.awt package contains classes that implements platform independent graphical user interface. The classes of this package provide developer to do real world programming by giving an extra feature Event Handling .The main classes and interfaces of this package are: Layout Manager Menu Container
18

19

Button Canvas Checkbox Color Event Font Graphics Image

4. Networking Concepts Used


Definition: Networking is a technique which uses physical connections, and computer programs to link two or more computers. It helps the user to share files, printers, and other resources; send electronic messages; and run
19

20

programs on other computers. Thus the concept of connected computers, sharing resources is called networking. Location of Computers on Internet: Computer connected on network have an address and that can be given by the following: 1. IP (Internet Protocol Address System): IP Address is the identifying number that enables any computer on the Internet to find any other computer on the network. Each packet sent over the Internet contains the IP address of the computer to which it is being sent. Intermediate routers use the address to determine how to forward the packet. Users almost never need to enter or view IP addresses directly. Instead, to make it easier for users, each computer is also assigned a domain name; protocol software automatically translates domain names into IP addresses. There are 5 address classes to represent the IP Address. Address Classes: Class Name Class A Class B Class C Class D Class E Address begin with In Binary In Decimal 0xxx 1-126 10xx 128-191 110x 192-223 1110 224-239 1111 240-254 Default Subnet Mask 255.0.0.0 255.255.0.0 255.255.255.0 255.255.255.255 For future use

INTERNET STANDARD PROTOCOLS: For communication and data transformation all computer hardwares and software have to follow series of standard rules known as computer communication protocols. This protocols specify how packet headers are formed and how packets are processed. Although many services use the Internet to carry data from one
20

21

computer to another, each service follows a separate set of rules that define the messages used in the exchange.

1.

TCP/IP(Transmission Control Protocol/Internet Protocol): TCP/IP is a set of rules that enables different types of computers and

networks on the Internet to communicate with one another. TCP/IP was originally developed by the United States Department of Defense for computers using the UNIX operating system, but it is now used by every computer, regardless of operating system, on the Internet. TCP defines how data are transferred (see Data Transfer) across the Internet to their destination. IP defines how data are divided into chunks, called packets, for transmission; it also determines the path each packet takes between computers. File Transfer Protocol or FTP is part of the Transmission Control Protocol/Internet Protocol (TCP/IP), the system that enables different types of computers and networks on the Internet to communicate. FTP, in computer communications, on the Internet and other networks, is a method of transferring files from one computer to another. The protocol is a set of rules that ensures a file is transmitted properly to the receiving computer. A computer that stores files that can be retrieved using FTP is called an FTP site or FTP server. Client/Server Architecture: It is an arrangement used on local area networks that makes use of distributed intelligence to exploit the full computing power. This is done by splitting the processing of an application between two distinct components: a client and a server.

21

22

Client is a complete, stand-alone personal computer offers the user its full range of power and features for running applications. the client computer needs to handle one activity at a time so small personal computers (PCs) are sufficient to work as a client. Server is a computer running administrative software that controls access to all or part of the network and its resources . enhances the client component by providing the strengths offered in a time-sharing environment: data management, information sharing between clients, and sophisticated network administration and security features. Powerful computers are chosen for server software because they must be able to handle requests for information from millions of people and do so quickly so that users who request information from the server will not experience long delays. To make information available, a computer runs a server program. To obtain and display information from a server, a computer user runs a client program. The client contacts a server to request information; the server responds by sending a copy of the requested information. To ensure that the exchange is meaningful, the client and server programs another. The advantages of the client/server architecture over older architectures are 1. Increase the processing power but it also 2. uses that power more efficiently. 3. The client portion of the application is typically optimized for user interaction, whereas the server portion provides the centralized, multi-user functionality. follow a communication protocol, a set of rules that the two programs use to talk to one

22

23

In client-server applications, the communication that occurs between the client and the server must be reliable. That is, no data can be dropped and it must arrive on the client side in the same order in which the server sent it. TCP provides a reliable, point-to-point communication channel that client-server applications on the Internet use to communicate with each other. To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection. A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. The client-server network is created in Java by capturing the sockets of each other by both client and server machines. This all process follows these steps: 1. Server runs on a specific computer and has a socket that is

bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request 2. The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port.

23

24

3. When server accepts the connection, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

4. When the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server. the socket on the client side is not bound to the port number used to rendezvous with the server. Now the client and server can communicate by writing to or reading from their sockets. The java.net package in Java provides a class, Socket, that implements one side of a two-way connection between a Java program and another program on the network. Also, java.net includes the ServerSocket class, which implements a socket that servers can use to listen for and accept connections to clients. We use the java.net and java.io packages to develop this project so, here we are giving a small description of both the packages: Javas I/O Package: Most of programs cannot accomplish their goals without accessing external data .external data is retrieved from an input source and result are sent to output destination. And for that purpose we use java.io package. Here are details of some important Java I/O classes:
24

25

BufferedInputStream: This class allows us to wrap any Input Stream into a buffered stream and to improve performance. It has two constructors: BufferedInputStream (InputStream inputStream) BufferedInputStream (InputStream.inputStream, int ufSize) The first constructor creates a buffered stream using default buffer size. While in second size is passed using bufSize. An optimal buffer size depends on the operating system, the amount of memory available and machine configuration. To make good use of buffer required this complicated calculations rather small buffer to an I/O is a better idea. Thus low-level system can read small block of data and store it in memory. Buffering an input stream provides read (), skip (), mark () and reset () methods for manipulation. BufferedOutputStream: This class allows us to reduce the number of times the system actually writes data and to improve performance. It has two constructors: BufferedOutputStream (Output Stream OutputStream) BufferedOutputStream bufSize) The first constructor creates a buffered stream using default buffer size. While in second size is passed using bufSize. Buffering an input stream provides flush () method to ensure that data buffers are physically written to the actual output device. BufferedReader: This class allows us to improve performance by buffering input. It has two constructors: (OutputStream OutputStream, int

25

26

BufferedReader (Reader inputStream) BufferedReader (Reader inputStream, int bufSize) The first constructor creates a buffered stream using default buffer size. While in second size is passed using bufSize. To support moving backward in the stream within available buffer it uses mark () and reset () methods. BufferedWriter: This class allows us to reduce the number of times the system actually writes data and to improve performance. It has two constructors: BufferedWriter (Writer outputStream) BufferedWriter (Writer outputStream, int bufSize) The first constructor creates a buffered stream using default buffer size. While in second size is passed using bufSize. It provides flush () method to ensure that data buffers are physically written to the actual output stream. PrintWriter: This class is character oriented version of PrintStream. It has four constructors: PrintWriter(OutputStream outputStream) PrintWriter(OutputStream outputStream, boolean flushOnNewline) PrintWriter(Writer outputStream ) PrintWriter(Writer outputStream, boolean flushOnNewline) Where flushOnNewline controls whether java flushes the output stream every time println() is called. If flushOnNewline is true, than flushing automatically takes place. If false, flushing is not automatic .the first and third constructors do not automatically flush. Javas PrintWriter objects support the
26

27

print() and println() methods for all types, including object. If argument is not simple type than the PrintWriter method will call the objects toString() method and then print out the result. Javas Networking Package: To build I/O objects across the network java extends the already established I/O interface by introducing an n package called java .net. Here are details of some important Java networking classes: InetAddress: This class is used to encapsulate both IP and DNS addresss. It has no visible constructors so to create an InetAddress object we use factory methods. Three commonly used InetAddress factory methods are: Static InetAdress getByName(String hostName) Static InetAdress getLocalHost(String host Name) InetAdress [ ] getAllByName(String host Name) If these methods are unable to resolve the host address they throw an UnknownHostException. ServerSocket(for TCP/IP) : This class is used to create servers that listen for either local or remote client programs to connect to them published ports. The constructors of ServerSocket reflect the port number for connections and queue time. Here the constructors: ServerSocket(int port) ServerSocket(int port, int maxQueue) ServerSocket(int port, int maxQueue, InetAddress localAddress)
27

28

The last constructer creates a server socket on the specified port with a maximum queue length of maxQueue .localAddress specifies the IP address to which this socket is binds on a multihomed host. Default Queue length is 52.serverSocket has a method called accept(), that wait for initiate communications and return with a normal socket that is used for communication. ClientSockets(for TCP/IP) : This class is used to create connection between client and server. There are two constructors used to create client socket.: Socket(String hostname ,int port) Socket(InetAddress.ipAddress, int port) The first constructer creates a socket connecting the local host to the named host and port. And second constructer creates a socket using a preexisting InetAddres object and a port. These can throw an IOException. A socket can be examined at any time for the address and the port information associated with it by the use of InetAddress getInetAddress() , int getPort() and int getLocalPort(). A socket can also be examined to gain access to the input and output stream coupled with it by using InputStream.getInputStream() and OutputStream.getOutputStream().

5. Design & Implementation


Modules in the Project
1. Baseserver

Server will always wait in a loop for the connection from a client. server will have a read thread for each individual client which will continuously read the connection generated at client side. To send a message to a particular client or broadcast the message server will have a send thread.
28

29

Server will keep a user list in a hashtable and add or remove the client name from the list as he log in or log off from the chat and broadcast that new list to all connected clients. Methods used : readUserList(); addClient(); removeClient(); getUser(); broadCastUser(); broadRemovedUser(); broadCastMessage();
2. Socketserver

It extend the base server class. It will generate a server type socket at a particular port. Later the client sockets will also be generated using same port number. Main method has been implemented in this class. Methods used : handleBroadcast(); handleGetUserList(); displayMessage();
3. Baseclient

It will find the address of the server machine and generate a

client

type socket at the same port number. Two threads will be generated to read and send the message to and from server. Communication between clients will be made through server. Client can not communicate directly to a client. When chatclient class is executed using a name as the argument it will generate a chat window for that client. It receive the user list from the server
29

30

and add it in the right side of chat window at the specific area. When a client wants to send the message to a particular client it selects that client from the user list and send the message to server along with address of that client and its own address also. In the case of broadcasting the message , message is sent to the server without having the destination address. Methods used : displayMessage(); buildLayout(); clearUser(); addUser();
4. Socketclient

It extends the baseclient class and generates two sockets at the client side one for receiving and another for sending.

6. User Manual
To make the communication among humans on computer various option have been added to the chat server which make the sending /receiving messages more efficient. Here are the details of option available to users. CREATION OF CLIENT: The creation of client can be achieved using .bat file for client or through the command prompt at any system over Local Area Network.

30

31

MESSAGE SENDING: The user can send message to another user present in the available online list. For this, first he will select a user from the list , then he will write a message in text area provided at his frame, then pressing SEND PRIVATE button, he can send the message to the selected user.

31

32

MESSAGE BROADCASTING: If user wants to broadcast the message then after writing the message he should press SEND TO ALL button, he can broadcast the message.

32

33

33

34

7. Conclusion
Minor Project is a very important part of the curriculum as it strengthens the concept and enhances knowledge. This project helped me learn a lot. While deciding the project, we wanted to make something in java.Then to our idea ,comes the concept of socket programming through java. This project was made to enhance our knowledge in the field of java, as it make use of core java & java networking. Also java provide a easy and attractive GUI to deal with. Also programming is not as complex as others as pointers are not included. More features could be added to this project to enhance the features of this project. As soon as well get time well try to add some more features to the project.

8. References
HERBERT SCHILDT, The Complete Java Reference Java 2, Tata McGraw-Hill Publishing Company Limited, New Delhi, 2002. ANDREW S. TANENBAUM, Computer Networks, Prentice -Hall of India Private Limited, New Delhi, 1999. ELLIOTE RUSTY HAROLD, Java Network Programming, OReilly Publishing Company Limited, New York, 1997. CAY S. HORSTMANN & GRAY CORNELL, Core Java 2

34

Vous aimerez peut-être aussi