4 Replies - 520 Views - Last Post: 02 May 2011 - 05:32 AM Rate Topic: -----

#1 pr4st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 11

java networking

Posted 02 May 2011 - 12:03 AM

hello,i'm beginner in java networking..
i tried to make a card program in java with a server and 4 client and i have problem how to give the card for 4 client and i'm follow tutorial about make server & client with java..
here the code
server.java
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;

public class ServerGame {
    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static final int PORT = 1234;
    private static koneksiClient t [] = new koneksiClient [4];

    public static void main(String[] args) {
        try{
            serverSocket = new ServerSocket (PORT);
            System.out.println("server running");
        }catch (IOException ioEx){
            System.out.println("failed to binding port");
            System.exit(1);
        }
        while (true){
            try{
                clientSocket = serverSocket.accept();
                for (int i=0; i<=3; i++){
                    (t[i] = new koneksiClient(clientSocket,t)).start();
                    break;
                }
            }catch(IOException ioEx){

            }
        }
    }
}

class koneksiClient extends Thread{
    Socket clientSocket;
    koneksiClient t[];
    DataInputStream input;
    PrintStream output;

    koneksiClient (Socket clientSocket, koneksiClient t []){
        this.clientSocket=clientSocket;
        this.t=t;
    }
    public void run(){
        String name;
        try{
            input = new DataInputStream(clientSocket.getInputStream());
            output = new PrintStream (clientSocket.getOutputStream());
            output.println("masukan nama :");
            name = input.readLine();
            output.println(name+" connected with server");
            System.out.println(name+ " connected with server");
        for (int i=0; i<=3; i++)
            if (t[i]!=null && t[i]!=this)
                t[i].output.println(name +" connected with server");
        for(int i=0; i<=3; i++)
		if (t[i]==this) t[i]=null;
            System.exit(1);
	    input.close();
	    output.close();
	    clientSocket.close();
    }catch (IOException ioEx){

    }
    }

}

public class Card {
    private int no;
    private int gbr;
    private String []nmr={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","AS"};
    private String [] gmbr = {"S","H","D","C"};

    public Kartu(int no,int gbr){
        this.no=no;
        this.gbr=gbr;
    }
    public @Override String toString(){
        return nmr [no] + " " + gmbr[gbr];
    }
    public int getNo(){
         switch (no) {
            case 0:
                return 2;
            case 1:
                return 3;
            case 2:
                return 4;
            case 3:
                return 5;
            case 4:
                return 6;
            case 5:
                return 7;
            case 6:
                return 8;
            case 7:
                return 9;
            case 8:
                return 10;
            case 9:
                return 11;
            case 10:
                return 12;
            case 11:
                return 13;
            case 12:
                return 14;
            default:
                return 0;
        }
    }
    public int getGbr(){
             return gbr;
         }
}

class deck{
    private List<Card> deck;

     public deck() {
        deck = new ArrayList<Card>();
        for (int i = 0; i < 52; i++) {
            deck.add(new Card(i % 13, i / 13));
        }
    }
     public void shuffle() {
        Collections.shuffle(deck);
    }
     public String toString() {
        return Arrays.toString(deck.toArray());
    }
     public static void main(String[] args) {
        deck test = new deck();
        System.out.println("deck sebelum di shuffle" + test);
        coba.shuffle();
        System.out.println("deck setelah di acak" + test);
    }
}




the client:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class ClientGame implements Runnable{
    private static Socket client = null;
    private static final int PORT = 1234;
    private static DataInputStream input;
    private static PrintStream output;
    private static BufferedReader inputLine;
    private static boolean tutup = false;

    public static void main(String[] args) {
        try {
            client = new Socket ("127.0.0.1",PORT);
            System.out.println("anda terhubung dengan server");
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            input = new DataInputStream (client.getInputStream());
            output = new PrintStream (client.getOutputStream());
        }catch (IOException ioEx){
            System.out.println("failed connect to server");
        }
        if (client != null && output != null && input != null) {
            try {
                new Thread(new ClientGame()).start();
		while (!tutup) {
                    output.println(inputLine.readLine());
                }

		output.close();
		input.close();
		client.close();
                System.exit(1);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }

    public void run() {
        
   String response;

	try{
	    while ((response = input.readLine()) != null) {
		System.out.println(tanggapan);
		
	    }
            tutup=true;
	} catch (IOException e) {
	    System.err.println("IOException:  " + e);
	}
    }
}


anyone can give me the hint or tutorial how the server give the card for 4 client?
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: java networking

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java networking

Posted 02 May 2011 - 12:19 AM

When a client connect you want to create a single client (not four)
and start a thread for that client. Does not make sense to pass an array of client to your client constructor
something like

    public static void main(String[] args) {
        try{
            serverSocket = new ServerSocket (PORT);
            System.out.println("server running");
        }catch (IOException ioEx){
            System.out.println("failed to binding port");
            System.exit(1);
        }
        int nbConnected = 0;
        while (nbConnected < 4){
            try{
                clientSocket = serverSocket.accept();
                t[nbConnected++] = new koneksiClient(clientSocket)).start();
                }
            }catch(IOException ioEx){

            }
        }
    }
}

class koneksiClient extends Thread{
    Socket clientSocket;
    DataInputStream input;
    PrintStream output;

    koneksiClient (Socket clientSocket){
        this.clientSocket=clientSocket;
    }
    public void run(){
        String name;
        try{
            input = new DataInputStream(clientSocket.getInputStream());
            output = new PrintStream (clientSocket.getOutputStream());
            output.println("masukan nama :");
            name = input.readLine();
            output.println(name+" connected with server");
            System.out.println(name+ " connected with server");
... your card handling here
    }catch (IOException ioEx){

    }
    }

}


Was This Post Helpful? 0
  • +
  • -

#3 pr4st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 11

Re: java networking

Posted 02 May 2011 - 04:38 AM

thanks for the answer pbl...
int nbConnected = 0;
        while (nbConnected < 4){
            try{
                clientSocket = serverSocket.accept();
                t[nbConnected++] = new koneksiClient(clientSocket)).start();
                }
            }catch(IOException ioEx){
 
            }


i tried your code but when client 4 enter the name my server is stopped
i want my server still running when accepted 4 client and block client 5 who request connection to server?anyone can explain to me...thanks
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java networking

Posted 02 May 2011 - 05:01 AM

different alternatives
int nbConnected = 0;
        // listen to socket until 4 connected
        while (nbConnected < 4){
            try{
                   clientSocket = serverSocket.accept();
                   t[nbConnected++] = new koneksiClient(clientSocket)).start();
              }
            }catch(IOException ioEx){
 
            }
         }
         // loop forever
         while(true) {
            // sleep 10 seconds
            try {
              Thread.sleep(10000L);
            }
            catch(Exception e) {
            }
            // check if at least one client still connected
            boolean oneConnected = false;
            for(int i - 0; i < t.length; i++) {
               // yes still conncetd
               if(t[i].clientSocket.isConnected) {
                  oneConnected = true;
                  break;
               }
            }
            // if none conncetde exit forecer loop
            if(!oneConnected)
               break;
         }


Was This Post Helpful? 0
  • +
  • -

#5 pr4st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 11

Re: java networking

Posted 02 May 2011 - 05:32 AM

wow its works...
thank you very much pbl :smile2: :smile2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1