1 / 11

Field Trip #25

Learn how to create a client/server chat application in Java from scratch. Understand JFrame, events, sockets, server, threads, and streams for successful communication. Practice by making a simple chat server with the provided step-by-step instructions.

ejacob
Download Presentation

Field Trip #25

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating a Client/Server By Keith Lynn Field Trip #25

  2. JFrame • To contain the contents of the Excel document, we will create a JFrame • A JFrame is a graphical interface • It can contains components that hold the data of the spreadsheet • It can also contain a menu bar which gives an interface to the user

  3. JButton • A Jbutton is a graphical component • We can place text on a Jbutton • A Jbutton has a margin so some characters won't be displayed • We can change the margin by obtain an Insets object and setting left and right to 0 and then applying it to the button

  4. Events • We can detect events like button clicks • We attach a listener to a component • When the event takes place, the listener is called • For button clicks, we create an ActionListenere • When the button is clicked, the method actionPerformed is called

  5. JTextComponent • JTextComponents allow us to place components on a JFrame where we can place text • A simple JTextComponent is JtextField • We can use a JTextField to hold small amounts of text • A JTextArea can contain a large amount of text • We typically place a JTextArea inside a JScrollPane

  6. Socket • A Socket allows communication between two machines • We can read from and write to the Socket by obtaining instances of its input stream and output stream with the methods getInputStream() and getOutputStream()

  7. SocketServer • The ServerSocket runs on a machine designated as the server • The ServerSocket waits for requests for connections and when a client connects a Socket is created • The client uses that socket to communicate with the server

  8. Thread • A Thread is a single line of execution • Once a Socket has been created for communication, we will use a thread to continuously send and receive information • In order to stop the communication, we will run the thread in a loop • We will control that loop with a variable • When we want the thread to stop, we change the variable

  9. Streams • We can read from the input stream using the class BufferedReader • We can read the next line with the method readLine() • In order to write to the output stream, we use a PrintWriter • We use the method write to the write to the stream • After we write text, we call the flush method on PrintWriter

  10. Creating a Simple Chat Server • To create a simple chat server, we first create a ServerSocket and pick a port for it to listen to • We wait for an incoming request from a client • When we receive one, we create a thread that will constantly read from and write to the socket • On the client side, once the connection has been established, we create a thread that constantly reads from and writes to the Socket

  11. Chat Server, cont'd • The client will simply display what the server sends • On the other hand, the server will accept what each client sends, and then send that information to all connected clients

More Related