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

New Topic/Question
Reply



MultiQuote



|