Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java Networking Thread

  Asked By: Kuhaylah    Date: Jan 31    Category: Java    Views: 881
  

I need some help in java. If you know, I really appriciated.
Here is my question .

This problem is from Book Deitel&Deitel Fourth Edition Chap # 17
Exercise # 17.17

Q : Multthreaded server are quite popular today, especially becaues
of the incresing use of multiprocessing servers. modify the simple
server application presented in section 17.6 to be a multithreaded
server. then use several client application and have each of them
connect to the server simultaneously, Use a Vectory to store teh
client threads.

The question I am having is

( 1 ) Do I have to make Thread of Server.java or Client. java

Any help would be helpful...

Here is 17.6 exercise

// Fig. 17.4: Server.java
// Set up a Server that will receive a connection
// from a client, send a string to the client,
// and close the connection.

// Java core packages
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;


// Java extension packages
import javax.swing.*;

public class Server extends JFrame{
private JTextField enterField;
private JTextArea displayArea;
private ClientChat client[] ;
private ServerSocket server;
// private Socket connection;
private int counter = 0;

// set up GUI
public Server()
{
super( "Server" );

Container container = getContentPane();

client = new ClientChat[2] ;
counter = 0 ;
try {
server = new ServerSocket( 5000, 2 );
}
catch ( IOException iException )
{
iException.printStackTrace();
System.exit( 1 ) ;
}


// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( false );

enterField.addActionListener(

new ActionListener() {

// send message to client
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
}

} // end anonymous inner class

); // end call to addActionListener

container.add( enterField, BorderLayout.NORTH );

// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );


setSize( 300, 150 );
setVisible( true );
} // end of start method

public void execute()
{
client[0] = new ClientChat( server.accept(), 1 );
client[0].start();
client[1] = new ClientChat( server.accept(), 2 );
client[1].start();
}
private void sendData( String message )
{
// send object to client
try {
// output.writeObject( "SERVER>>> " + message );
// output.flush();
// displayArea.append( "\nSERVER>>>" + message );
}

// process problems sending object
catch ( IOException ioException ) {
displayArea.append( "\nError writing object" );
}
}
// execute application
public static void main( String args[] )
{
Server application = new Server();

application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );

application.execute();
}

private class ClientChat extends Thread {
private Socket connection1 ;
private ObjectInputStream input ;
private ObjectOutputStream output ;
private int chatplayers ;

public ClientChat( Socket socket1, int number1 )
{
chatplayers = number1 ;
connection1 = socket1 ;

try {
input = new ObjectInputStream( connection1.getInputStream
() ) ;
output = new ObjectOutputStream( connection1.getOutputStream
() );
}
catch ( IOException iException )
{
iException.printStackTrace() ;
System.exit(1 );
}
} // constructor.


// set up and run server

public void run()
{
// set up server to receive connections;
// process connections
try {

// Step 1: Create a ServerSocket.
// server = new ServerSocket( 5000, 100 );

while ( true ) {

// Step 2: Wait for a connection.
waitForConnection();

// Step 3: Get input and output streams.
getStreams();

// Step 4: Process connection.
processConnection();

// Step 5: Close connection.
closeConnection();

++counter;
}
}

// process EOFException when client closes connection
catch ( EOFException eofException ) {
System.out.println( "Client terminated connection" );
}

// process problems with I/O
catch ( IOException ioException ) {
ioException.printStackTrace();
}
} // end of Run

// wait for connection to arrive, then display connection info
private void waitForConnection() throws IOException
{
displayArea.setText( "Waiting for connection\n" );

// allow server to accept a connection

displayArea.append( "Connection " + counter +
" received from: " +
connection1.getInetAddress().getHostName() );
}

// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream(
connection1.getOutputStream() );

// flush output buffer to send header information
output.flush();

// set up input stream for objects
input = new ObjectInputStream(
connection1.getInputStream() );

displayArea.append( "\nGot I/O streams\n" );
}

// process connection with client
private void processConnection() throws IOException
{
// send connection successful message to client
String message = "SERVER>>> Connection successful";
output.writeObject( message );
output.flush();

// enable enterField so server user can send messages
enterField.setEnabled( true );

// process messages sent from client
do {

// read message and display it
try {
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(
displayArea.getText().length() );
}

// catch problems reading from client
catch ( ClassNotFoundException classNotFoundException ) {
displayArea.append( "\nUnknown object type received" );
}

} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
}

// close streams and socket
private void closeConnection() throws IOException
{
displayArea.append( "\nUser terminated connection" );
enterField.setEnabled( false );
output.close();
input.close();
connection1.close();
}

// send message to client
// }

}// end of Clientchat class
} // end class Server

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Java Networking Thread Or get search suggestion and latest updates.




Tagged: