Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

client/server

Posted on 2003-11-20
2
Medium Priority
?
252 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

886 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