Solved

client/server

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

617 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