110 likes | 120 Views
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.
E N D
Creating a Client/Server By Keith Lynn Field Trip #25
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
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
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
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
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()
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
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
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
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
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