Compare commits

12 Commits

Author SHA1 Message Date
Simon O'Shea 541c932e4e Delete loginDEMO.class 2021-01-05 15:00:00 -05:00
Simon O'Shea 15bfb626fc Delete loginDEMO$2.class 2021-01-05 14:59:53 -05:00
Simon O'Shea 030f4ecfb0 Delete loginDEMO$1.class 2021-01-05 14:59:46 -05:00
Simon O'Shea adc93d1da4 Delete java chat.jar 2021-01-05 14:59:16 -05:00
Simon O'Shea dfd3e8c8f0 Add files via upload
cleaning up
2021-01-05 14:58:41 -05:00
Simon O'Shea 21fcc670e8 Add files via upload 2021-01-05 14:58:07 -05:00
Simon O'Shea ba4924b8d6 Delete java chat DEMO.jar 2021-01-05 14:57:15 -05:00
Simon O'Shea 0f3c0a5364 Create README.md 2021-01-05 14:47:26 -05:00
O'Shea cca61c9f56 Final implementation, ready for presentation! 2020-12-10 14:03:54 -05:00
O'Shea 690bbba955 working v2.0, attempt at GUI 2020-12-07 19:36:36 -05:00
O'Shea b7ff7847e9 working v 1.0 2020-12-05 17:11:53 -05:00
O'Shea 54b8ea3ac5 initial commit 2020-11-29 15:32:33 -05:00
72 changed files with 1364 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-13.0.1">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>javaChat</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
+14
View File
@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=12
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=12
+17
View File
@@ -0,0 +1,17 @@
# javaChat
Simon O'Shea and Jacob Gohring's Network Programming (COMP-2100-05) Final Project
javaChat is a barebones group chat application reminiscent of an IRC.
To host a chat on your own, the only preresiquites are that you have JDK 13 or above, have your router's port 6789 forwarded, and make a firewall rule for port 6789 to permit inbound and outbound access (unfortunately this was the only way I could get the data to transfer cross-network, am looking for another solution and would appreciate feedback).
********************************
To use:
Step 1: To host a server, open "java chat server.jar" (it will open and run silently in the background, check task manager under "background processes" for an "OpenJDK Platform Binary" process and kill it to end the server).
Step 2: To join the room, open the client "java chat.jar" and enter the IP address of the server. If you're running the server on the same machine, "localhost" will also work. Otherwise you just enter the public IP address of the person's computer that is hosting the server (or the LAN address if you're connected to the same router).
Step 3: Enter a username, hit enter, and chat away!
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+94
View File
@@ -0,0 +1,94 @@
package backend;
import java.net.* ;
import javax.swing.JFrame ;
import java.io.* ;
public class ChatClientCMD
{
/**
*
*
* @param args
* @throws IOException
*/
public static void main( String[] args ) throws IOException
{
// Get server IP
BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ;
System.out.print( "\nPlease enter the IP of the desired server: " ); // GUI: IP BOX
String serverIP = keyboard.readLine() ;
// Open your connection to a server, at port 6789
Socket socket = new Socket( serverIP , 6789 ) ;
// Create communication streams
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
// Establish connection, make user-name
System.out.print( "\nConnection established,\nPlease enter a username: " ) ;
String username = keyboard.readLine() ;
dataStreamOut.writeUTF( username ) ;
/////////////////////////
//// Program Running ////
/////////////////////////
// Thread for sending messages
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run()
{
System.out.print( "\n-= Type /help for a list of commands =-" ) ;
while( true )
{
try
{
String messageOut = keyboard.readLine() ;
dataStreamOut.writeUTF( messageOut ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
}) ;
// Thread for receiving messages
Thread receiveMessage = new Thread( new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
String messageIn = dataStreamIn.readUTF() ;
System.out.printf( "%n%s\n", messageIn ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
});
// Begin threads
sendMessage.start() ;
receiveMessage.start() ;
}
}
// end class ChatClient
Binary file not shown.
+309
View File
@@ -0,0 +1,309 @@
package backend ;
import java.io.DataInputStream ;
import java.io.DataOutputStream ;
import java.io.IOException ;
import java.net.ServerSocket ;
import java.net.Socket ;
import java.util.ArrayList ;
/**
* ChatServer class accepts Client requests to join
* and creates an individual thread for each client.
* The "brain" of the server.
*
* @author osheas1
* @author gohringj
* @version Semi-Final Draft
*/
public class ChatServer
{
// List of all active users in Server
public static ArrayList<ClientHandler> userList = new ArrayList<>() ;
/**
* @param args (unused)
* @throws IOException (is thrown if connection is unexpectedly closed)
*/
public static void main( final String[] args ) throws IOException
{
// Register service on port 6789
ServerSocket serverSocket = new ServerSocket( 6789 ) ;
Socket socket = null ;
System.out.println( "\nServer established. Awaiting connections..." ) ;
// Loop to accept client requests
while ( true )
{
socket = serverSocket.accept() ;
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
System.out.println( "\nClient connection detected, requesting username..." ) ;
String username = dataStreamIn.readUTF() ;
ClientHandler handle = new ClientHandler( socket, username, dataStreamOut, dataStreamIn ) ;
System.out.println( "\nCreating new thread...\n" ) ;
Thread thread = new Thread( handle ) ;
userList.add( handle ) ;
thread.start() ;
}
}
} // end class ChatServer
/**
* ClientHandler class manages the actual messages being sent
* between the users. The "heart" of the server.
*
* @author osheas1
* @author gohringj
* @version Semi-Final Draft
*/
class ClientHandler implements Runnable
{
Socket socket ;
String username ;
DataOutputStream dataStreamOut ;
DataInputStream dataStreamIn ;
boolean loggedIn ;
int commandCheck ;
/**
* Constructor
* @param s = client socket
* @param user = client's user-name
* @param dos = data stream to client
* @param dis = data stream from client
*/
public ClientHandler( Socket s, String user, DataOutputStream dos, DataInputStream dis )
{
this.socket = s ;
this.username = user ;
this.dataStreamOut = dos ;
this.dataStreamIn = dis ;
this.loggedIn = true ;
}
@Override
public void run()
{
// Note the connection of the new user
String announcement = String.format( "\n>>> User \"%s\" has successfully connected (type /help for a list of commands) <<<\n", this.username ) ;
System.out.println( announcement ) ;
// Distribute announcement to entire server
sendMessage( announcement ) ;
// Message management
String message = "" ;
while ( true )
{
// Try-Catch block interprets incoming messages
try
{
message = this.dataStreamIn.readUTF() ;
if(message == "" || message == " ")
{
continue ;
}
}
catch ( IOException x )
{
x.printStackTrace() ;
}
// Check message for user commands, returns TRUE if user "/quit"s
if( commands( message, this ) )
{
break ;
}
// Print the message server-side
message = String.format( "%s: %s", this.username, message ) ;
System.out.println( message ) ;
// Distribute the message to all users
if ( this.commandCheck == 0 )
{
sendMessage( message ) ;
}
else
{
this.commandCheck = 0 ;
}
} // end while
// close streams when user disconnects
try
{
this.loggedIn = false ;
this.dataStreamIn.close() ;
this.dataStreamOut.close() ;
this.socket.close() ;
}
catch ( IOException x )
{
x.printStackTrace() ;
}
} // end run
/**
* The 'commands' method manages the usage of user-commands
* by distributing private messages, providing a user-list,
* etc.
*
* @param message = the command itself
* @param user = the user that
* @return = boolean that indicates that the user has disconnected
*/
public boolean commands( String message, final ClientHandler user )
{
final String[] commands = new String[] { "/help", "/quit", "/userlist", "/pm" } ;
try
{
// Quit command terminates client's connection, with an announcement
if ( message.equals( "/quit" ) )
{
this.commandCheck++ ;
String announcement = String.format( "\n-= \"%s\" has disconnected =- \n", user.username ) ;
System.out.println( announcement ) ;
sendMessage( announcement ) ;
return true ;
}
// User-List command provides the user a list of everyone else connected to the server
if ( message.equals( "/userlist" ) )
{
this.commandCheck++ ;
user.dataStreamOut.writeUTF( "> Current list of users in the server: " ) ;
for ( ClientHandler users : ChatServer.userList )
{
if ( !users.loggedIn )
{
continue ;
}
user.dataStreamOut.writeUTF( "> " + users.username ) ;
}
}
// Help command lists all available commands
if ( message.equals( "/help" ) )
{
this.commandCheck++ ;
user.dataStreamOut.writeUTF( "\n> All available commands: " ) ;
for ( String command : commands )
{
user.dataStreamOut.writeUTF( "> " +command ) ;
}
user.dataStreamOut.writeUTF( "\n" ) ;
}
// Private Message Command allows users to send messages to a single recipient
if ( message.contains( "/pm" ) )
{
this.commandCheck++ ;
user.dataStreamOut.writeUTF( "\n> Who would you like to private message?" ) ;
String recipient = this.dataStreamIn.readUTF() ;
user.dataStreamOut.writeUTF( "> \"" + recipient + "\"" ) ;
ClientHandler sendTo = null ;
// loop determines which ClientHandler object to send the message to
for ( ClientHandler users : ChatServer.userList )
{
if ( !users.loggedIn )
{
continue ;
}
else if ( users.username.equals( recipient ) )
{
sendTo = users ;
}
}
if ( sendTo == null )
{
user.dataStreamOut.writeUTF( "\n> User not found, please try again" ) ;
return false ;
}
user.dataStreamOut.writeUTF( "\n> What would you like to say?" ) ;
String privateMessage = this.dataStreamIn.readUTF() ;
user.dataStreamOut.writeUTF( "> sent: \"" + privateMessage + "\"" ) ;
sendPrivateMessage( privateMessage, user.username, sendTo ) ;
}
}
catch ( IOException x )
{
while ( ChatServer.userList.size() != 0 )
{
System.out.println( x ) ;
}
}
return false ;
} // end commands
// Convenience method to send messages privately
private static void sendPrivateMessage( String message, final String sentfrom, final ClientHandler sendto )
{
try
{
for ( ClientHandler users : ChatServer.userList )
{
if ( users.equals( sendto ) )
{
message = String.format( "\nFrom <%s>: %s", sentfrom, message ) ;
users.dataStreamOut.writeUTF( message ) ;
}
}
}
catch ( final IOException x )
{
x.printStackTrace() ;
}
}
// Convenience method to distribute messages to all clients
private static void sendMessage( String message )
{
try
{
for ( ClientHandler users : ChatServer.userList )
{
if ( !users.loggedIn )
{
continue ;
}
users.dataStreamOut.writeUTF( message ) ;
}
}
catch ( IOException x )
{
x.printStackTrace() ;
}
}
}
// end class ClientHandler
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+167
View File
@@ -0,0 +1,167 @@
package chatApp ;
@SuppressWarnings( "javadoc" )
public class ChatGUI extends javax.swing.JFrame {
public boolean messageReady = false ;
public String message ;
public String username ;
/*
public void giveTitle(String name)
{
this.username = name ;
setTitle("logged in as:" + name ) ;
}
*/
public String getMessageOut()
{
if( this.message.contentEquals( "" ) || this.message.contentEquals( " " ) )
{
this.messageReady = false ;
return null ;
}
if( this.messageReady )
{
this.messageReady = false ;
return this.message ;
}
return null ;
}
public void printMessage( String messageOut )
{
messageOut = String.format( "%s\n%s", this.jTextArea1.getText(), messageOut ) ;
this.jTextArea1.setText( null ) ;
this.jTextArea1.setText( messageOut ) ;
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
}
public ChatGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
setTitle("logged in as: " + this.username );
jButton1.setText("send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
setResizable(false);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setLineWrap( true );
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane2)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(50, 50, 50))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
{
if( this.jTextField1.getText() != null )
{
this.message = this.jTextField1.getText() ;
this.messageReady = true ;
this.jTextField1.setText( null ) ;
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if( this.jTextField1.getText() != null )
{
this.message = this.jTextField1.getText() ;
this.messageReady = true ;
this.jTextField1.setText( null ) ;
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+109
View File
@@ -0,0 +1,109 @@
package chatApp;
import java.net.* ;
import java.io.* ;
public class ClientGUI
{
static LoginGUI login = new LoginGUI() ;
static ChatGUI chatRoom = new ChatGUI() ;
@SuppressWarnings( "javadoc" )
public static void main( String[] args ) throws IOException
{
login.setVisible( true );
boolean done = false ;
while( !done )
{
done = login.isDone() ;
System.out.print("");
}
String ip = login.getIP() ;
String username = login.getUsername() ;
// Open your connection to a server, at port 6789
Socket socket = new Socket( ip , 6789 ) ;
// Create communication streams
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
dataStreamOut.writeUTF( username ) ;
chatRoom.setTitle( "logged in as: " + username ) ;
chatRoom.setVisible( true ) ;
// Thread for sending messages
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
// if-statement is only triggered if client closes window (disconnects)
if( !chatRoom.isVisible() )
{
dataStreamOut.writeUTF( "/quit" ) ;
dataStreamIn.close() ;
dataStreamOut.close() ;
socket.close() ;
System.exit( 0 ) ;
}
// extract message information from GUI text field
if( chatRoom.messageReady )
{
String messageOut = chatRoom.getMessageOut() ;
if( messageOut == null )
{
continue ;
}
dataStreamOut.writeUTF( messageOut ) ;
}
System.out.print("") ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
}) ;
// Thread for receiving messages
Thread receiveMessage = new Thread( new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
String messageIn = dataStreamIn.readUTF() ;
chatRoom.printMessage( messageIn ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
});
// Begin threads
sendMessage.start() ;
receiveMessage.start() ;
}
}
// end class ClientGUI
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+182
View File
@@ -0,0 +1,182 @@
package chatApp;
import java.awt.* ;
import java.util.* ;
import javax.swing.* ;
public class LoginGUI extends javax.swing.JFrame {
String ip ;
String username ;
boolean done ;
public String getIP()
{
return ip ;
}
public String getUsername()
{
return username ;
}
public boolean isDone()
{
return done ;
}
public LoginGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
ipBox = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("java chat login");
setAlwaysOnTop(true);
setResizable(false);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Enter Server's IP:");
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
ipBox.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setToolTipText("");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Enter Username:");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
jLabel3.setText("inputs must be at least one character");
jLabel3.setEnabled(false);
ipBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(43, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
.addComponent(ipBox))
.addGap(40, 40, 40))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ipBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addContainerGap(22, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt)
{
if( ipBox.getText().isBlank() )
{
jLabel3.setEnabled( true );
}
else
{
jTextField1.grabFocus();
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
{
if( jTextField1.getText().isBlank() || ipBox.getText().isBlank() )
{
jLabel3.setEnabled( true );
}
else
{
this.ip = ipBox.getText() ;
this.username = jTextField1.getText() ;
this.done = true ;
dispose();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField ipBox;
// End of variables declaration
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+166
View File
@@ -0,0 +1,166 @@
package demonstration;
@SuppressWarnings( "javadoc" )
public class ChatDEMO extends javax.swing.JFrame {
public boolean messageReady = false ;
public String message ;
public String username ;
/*
public void giveTitle(String name)
{
this.username = name ;
setTitle("logged in as:" + name ) ;
}
*/
public String getMessageOut()
{
if( this.message.contentEquals( "" ) || this.message.contentEquals( " " ) )
{
this.messageReady = false ;
return null ;
}
if( this.messageReady )
{
this.messageReady = false ;
return this.message ;
}
return null ;
}
public void printMessage( String messageOut )
{
messageOut = String.format( "%s\n%s", this.jTextArea1.getText(), messageOut ) ;
this.jTextArea1.setText( null ) ;
this.jTextArea1.setText( messageOut ) ;
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
}
public ChatDEMO() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
setTitle("logged in as: " + this.username );
jButton1.setText("send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
setResizable(false);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setLineWrap( true );
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane2)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(50, 50, 50))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
{
if( this.jTextField1.getText() != null )
{
this.message = this.jTextField1.getText() ;
this.messageReady = true ;
this.jTextField1.setText( null ) ;
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if( this.jTextField1.getText() != null )
{
this.message = this.jTextField1.getText() ;
this.messageReady = true ;
this.jTextField1.setText( null ) ;
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatDEMO().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
+110
View File
@@ -0,0 +1,110 @@
package demonstration;
import java.net.* ;
import java.io.* ;
@SuppressWarnings( "javadoc" )
public class ClientDEMO
{
static LoginDEMO loginDemo = new LoginDEMO() ;
static ChatDEMO chatRoom = new ChatDEMO() ;
@SuppressWarnings( "javadoc" )
public static void main( String[] args ) throws IOException
{
loginDemo.setVisible( true );
boolean done = false ;
while( !done )
{
done = loginDemo.isDone() ;
System.out.print("");
}
String ip = loginDemo.getIP() ;
String username = loginDemo.getUsername() ;
// Open your connection to a server, at port 6789
Socket socket = new Socket( ip , 6789 ) ;
// Create communication streams
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
dataStreamOut.writeUTF( username ) ;
chatRoom.setTitle( "logged in as: " + username ) ;
chatRoom.setVisible( true ) ;
// Thread for sending messages
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
// if-statement is only triggered if client closes window (disconnects)
if( !chatRoom.isVisible() )
{
dataStreamOut.writeUTF( "/quit" ) ;
dataStreamIn.close() ;
dataStreamOut.close() ;
socket.close() ;
System.exit( 0 ) ;
}
// extract message information from GUI text field
if( chatRoom.messageReady )
{
String messageOut = chatRoom.getMessageOut() ;
if( messageOut == null )
{
continue ;
}
dataStreamOut.writeUTF( messageOut ) ;
}
System.out.print("") ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
}) ;
// Thread for receiving messages
Thread receiveMessage = new Thread( new Runnable()
{
@Override
public void run()
{
while( true )
{
try
{
String messageIn = dataStreamIn.readUTF() ;
chatRoom.printMessage( messageIn ) ;
}
catch( IOException x )
{
x.printStackTrace() ;
System.exit( 0 ) ;
}
}
}
});
// Begin threads
sendMessage.start() ;
receiveMessage.start() ;
}
}
// end class ClientGUI
Binary file not shown.
Binary file not shown.
Binary file not shown.
+158
View File
@@ -0,0 +1,158 @@
package demonstration;
@SuppressWarnings( "javadoc" )
public class LoginDEMO extends javax.swing.JFrame {
/**
* Creates new form loginGUI
*/
public String ip ;
public String username ;
public boolean done ;
public String getIP()
{
return this.ip ;
}
public String getUsername()
{
return this.username ;
}
public boolean isDone()
{
return this.done ;
}
public LoginDEMO() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("java chat login DEMO");
setAlwaysOnTop(true);
setResizable(false);
jPasswordField1.setEditable(false);
jPasswordField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jPasswordField1.setText("12.34.56.78");
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Enter Server's IP:");
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setToolTipText("");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Enter Username:");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
jLabel3.setText("username must be at least one character");
jLabel3.setEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(43, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
.addGap(40, 40, 40))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addContainerGap(22, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
this.ip = "74.69.251.12" ;
if( jTextField1.getText().isBlank() )
{
jLabel3.setEnabled( true );
}
else
{
this.username = jTextField1.getText() ;
this.done = true ;
dispose();
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginDEMO().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
+11
View File
@@ -0,0 +1,11 @@
/**
*
*/
/**
* @author osheas1
*
*/
module javaChat
{
requires java.desktop;
requires java.logging;}