connection and protocol

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import java.net.*;

import java.util.*;

public class Client extends Frame implements ActionListener

{

PrintStream ps = null;

PrintWriter pw = null;

BufferedReader bf=null;

Socket s = null;

Button emailButton = new Button("Find Email");//Button for finding email

Button nameButton = new Button("Find Name");//Button for finding staff name

Button numberButton = new Button("Number");//Button for number of members of staff

Button quitButton = new Button("Quit");//Button for quitting

TextField inputData = new TextField(); //For communicating arguments

TextField results = new TextField();//Results text field

Label inputDataLabel = new Label("Input data");

Label resultsLabel = new Label("Results");

public Client() {

super("Presentation");

setLayout(new GridLayout(4,2));

add(emailButton);

add(nameButton);

add(numberButton);

add(quitButton);

add(inputDataLabel);

add(inputData);

add(resultsLabel);

add(results);

//Register listeners

numberButton.addActionListener(this);

nameButton.addActionListener(this);

emailButton.addActionListener(this);

quitButton.addActionListener(this);

initConnection();

}

public void actionPerformed(ActionEvent ae)

{

String name = inputData.getText();

try

{

pw.println(inputData.getText());

String reply = bf.readLine();

results.setText(reply);

}

catch(Exception e)

{System.out.println("Problem establishing a connection 1");}

{

try

{

s = new Socket("127.0.0.1", 1200);

InputStream is = s.getInputStream();

BufferedReader bf = new BufferedReader(new InputStreamReader(is));

}

catch(java.io.IOException e)

{System.out.println("Problem establishing a connection");}

}

public static void main(String[] args)

{

Client cf = new Client();

cf.setSize(300,200); //300 pixels by 200 pixels

cf.setVisible(true);

}

}

Any help on the code above would be appreciated.

Firstly, I want to confirm that I have established a connection with the server I have created ,what is the simplest way of doing this?

Secondly, this particular piece of code aims to use a protocol to complete a range of tasks. I'm not familiar with ActionListener but want to create code that initiates one of four methods depending on the button selected. Again any guidance would be appreciated.

[2660 byte] By [nick211001a] at [2007-11-27 7:05:48]
# 1

When you post code, please wrap it in [code][/code] tags so it's easier to read.Also, indent it meaningfully.

I would advice doing network connections in a GUI event handler. You don't want the GUI to be constrained by the network. Rather, I'd suggest creating a new object to handle the connection, run it with its own thread, and use the GUI to start the object running.

You try to add a Client object as the action handler to some buttons, but Client does not implement ActionListener.

Personally I'd suggest using separate inner classes to handle action events for different buttons.

paulcwa at 2007-7-12 18:57:03 > top of Java-index,Java Essentials,New To Java...