Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class SERVER extends JFrame {
- //JAVA NETWORK CHAT BY BEN BOLLINGER
- private JTextField userText;
- private JTextArea chatWindow;
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private ServerSocket server;
- private Socket connection;
- //CONSTRUCTOR
- public SERVER(){
- super("SERVER TEST");
- userText = new JTextField();
- userText.setEditable(false);
- userText.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent event){
- sendMessage(event.getActionCommand()); //send data from text
- userText.setText(""); //reset text
- }
- }
- );
- add(userText, BorderLayout.NORTH);
- chatWindow = new JTextArea();
- add(new JScrollPane(chatWindow));
- setSize(300,150);
- setVisible(true);
- }
- //SET UP AND RUN SERVER
- public void startRunning(){
- try{
- server = new ServerSocket(6789, 100); //set port to 6789, and set limit to 100
- while(true){
- try{
- waitForConnection();
- setupStreams();
- whileChatting();
- }catch(EOFException eofException){
- showMessage("\n SERVER ENDED THE CONNECTION! ");
- }finally{
- closeChat();
- }
- }
- }catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- //WAIT FOR CONNECTION AND DISPLAY INFO
- private void waitForConnection() throws IOException{
- showMessage("WAITING FOR SOMEONE TO CONNECT... \n");
- connection = server.accept(); //accept socket
- showMessage(" NOW CONNECTED TO " + connection.getInetAddress().getHostName());
- }
- //GET STREAM TO SEND + RECIEVE DATA
- private void setupStreams() throws IOException{
- output = new ObjectOutputStream(connection.getOutputStream());
- output.flush();
- input = new ObjectInputStream(connection.getInputStream());
- showMessage("\n Streams are now setup! \n");
- }
- //DURING CHAT CONVERSATION
- private void whileChatting() throws IOException{
- String message = " You are now connected! ";
- sendMessage(message);
- ableToType(true);
- do{
- try{
- message = (String) input.readObject();
- showMessage("\n" + message);
- }catch(ClassNotFoundException classNotFoundException){
- showMessage("\n ERROR: OBJECT UNABLE TO CONVERT TO DATA.STRING");
- }
- }while(!message.equals("CLIENT - END"));
- }
- //CLOSE STREAMS AND SOCKETS
- private void closeChat(){
- showMessage("\n Closing connections... \n");
- ableToType(false);
- try{
- output.close();
- input.close();
- connection.close();
- }catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- //SEND MESSAGE TO CLIENT
- private void sendMessage(String message){
- try{
- output.writeObject("SERVER - " + message);
- output.flush();
- showMessage("\nSERVER - " + message);
- }catch(IOException ioException){
- chatWindow.append("\n ERROR: UNABLE TO SEND MESSAGE");
- }
- }
- //UPDATE CHAT WINDOW
- private void showMessage(final String text){
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run(){
- chatWindow.setEditable(false);
- chatWindow.append(text);
- }
- }
- );
- }
- //ALLOW USER TO TYPE
- private void ableToType(final boolean tof){
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run(){
- userText.setEditable(tof);
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement