Solved

client/server

Posted on 2003-11-20
2
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 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