希望看的懂的人
能给我一些注解
非常感谢
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int counter = 1;
// set up GUI
public Server()
{
super( "Server" );
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEditable( false );
enterField.addActionListener(
new ActionListener() {
// send message to client
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
enterField.setText( "" );
}
}
);
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),
BorderLayout.CENTER );
setSize( 300, 150 );
setVisible( true );
} // end Server constructor
// set up and run server
public void runServer()
{
// set up server to receive connections; process connections
try {
// Step 1: Create a ServerSocket.
server = new ServerSocket( 12345, 100 );
while ( true ) {
try {
waitForConnection(); // Step 2: Wait for a connection.
getStreams(); // Step 3: Get input & output streams.
processConnection(); // Step 4: Process connection.
}
// process EOFException when client closes connection
catch ( EOFException eofException ) {
System.err.println( "Server terminated connection" );
}
finally {
closeConnection(); // Step 5: Close connection.
++counter;
}
} // end while
} // end try
// proc ..
访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容