Solved

client/server

Posted on 2003-11-20
2
238 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 70
wordappend challenge 8 86
What is the latest versions eclipse neon 2 122
Unable to start eclipse ? 17 87
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now