Simple Chat Program In Java Computer ProjectorsBuilding an Internet chat system. You may have seen one of the many Java- based chat systems that have popped up on the Web. Compiling & Running a Simple Program : Training Index. Contact and Chat US Sales. This is a simple chat program using WCF with NetTcp binding. Simple chat application for ASP.NET. Computer Science Project Topics,Computer Science Project Ideas,Computer Science Project,Computer Project. Free Project Download. This is Chat program Java. Chat Program two way communication Java. A Java Chat Application. Chat History is not complete. The project's main purpose was to demonstrate networking. I tried to run this program in our computer. After reading this article, you'll understand how they work - - and know how to build a simple chat system of your own. This simple example of a client/server system is intended to demonstrate how to build applications using just the streams available in the standard API. The chat uses TCP/IP sockets to communicate, and can be embedded easily in a Web page. For reference, we provide a sidebar explaining Java network programming components that are relevant to this application. If you're still getting up to speed, take a look at the sidebar first. If you're already well- versed in Java, though, you can jump right in and simply refer to the sidebar for reference. We start with a simple graphical chat client. It takes two command- line parameters - - the server name and the port number to connect to. It makes a socket connection and then opens a window with a large output region and a small input region. The Chat. Client interface. After the user types text into the input region and hits Return, the text is transmitted to the server. The server echoes back everything that is sent by the client. The client displays everything received from the server in the output region. When multiple clients connect to one server, we have a simple chat system. Class Chat. Client. This class implements the chat client, as described. This involves setting up a basic user interface, handling user interaction, and receiving messages from the server. Chat. Client extends Frame implements Runnable . We implement the Runnable interface so that we can start a Thread that receives messages from the server. The constructor performs the basic setup of the GUI, the run() method receives messages from the server, the handle. Event() method handles user interaction, and the main() method performs the initial network connection. The Chat. Client communicates over the specified streams; we create buffered data streams i and o to provide efficient higher- level communication facilities over these streams. We then set up our simple user interface, consisting of the Text. Areaoutput and the Text. Fieldinput. We layout and show the window, and start a Threadlistener that accepts messages from the server. When a String arrives, we append it to the output region and repeat the loop. An IOException could occur if the connection to the server has been lost. In that event, we print out the exception and perform cleanup. Note that this will be signalled by an EOFException from the read. UTF() method. To clean up, we first assign our listener reference to this Thread to null; this indicates to the rest of the code that the thread has terminated. We then hide the input field and call validate() so that the interface is laid out again, and close the Output. Streamo to ensure that the connection is closed. Note that we perform all of the cleanup in a finally clause, so this will occur whether an IOException occurs here or the thread is forcibly stopped. We don't close the window immediately; the assumption is that the user may want to read the session even after the connection has been lost. Event (Event e) . When we catch this event, we write the message to the output stream, then call flush() to ensure that it is sent immediately. The output stream is a Data. Output. Stream, so we can use write. UTF() to send a String. If an IOException occurs the connection must have failed, so we stop the listener thread; this will automatically perform all necessary cleanup. The second event is the user attempting to close the window. It is up to the programmer to take care of this task; we stop the listener thread and hide the Frame. String args. Creating the socket may throw an exception that will exit this method and be displayed. Building a multithreaded server. We now develop a chat server that can accept multiple connections and that will broadcast everything it reads from any client. It is hardwired to read and write Strings in UTF format. There are two classes in this program: the main class, Chat. Server, is a server that accepts connections from clients and assigns them to new connection handler objects. The Chat. Handler class actually does the work of listening for messages and broadcasting them to all connected clients. One thread (the main thread) handles new connections, and there is a thread (the Chat. Handler class) for each client. The chat system framework. Every new Chat. Client will connect to the Chat. Server; this Chat. Server will hand the connection to a new instance of the Chat. Handler class that will receive messages from the new client. Within the Chat. Handler class, a list of the current handlers is maintained; the broadcast() method uses this list to transmit a message to all connected Chat. Clients. This class is concerned with accepting connections from clients and launching handler threads to process them. Chat. Server . We supply a constructor that performs all of the actual work for the class, and a main() method that actually starts it. We create a Server. Socket and then sit in a loop accepting clients with the accept() method of Server. Socket. For each connection, we create a new instance of the Chat. Handler class, passing the new Socket as a parameter. After we have created this handler, we start it with its start() method. This starts a new thread to handle the connection so that our main server loop can continue to wait on new connections. String args. This is the port to which clients will connect. Vector. import java. Chat. Handler extends Thread . The constructor accepts a Socket to which we attach; the run() method, called by the new thread, performs the actual client processing. Again, we use buffered data streams; these provide us with efficient I/O and methods to communicate high- level data types - - in this case, Strings. Vector handlers = new Vector (). First we add our thread to the Vector of Chat. Handlers handlers. The handlers. Vector keeps a list of all of the current handlers. It is a static variable and so there is one instance of the Vector for the whole Chat. Handler class and all of its instances. Thus, all Chat. Handlers can access the list of current connections. Note that it is very important for us to remove ourselves from this list afterward if our connection fails; otherwise, all other handlers will try to write to us when they broadcast information. This type of situation, where it is imperative that an action take place upon completion of a section of code, is a prime use of the try .. When the loop exits, whether because of an exception reading from the client or because this thread is stopped, the finally clause is guaranteed to be executed. In this clause, we remove our thread from the list of handlers and close the socket. String message) . We first synchronize on the list of handlers. We don't want people joining or leaving while we are looping, in case we try to broadcast to someone who no longer exists; this forces the clients to wait until we are done synchronizing. If the server must handle particularly heavy loads, then we might provide more fine- grained synchronization. Within this synchronized block we get an Enumeration of the current handlers. The Enumeration class provides a convenient way to iterate through all of the elements of a Vector. Our loop simply writes the message to every element of the Enumeration. Note that if an exception occurs while writing to a Chat. Client, then we call the client's stop() method; this stops the client's thread and therefore performs the appropriate cleanup, including removing the client from handlers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |