Here is my server code:
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.net.*;
- public class serverForm extends javax.swing.JFrame {
- private PrintWriter output = null;
- private BufferedReader input = null;
- private ServerSocket server = null;
- private Socket connection = null;
- public serverForm() {
- initComponents();
- }
- public void runServer()
- {
- try
- {
- server = new ServerSocket( Integer.parseInt(jTextField1.getText()), 2 );
- } catch (IOException e) {
- System.err.println("Could not listen on port: 4444.");
- System.exit(1);
- }
- try
- {
- waitForConnection();
- }
- catch ( IOException ioException )
- {
- ioException.printStackTrace();
- }
- try
- {
- output = new PrintWriter(connection.getOutputStream(), true);
- input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- processConnection();
- }
- catch ( IOException ioException )
- {
- ioException.printStackTrace();
- }
- finally
- {
- closeConnection();
- }
- }
- private void waitForConnection() throws IOException
- {
- statuslbl.setText( "Waiting for connection\n" );
- connection = server.accept();
- statuslbl.setText( "Connection received from: " + connection.getInetAddress().getHostName() );
- }
- private void processConnection() throws IOException
- {
- String inputLine, outputLine;
- protocol kkp = new protocol();
- outputLine = kkp.processInput(null);
- output.println(outputLine);
- jTextArea1.setText(jTextArea1.getText() + outputLine + "\n");
- while ((inputLine = input.readLine()) != null)
- {
- outputLine = kkp.processInput(inputLine);
- jTextArea1.setText(jTextArea1.getText() + inputLine + "\n");
- jTextArea1.setText(jTextArea1.getText() + outputLine + "\n");
- try
- {
- output.println(outputLine);
- }
- catch ( Exception e)
- {
- statuslbl.setText( "Error writing object" );
- }
- if (outputLine.equals("Bye."))
- break;
- }
- }
- private void closeConnection()
- {
- statuslbl.setText( "Terminating connection" );
- try
- {
- output.close();
- input.close();
- connection.close();
- }
- catch ( IOException ioException )
- {
- ioException.printStackTrace();
- }
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- runServer();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new serverForm().setVisible(true);
- }
- });
- }
Expand|Select|Wrap|Line Numbers
- import java.net.*;
- import java.io.*;
- public class protocol {
- private static final int WAITING = 0;
- private static final int SENTKNOCKKNOCK = 1;
- private static final int SENTCLUE = 2;
- private static final int ANOTHER = 3;
- private static final int NUMJOKES = 5;
- private int state = WAITING;
- private int currentJoke = 0;
- private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
- private String[] answers = { "Turnip the heat, it's cold in here!",
- "I didn't know you could yodel!",
- "Bless you!",
- "Is there an owl in here?",
- "Is there an echo in here?" };
- public String processInput(String theInput) {
- String theOutput = null;
- if (state == WAITING) {
- theOutput = "Knock! Knock!";
- state = SENTKNOCKKNOCK;
- } else if (state == SENTKNOCKKNOCK) {
- if (theInput.equalsIgnoreCase("Who's there?")) {
- theOutput = clues[currentJoke];
- state = SENTCLUE;
- } else {
- theOutput = "You're supposed to say \"Who's there?\"! " +
- "Try again. Knock! Knock!";
- }
- } else if (state == SENTCLUE) {
- if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
- theOutput = answers[currentJoke] + " Want another? (y/n)";
- state = ANOTHER;
- } else {
- theOutput = "You're supposed to say \"" +
- clues[currentJoke] +
- " who?\"" +
- "! Try again. Knock! Knock!";
- state = SENTKNOCKKNOCK;
- }
- } else if (state == ANOTHER) {
- if (theInput.equalsIgnoreCase("y")) {
- theOutput = "Knock! Knock!";
- if (currentJoke == (NUMJOKES - 1))
- currentJoke = 0;
- else
- currentJoke++;
- state = SENTKNOCKKNOCK;
- } else {
- theOutput = "Bye.";
- state = WAITING;
- }
- }
- return theOutput;
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import java.net.*;
- public class ClientForm extends javax.swing.JFrame {
- private Socket kkSocket = new Socket();
- private PrintWriter out = null;
- private BufferedReader in = null;
- /** Creates new form NewJFrame */
- public ClientForm() {
- initComponents();
- }
- public void send()
- {
- if (kkSocket.isConnected() != true)
- statuslbl.setText("Please Connect First");
- else
- try
- {
- String client,server;
- client = clientInput.getText();
- out.println(client);
- server = (String)in.readLine();
- if (server.equals("Bye."))
- {
- in.close();
- out.close();
- kkSocket.close();
- }
- jTextArea1.setText(jTextArea1.getText() + "User : " + client + "\n");
- jTextArea1.setText(jTextArea1.getText() + "Server : " + server + "\n");
- }
- catch (IOException e)
- {
- }
- clientInput.setText("");
- }
- private void connectbtnActionPerformed(java.awt.event.ActionEvent evt) {
- try
- {
- if (kkSocket.isConnected() == true)
- kkSocket.close();
- kkSocket = new Socket(ipText.getText(),Integer.parseInt(portText.getText()));
- out = new PrintWriter(kkSocket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader( kkSocket.getInputStream()));
- jTextArea1.setText(jTextArea1.getText() + "Server : " + (String)in.readLine() + "\n");
- }
- catch (Exception e)
- {
- statuslbl.setText("Can't Connect To Host");
- }
- if (kkSocket.isConnected() == true)
- statuslbl.setText("Connected");
- }
- private void clientInputActionPerformed(java.awt.event.ActionEvent evt) {
- send();
- }
- private void sendbtnActionPerformed(java.awt.event.ActionEvent evt) {
- send();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new ClientForm().setVisible(true);
- }
- });
- }