Solved

client/server

Posted on 2003-11-20
2
244 Views
Last Modified: 2008-03-17
Hi, below is a client and server program that I am having trouble with, im loading an applet in a browser but the server does'nt seem to be recognising the connection, how do I send a string to the server, store it on the server until something happens, in this case until the other client sends another string to the server. Im not sure this is the correct forum but any help is greatly appreciated.

Server program


import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class server extends JFrame {
   private byte board[];
   private boolean xMove;
   private JTextArea output;
   private Player players[];
   private ServerSocket server;
   private int currentPlayer;

   public server()
   {
      super( "server" );

      board = new byte[ 9 ];
      xMove = true;
      players = new Player[ 2 ];
      currentPlayer = 0;

      // set up ServerSocket
      try {
         server = new ServerSocket( 7000, 2 );
      }
      catch( IOException e ) {
         e.printStackTrace();
         System.exit( 1 );
      }

      output = new JTextArea();
      getContentPane().add( output, BorderLayout.CENTER );
      output.setText( "Server awaiting connections\n" );

      setSize( 300, 300 );
      show();
   }

   // wait for two connections so game can be played
   public void execute()
   {
      for ( int i = 0; i < players.length; i++ ) {
         try {
            players[ i ] =
               new Player( server.accept(), this, i );
            players[ i ].start();
         }
         catch( IOException e ) {
            e.printStackTrace();
            System.exit( 1 );
         }
      }

      // Player X is suspended until Player O connects.
      // Resume player X now.
      synchronized ( players[ 0 ] ) {
         players[ 0 ].threadSuspended = false;
         players[ 0 ].notify();
      }

   }

   public void display( String s )
   {
      output.append( s + "\n" );
   }

   
   public boolean gameOver()
   {
      // Place code here to test for a winner of the game
      return false;
   }

   public static void main( String args[] )
   {
      TicTacToeServer game = new TicTacToeServer();

      game.addWindowListener( new WindowAdapter() {
        public void windowClosing( WindowEvent e )
            {
               System.exit(0);
            }
         }
      );

      game.execute();
   }
}

// Player class to manage each Player as a thread
class Player extends Thread {
   private Socket connection;
   private DataInputStream input;
   private DataOutputStream output;
   private server control;
   private int number;
   private char mark;
   protected boolean threadSuspended = true;

   public Player( Socket s, server t, int num )
   {
      mark = ( num == 0 ? 'X' : 'O' );

      connection = s;

      try {
         input = new DataInputStream(
                    connection.getInputStream() );
         output = new DataOutputStream(
                    connection.getOutputStream() );
      }
      catch( IOException e ) {
         e.printStackTrace();
         System.exit( 1 );
      }

      control = t;
      number = num;
   }

   public void otherPlayerMoved( int loc )
   {
      try {
         output.writeUTF( "Opponent moved" );
         output.writeInt( loc );
      }
      catch ( IOException e ) { e.printStackTrace(); }
   }

   public void run()
   {
      boolean done = false;

      try {
         control.display( "Player " +
            ( number == 0 ? 'X' : 'O' ) + " connected" );
         output.writeChar( mark );
         output.writeUTF( "Player " +
            ( number == 0 ? "X connected\n" :
                            "O connected, please wait\n" ) );

         // wait for another player to arrive
         if ( mark == 'X' ) {
            output.writeUTF( "Waiting for another player" );

            try {
               synchronized( this ) {
                  while ( threadSuspended )
                     wait();
               }
            }
            catch ( InterruptedException e ) {
               e.printStackTrace();
            }

            output.writeUTF(
               "Other player connected. Your move." );
         }

         // Play game
         while ( !done ) {
            int location = input.readInt();

       

            if ( control.gameOver() )
               done = true;
         }

         connection.close();
      }
      catch( IOException e ) {
         e.printStackTrace();
         System.exit( 1 );
      }
   }
}

/**********************************************************/

Client program

// Fig. 21.8: TicTacToeClient.java
// Client for the TicTacToe program
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

// Client class to let a user play Tic-Tac-Toe with
// another user across a network.
public class c extends JApplet
                             implements Runnable {
   private JTextField id;
   private JTextArea display;
   private JPanel boardPanel, panel2;
   private Socket connection;
   private DataInputStream input;
   private DataOutputStream output;
   private Thread outputThread;
   private char myMark;
   private boolean myTurn;

   // Set up user-interface and board
   public void init()
   {

   }

   // Make connection to server and get associated streams.
   // Start separate thread to allow this applet to
   // continually update its output in text area display.
   public void start()
   {
      try {
         connection = new Socket(
            InetAddress.getByName( "10.0.0.79" ), 7000 );
         input = new DataInputStream(
                        connection.getInputStream() );
         output = new DataOutputStream(
                        connection.getOutputStream() );
      }
      catch ( IOException e ) {
         e.printStackTrace();
      }

      outputThread = new Thread( this );
      outputThread.start();
   }

   // Control thread that allows continuous update of the
   // text area display.
   public void run()
   {

      // Receive messages sent to client
      while ( true ) {
         try {
            String s = input.readUTF();
           
         }
         catch ( IOException e ) {
            e.printStackTrace();
         }
      }
   
   }

}
/*****************************************************/
this html file loads the client (c.class)

<html>
<applet code="c.class" width=300 height=225>
</applet>
</html>
0
Comment
Question by:Ronayne
2 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9795412
 Are you getting any error messages? What exactly dosn't work?
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 125 total points
ID: 9872891
You need to do following things in your applet

1. For connecting to the server, you need to create signed applet
2. If you are creating this for your own purpose, change the java security policy settings. For this you need to use Java PolicyTool(policytool.ex) otherwise you need to create signed applet (Trusted applet)
3. http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20245900.html?query=Signed+Applet&searchType=topic (Probably this helps you)
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_11346457.html?query=Signed+Applet&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10329550.html?query=Signed+Applet&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10133857.html?query=Signed+Applet&searchType=topic



http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10301028.html?query=Signed+Applet+sockets&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10589261.html?query=Signed+Applet+sockets&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10073195.html?query=Signed+Applet+sockets&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20088219.html?query=Signed+Applet+sockets&searchType=topic
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20425906.html?query=Signed+Applet+sockets&searchType=topic
(these links also helps you)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 67
JavaFX TableView not displaying correctly 3 51
How to convert from xls to xlsx using java 7 52
add projects t working set in maven 2 16
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question