It's basically a simple email type app. I'm not trying to multithread it yet i just want to learn the basics.
Client code
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client2 extends JFrame {
private static InetAddress host; //........
private JTextField enter;
private JTextArea display, saved;
ObjectOutputStream output;
ObjectInputStream input;
String message = "";
JButton deleteBtn;
public Client2()
{
super( "Client" );
enter = new JTextField(400);
enter.setEnabled( false );
enter.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
sendData( e.getActionCommand() );
}
}
);
add( enter, BorderLayout.NORTH );
display = new JTextArea();
add( new JScrollPane( display ),
BorderLayout.CENTER );
setSize( 600, 300 );
setVisible(true);
add( new JButton( "Delete"), BorderLayout.EAST);
saved = new JTextArea();
add( new JScrollPane( display ),
BorderLayout.SOUTH );
setSize( 600, 300 );
setVisible(true);
}
public void runClient()
{
Socket client;
try {
// Step 1: Create a Socket to make connection.
display.setText( "Attempting connection\n" );
host = InetAddress.getLocalHost(); //...............
client = new Socket(host, 50000 );
display.append( "Connected to: " +
client.getInetAddress().getHostName() );
// Step 2: Get the input and output streams.
output = new ObjectOutputStream(
client.getOutputStream() );
output.flush();
input = new ObjectInputStream(
client.getInputStream() );
display.append( "\nGot I/O streams\n" );
// Step 3: Process connection.
enter.setEnabled( true );
do {
try {
message = (String) input.readObject();
display.append( "\n" + message );
display.setCaretPosition(
display.getText().length() );
}
catch ( ClassNotFoundException cnfex ) {
display.append(
"\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
// Step 4: Close connection.
display.append( "Closing connection.\n" );
output.close();
input.close();
client.close();
}
catch ( EOFException eof ) {
System.out.println( "Server terminated connection" );
}
catch ( IOException e ) {
e.printStackTrace();
}
}
private void sendData( String s )
{
try {
message = s;
output.writeObject( "CLIENT1>>> " + s );
output.flush();
display.append( "\nCLIENT1>>>" + s );
}
catch ( IOException cnfex ) {
display.append(
"\nError writing object" );
}
}
public static void main( String args[] )
{
Client2 app = new Client2();
app.runClient();
}
}// end of Client
Server code
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextField enter;
private JTextArea display, saved;
ObjectOutputStream output;
ObjectInputStream input;
public Server()
{
super( "Client" );
enter = new JTextField();
enter.setEnabled( false );
enter.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
sendData( e.getActionCommand() );
}
}
);
add( enter, BorderLayout.NORTH );
display = new JTextArea();
add( new JScrollPane( display ),
BorderLayout.CENTER );
setSize( 600, 300 );
setVisible(true);
add( new JButton( "Delete"), BorderLayout.EAST);
saved = new JTextArea();
add( new JScrollPane( display ),
BorderLayout.SOUTH );
setSize( 600, 300 );
setVisible(true);
}
public void runServer()
{
ServerSocket server;
Socket connection;
int counter = 1;
try {
// Step 1: Create a ServerSocket.
server = new ServerSocket(50000 );
while ( true ) {
// Step 2: Wait for a connection.
display.setText( "Waiting for connection\n" );
connection = server.accept();
display.append( "Connection " + counter +
" received from: " +
connection.getInetAddress().getHostName() );
// Step 3: Get input and output streams.
output = new ObjectOutputStream(
connection.getOutputStream() );
output.flush();
input = new ObjectInputStream(
connection.getInputStream() );
display.append( "\nGot I/O streams\n" );
// Step 4: Process connection.
String message =
"SERVER>>> Connection successful";
output.writeObject( message );
output.flush();
enter.setEnabled( true );
do {
try {
message = (String) input.readObject();
display.append( "\n" + message );
display.setCaretPosition(
display.getText().length() );
}
catch ( ClassNotFoundException cnfex ) {
display.append(
"\nUnknown object type received" );
}
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
// Step 5: Close connection.
display.append( "\nUser terminated connection" );
enter.setEnabled( false );
output.close();
input.close();
connection.close();
++counter;
}
}
catch ( EOFException eof ) {
System.out.println( "Client terminated connection" );
}
catch ( IOException io ) {
io.printStackTrace();
}
}
private void sendData( String s )
{
try {
output.writeObject( "CLIENT2>>> " + s );
output.flush();
display.append( "\nCLIENT2>>>" + s );
}
catch ( IOException cnfex ) {
display.append(
"\nError writing object" );
}
}
public static void main( String args[] )
{
Server app = new Server();
app.runServer();
}
}//end of Server
Thanks in advance for any guidance.
PJ

New Topic/Question
Reply




MultiQuote




|