with my java code i cant figure were to put the Port: 2016 Client class and have
ID: 3573441 • Letter: W
Question
with my java code i cant figure were to put the Port: 2016 Client class and have the SERVER: fa16-cop3330.hpc.lab in clientTest class to be able to connect to my teacher's server to get a respones. it run and saids "Attempting connection" then does nothing ater that. but it will let you type somthing after and still no respones
public class Client extends JFrame
{
private JTextField enterField; // enters information from user
private JTextArea displayArea; // display information to user
private ObjectOutputStream output; // output stream to server
private ObjectInputStream input; // input stream from server
private String message = ""; // message from server
private String chatServer; // host server for this application
private Socket client; // socket to communicate with server
// initialize chatServer and set up GUI
public Client( String host )
{
super( "Client" );
chatServer = host; // set server to which this client connects
enterField = new JTextField(); // create enterField
enterField.setEditable( false );
enterField.addActionListener(
new ActionListener()
{
// send message to server
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
enterField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
add( enterField, BorderLayout.NORTH );
displayArea = new JTextArea(); // create displayArea
add( new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 300, 150 ); // set size of window
setVisible( true ); // show window
} // end Client constructor
// connect to server and process messages from server
public void runClient()
{
try // connect to server, get streams, process connection
{
connectToServer(); // create a Socket to make connection
getStreams(); // get the input and output streams
processConnection(); // process connection
}//endtry
catch ( EOFException eofException )
{
displayMessage( " ROBERT_HORTON terminated connection" );
} // end catch
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
finally
{
closeConnection(); // close connection
} // end finally
}//end method runClient
// connect to server
private void connectToServer() throws IOException
{
displayMessage( "Attempting connection " );
// create Socket to make connection to server
client = new Socket( InetAddress.getByName( chatServer ), 12345 );
// display connection information
displayMessage( "Connected to: " +
client.getInetAddress().getHostName() );
} // end method connectToServer
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream( client.getOutputStream() );
output.flush(); // flush output buffer to send header information
// set up input stream for objects
input = new ObjectInputStream( client.getInputStream() );
displayMessage( " Got I/O streams " );
} // end method getStreams
// process connection with server
private void processConnection() throws IOException
{
// enable enterField so client user can send messages
setTextFieldEditable( true );
do // process messages sent from server
{
try // read message and display it
{
message = ( String ) input.readObject(); // read new message
displayMessage( " " + message ); // display message
} // end try
catch ( ClassNotFoundException classNotFoundException )
{
displayMessage( " Unknown object type received" );
} // end catch
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
} // end method processConnection
// close streams and socket
private void closeConnection()
{
displayMessage( " Closing connection" );
setTextFieldEditable( false ); // disable enterField
try
{
output.close(); // close output stream
input.close(); // close input stream
client.close(); // close socket
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
} // end method closeConnection
// send message to server
private void sendData(String message)
{
try //send object to sever
{
output.writeObject( "ROBERT_HORTON>>> " + message );
output.flush(); // flush data to output
displayMessage( " ROBERT_HORTON>>> " + message );
}// end try
catch (IOException ioException)
{
displayArea.append( " Error writing object" );
} // end catch
} // end method sendData
// manipulates displayArea in the event-dispatch thread
private void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // updates displayArea
{
displayArea.append( messageToDisplay );
} // end method run
} // end anonymous inner class
); // end call to SwingUtilities.invokeLater
} // end method displayMessage
// manipulates enterField in the event-dispatch thread
private void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // sets enterField's editability
{
enterField.setEditable( editable );
} // end method run
} // end anonymous inner class
); // end call to SwingUtilities.invokeLater 187
} // end method setTextFieldEditable
} // end class Client
public class ClientTest
{
public static void main( String[] args )
{
Client application; // declare client application
// if no command line args
if ( args.length == 0 )
application = new Client( "fa16-cop3330.hpc.lab" ); // connect to server
else
application = new Client( args[ 0 ] ); // use args to connect
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.runClient(); // run client application
} // end main
} // end class ClientTest
Explanation / Answer
Answer: first run the server class and then client class. and do not use default constructor while creating server in server class.
create server like this.
As client is trying to connect to 12345 port
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.