28-09-2013, 04:56 PM
CHAT SERVER
CHAT SERVER.docx (Size: 29.46 KB / Downloads: 23)
Objective:
In this Minor project, we will build both the server and clients sides of a simple chat system. After this project one can know that with little or no experience in a programming language there is a possibility of making a classy project known as a Chat Server. In this project we will make detailed use of multithreading and networking and by the end of this project all the team members who are giving their contribution in the making of this project will be having deep of knowledge of these topics.
Introduction :
In this project we will build a simple centralized, connection-oriented Java Server. In doing so, we’ll learn a basic framework that you can use when creating such a server, using time-honoured techniques that work well in many situations. We’ll also examine some of the framework and explore the ways of getting around them.
Description :
Connection oriented server:
Generally speaking, the job of any server is to provide a centralized service. However, there are many different ways of providing services, and many different ways to structure the communications.
Chat is roughly described as a connection-oriented service, because a user establishes a connection and maintains that connection, sending and receiving text for the duration of the session.
This is in contrast to the Web, where the protocol is (at least in theory) transactional – the browser asks for a page, and the server sends it; the connection is then closed. (In practice, the connection is kept open and reused, but this is more a speed-optimization than a structuring metaphor.) We'll be creating a stripped-down, connection-oriented server. Learning the basic framework will help you a great deal in creating other connection-oriented servers in the future .
Why create from scratch?
In creating this prototype server, we'll be using nothing more than the basic packages built into every Java implementation. This allows us to explore server programming at the very lowest level possible in the Java language.There are certainly many systems available that can take care of many of these networking details for you. In many cases, the best real-world solution is to use an existing framework, because it often provides useful features such as fault-tolerance, load-balancing, and sessioning. It is, nevertheless, crucial to understand how these things work at the lowest level. No existing solution is perfect for all problems, and existing solutions often have minor flaws that your code must work around. Simply choosing the right pre-packaged solution takes a discriminating eye that has been educated as to the tradeoffs inherent in various techniques.
Why a framework?
The word framework has a technical definition within the object-oriented community – it means a design structure that can be reused.
This is not the same as traditional code reuse, in which bits of code written for one purpose (or for no particular purpose at all) are reused for a new purpose. Rather, a framework is a reusable, overarching structure inside which you can implement your particular application. One way to think of the difference between a framework and traditional code reuse is that traditional reuse involves inserting pre-existing elements into your particular structure, while a framework involves inserting your particular elements into a pre-existing framework.The code that we'll develop in this tutorial isn't technically a framework, in that it is not structured to allow you to easily repurpose it for something else. Structuring it that way would distract from the purpose of this tutorial, which is to explore basic techniques of server programming.
However, in a design sense, it is a framework because the structure of the code is something you'll be able to use again and again.
Analysis:
Take care of all networking responsibilities on its own. As we'll see, this isn't very hard. Listening on a port The first thing we have to do is to get ready to receive incoming connections. To do this, we must listen on a port.
A port can be thought of as an address within a single computer. Remember that often a single machine might serve as a Web server, a chat server, an FTP server, and several other kinds of servers at the same time. Because of this, a connection to a server needs to specify not only the address of the machine itself, but also the particular service within the machine. This internal address is a port and is represented by a single integer between 1 and 65535. Many standard services have a dedicated port number. For example, telnet uses port 23, FTP uses ports 20 and 21, and Web servers, by default, use port 80. Since our chat system is not famous (yet), we're going to have to use one of the port numbers allocated for general use.
We'll use port 5000. This means that our server is going to listen for connections on port 5000. Our clients, when connecting to our server machine, will specify that they want to connect to port 5000 on our server machine. This way, our clients and our server will be able to talk. Sockets Our communications between client and server will pass through a Java object called a Socket. Sockets are not at all Java-specific; the term is taken directly from the terminology of general IP (Internet Protocol) network programming. In Java programs, a Socket object is simply a wrapper around the low-level sockets that Internet programmers have been using for years. And the abstraction used by the Java language is very clean, so socket programming in the Java language is much more pleasant than it is, for example, in C. The most important thing to know about a Socket object is that it contains (among other
things) two Streams. One is for reading data coming in, and the other is for writing data out. That is to say, a Socket has an InputStream and an OutputStream.
The While-Accept loop (Server side)
We're ready to start accepting network connections from our clients. Here's how the chat is going to work. We mentioned above that the Java language provides an object called a Socket, which represents a connection to a program somewhere else and through which data can pass. But how do we get this socket in the first place? A client, almost by definition, initiates the connection to a server. Therefore, the first job of a server is to wait for a connection to come in. That is, we need something to give us the Sockets that are connected to our clients. That's where the ServerSocket comes in. This is an object whose job is simple: listen on a port and when a new connection comes in, create a Socket that represents that new connection.
Accepting Sockets Remember that your program will potentially be serving many clients from all over the Internet. And these clients will be connecting to your server without regard to each other. That is, there's no way to control the order, or the timing, with which the connections are arriving. As we'll see later, multithreading is an excellent way to deal with these connections once they have come in.
Serialization of incoming requests
Serialization is a useful way, in general, to deal with things that are happening simultaneously. A potential drawback, however, is that it can remove parallelism. That is to say, serialization can prevent us from saving time by working on multiple things at the same time. In the code above, while the program is dealing with one connection, other connections might be piling up. But serialization is not a problem for us because each time a connection comes in, we're going to create a new thread to deal with it. Once the new thread is created, it can go off and deal with the new connection, and our while-accept loop can go back to accepting new connections. If the act of creating this new thread is fast enough, then the connections won't pile up.
The While-Read/Write loop (Server side)
The communications protocol
Now that we've gotten to the point where we're going to be talking to the client, we should talk a little bit about our communication protocol. Every client/server system has a communications protocol, which is nothing more than the format you use to send the data back and forth. The protocol can be so simple it hardly deserves the title of protocol, or it can be a sophisticated standard that has been ratified by consortia all over the world. Either way, it's a protocol. We're going to create our own protocol, because in the Java language it's very easy to do, and because there's little for us to gain from using an existing standard. Our protocol will be very simple.
The Java language has a pair of extremely useful classes called DataInputStream and
DataOutputStream. These classes allow you to read and write low-level data objects (like integers and strings) to a stream, without having to consider the format in which they are written. Because these classes use the same format, and because this format doesn't change, you can be sure that an integer written to a DataOutputStream will be properly read from the DataInputStream at the other end.