Welcome to Dream.In.Code
Become a Java Expert!

Join 150,053 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,723 people online right now. Registration is fast and FREE... Join Now!




server client problem

 
Reply to this topicStart new topic

server client problem

killer_beast
4 Jun, 2008 - 09:38 PM
Post #1

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
so for the final time just i want to know whether this program will work on two machines after i alter the ports
and tell me if you run the program using this code below , how did you run it. it should be able to create the server and client right.



CODE

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

import java.awt.*;
import java.awt.event.*;
public class final_class {

    /**
     * @param args
     */
    private static int factor;private static int terrain;
    private static int score1=0,score2=0;
    private static int[] t1,t2;
    private static int i,r1;
    static pla p1,p2;
    public void final_class(){}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        t1=new int[5];t2=new int[5];
        for(i=0;i<5;i++){t1[i]=0;t2[i]=0;}
        
        Thread t=new CreateGui();
    t.start();
    for(i=0;i<5;i++)
      {while(p2 == null) {  
                     try {  
                         Thread.sleep(1000L);  
                     }  
                     catch (Exception e) {}  
                 }  
                 // ok the 2 frames where created  
                 // now we can wait on both factor  
                 while(p1.terrain == -1 || p2.terrain == -1) {  
                     System.out.println("We wait because one of the factor still == -1");  
                     try {  
                         Thread.sleep(1000L);   // sleep 1 second  
                     }  
                     catch (Exception e) {}  
                 }  
                 // OK both factor do not equals -1 anymore  
                 System.out.println("Ready to process: terrain = " + p1.terrain + " factor = " + p2.terrain);  
                 if(t1[p1.terrain-1]==1 || t2[p2.terrain-1]==1)
                 {
                System.out.println("wrong choice, cant choose same parameters again ");    i=i-1;
                 }
                 else{
                     mxpro r=new mxpro(p1.terrain,p2.terrain);
                     score1=score1+r.res1;
                     score2=score2+r.res2;
                 t1[p1.terrain-1]=1;t2[p2.terrain-1]=1;
                 }
                
                
/*if(factor>0){switch(terrain){case 1: mx1 r1=new mx1();r1.setVisible(true);break;case 2:mx2 r2=new mx2();r2.setVisible(true);break;case 3: mx3 r3=new mx3();r3.setVisible(true);break;case 4:mx4 r4=new mx4();r4.setVisible(true);break;case 5:mx5 r5=new mx5(); r5.setVisible(true);break; }*/    
// factor=p2.choix();System.out.println("fac "+factor);//terrain=p1.choix();System.out.println("terrain "+terrain);//System.out.println("terrain "+terrain);
      }
    score sc;
     sc=new score(score1,score2);sc.setVisible(true);
    
     p2.closeclient();
     p1.closeserver();
         }
          static class CreateGui extends Thread{
                public void run(){
                    identi rr=new identi();
                    rr.setVisible(true);
                    while(rr.x==-1){try {  
                         Thread.sleep(1000L);   // sleep 1 second  
                     }  
                     catch (Exception e) {}  
                 }  
                if(rr.x==1){p1=new pla("terrain",rr.x);
                p1.setVisible(true);}
                else if(rr.x==0)
                {
                p2=new pla("factor",rr.x);p2.setVisible(true);}
                }
}
}  


CODE

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

class pla extends JFrame implements ActionListener
{
private JPanel x=new JPanel();
private JButton b1=new JButton("1");
private JButton b2=new JButton("2");
private JButton b3=new JButton("3");
private JButton b4=new JButton("4");
private JButton b5=new JButton("5");
private JTextField t=new JTextField(1);
private  BufferedWriter OBW = null;
private  BufferedReader IBR = null;
private Socket soc = null;
Socket Csoc;
ServerSocket Ssoc;
int terrain=-1;
// int r;
//final_class fc = new final_class();
public pla(String text,int a){
    //if a=0->cliente           a=1 -> server
    super(text);
    getContentPane().add(x);
    setSize(300,300);
    setVisible(true);

    {   if(a==0)
    {String server = new String("");
    try
    {
     // etablissement de la connexion cote client sur le port 20000
     System.out.print("Etablissement connexion avec le serveur ...");
     soc = new Socket(server,20000);
     System.out.println(" Le serveur " + server + " nous a reconnu");

     // phase de communication avec le serveur :
     // creation des objets de type InputStream et OutputStream
     // correspondant a l'etablissement d'un flux de donnees
     // permettant d'echanger des informations avec le serveur
     InputStream IS = soc.getInputStream();
     OutputStream OS = soc.getOutputStream();

    // Afin de pouvoir utiliser des entrees/sorties formatees,
    // mise en place de tampons de donnees
     IBR = new BufferedReader(new InputStreamReader(IS));
     OBW = new BufferedWriter(new OutputStreamWriter(OS));
    }
    catch(IOException e)
      { System.out.println("I/O exception error "); }
  
    }
    else if(a==1)
    {

        String reponse;
        int nbrcar,figure;
         try
            {
             // etablissement du service cote serveur sur le port 20000
             // creation du socket serveur que l'on attache au port 20000
             System.out.print("generating the server at port 20000 ...");
             Ssoc = new ServerSocket(20000, 180);
             System.out.println(" OK");
            // phase d'attente d'une connexion avec un client
            System.out.print("waiting for connection with cliente ...");
            Csoc = Ssoc.accept();   // lors de la connexion, on cree le
                 // socket client qui va servir a etablir la communication
            System.out.println("connection with cliente acheived");

            InputStream IS = Csoc.getInputStream();
            OutputStream OS = Csoc.getOutputStream();

             IBR = new BufferedReader(new InputStreamReader(IS));
             OBW = new BufferedWriter(new OutputStreamWriter(OS));
            
             reponse = new String(IBR.readLine()); // lecture chaine de caracteres
             System.out.println("the client replied that: " + reponse);
            
            }
         catch(UnknownHostException e) { }
         catch(IOException e)
              { System.out.println("Erreur entree/sortie pendant la connexion"); }
    }
    }
    x.add(b1);    b1.addActionListener((ActionListener) this);
    x.add(b2);    b2.addActionListener((ActionListener) this);
    x.add(b3);    b3.addActionListener((ActionListener) this);
    x.add(b4);    b4.addActionListener((ActionListener) this);
    x.add(b5);    b5.addActionListener((ActionListener) this);
    x.add(t);
    b1.setVisible(true);b2.setVisible(true);b3.setVisible(true);b4.setVisible(true);b5.setVisible(true);t.setVisible(true);
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
}
public void actionPerformed(ActionEvent ev){
    if(ev.getSource()==b1){t.setText("1");terrain=1;}
    if(ev.getSource()==b2){t.setText("2");terrain=2;}
    if(ev.getSource()==b3){t.setText("3");terrain=3;}
    if(ev.getSource()==b4){t.setText("4");terrain=4;}
    if(ev.getSource()==b5){t.setText("5");terrain=5;}
    //fc.passVart(terrain);
    switch(terrain){
    case 1: x.remove(b1);b1.setVisible(false);break;
    case 2: x.remove(b2);b2.setVisible(false);break;
    case 3:x.remove(b3);b3.setVisible(false);break;
    case 4: x.remove(b4);b4.setVisible(false);break;
    case 5: x.remove(b5);b5.setVisible(false);break;
    
    default: break;}
    }

public void closeserver(){
    System.out.print("Enfin, on ferme la communication avec le client ...");
   try{ Csoc.close();}catch(IOException ea){};
    System.out.println(" C'est fait, au revoir le client ...\n");
    System.out.print("\nPuis on arrete le service sur le port 20000 ...");
    try{Ssoc.close();}
   catch(Exception ea){ System.out.println("Erreur d'entree/sortie"); }
}
public void closeclient()
{
    System.out.print("Enfin, on ferme la communication ...");
try{    soc.close();}    
catch(UnknownHostException eaa)
  { System.out.println("le nom du serveur est inconnu"); }
catch(IOException eaa)
  { System.out.println("Erreur entree/sortie pendant la connexion"); }
System.out.println(" OK");

}
}




A JFrame is a Java Object if the receiving socket knows anything about Java it should be able to understand it and display it accordingly

thats what you said pbl
yes the unix machine there recognize java so it should work .
only think if the client and server program i created work . buut how can i test it on my own computer by creating a local server and client on same machine


This post has been edited by killer_beast: 4 Jun, 2008 - 09:43 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Server Client Problem
4 Jun, 2008 - 09:47 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Haven't test it but as far as I can see:
- the client will call the server
- the server will create the GUI on itself.... not realley what you want

Cannot say more until we see the Client part of it
User is online!Profile CardPM
+Quote Post

killer_beast
RE: Server Client Problem
4 Jun, 2008 - 09:53 PM
Post #3

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 34


My Contributions
QUOTE(pbl @ 4 Jun, 2008 - 10:47 PM) *

Haven't test it but as far as I can see:
- the client will call the server
- the server will create the GUI on itself.... not realley what you want

Cannot say more until we see the Client part of it


and the client will create the GUI too look at the part where i say a==0 it creates a client and the rest of the code with buttons is not included in the if else statement . i told you i just followed the course for this part ,and in my course i just had this for client. if i am wrong then what i have to do for the client part then
User is offlineProfile CardPM
+Quote Post

fsloke
RE: Server Client Problem
9 Jun, 2008 - 01:41 AM
Post #4

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Cannot copy Paste again your code...

It look many class not included....

Thank.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month