Results 1 to 1 of 1
Thread: Multi Client-Bingo
- 05-23-2009, 05:21 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 10
- Rep Power
- 0
Multi Client-Bingo
hi guys we are making a multi client bingo game via socket.
we have a bingo class, serverthread class, multiserver class and a client class.
All the control functions are available in bingo class. In client class we call the bingo class so, bingo's all functions come to client. These are in client part.
In server side ServerThread class is in relation with the clients. In multiserver class the connections ( ip,port) are listed.
We want to do server creates random numbers from 0 to 90,90 is exceptable, and throw them to the client.Client will read them and if the number is in his panel then close it.
I have a problem in ServerThread and client.ServerThread creates different numbers correctly.Client take only a number which is zero.
The ServerThread,client and multiserver codes are in below.the bingo class works perfect
i am waiting your answer , it is so important.
//MultiServerJava Code://ServerThread Class public class ServerThread extends Thread{ private Socket server = null; private MultiServer ms = null; private final int c; /** * @param args */ ArrayList<Integer> uretilenSayilar = new ArrayList<Integer>(); Random r; @SuppressWarnings("static-access") public ServerThread(Socket socket, MultiServer m) { super("Server"); this.server = socket; this.ms = m; c = ms.count; try{ ms.clients[c] = (String)ms.input[c].readObject(); for(int i = 0; i <= c; i++) { if(i==c) { for(int j = 0; j<=c; j++) { ms.output[i].writeObject("MYbingoGame"+ms.clients[j]); ms.output[i].flush(); } } else { ms.output[i].writeObject("MYbingoGame"+ms.clients[c]); ms.output[i].flush(); } } } catch(Exception ex){} } @SuppressWarnings({"static-access","static-access"}) public void run() { for(int a=0; a<91; a++) { uretilenSayilar.add(a); } r = new Random(); int gonder=-1; while(true) { try { if(uretilenSayilar.isEmpty()) //gönderilecek sayı kalmayınca -1 gönder client'lara { gonder=-1; } else { gonder=uretilenSayilar.remove(r.nextInt(uretilenSayilar.size())); uretilenSayilar.trimToSize(); } sleep(300); for(int i = 0; i<ms.count; i++) { ms.output[i].writeObject(ms.clients[c]+">>"+gonder+"\n"); System.out.println("Sayı " + gonder + " gonderildi"); ms.output[i].flush(); } server.close(); } catch(Exception ex) { } } } }
public class MultiServer {
public static ObjectOutputStream[]output = new ObjectOutputStream[5];
public static ObjectInputStream []input = new ObjectInputStream[5];
public String []clients = new String[5];
public static int count = 0;
public MultiServer()
{
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
MultiServer ms = new MultiServer();
ServerSocket server = null;
Socket socket = null;
try{
server = new ServerSocket(444);
}
catch(Exception ex){}
boolean listening = true;
try{
while(listening && count<5)
{
System.out.println("Client bekliyorum...");
if((socket = server.accept())!= null)
{
System.out.println("Client baglandi !");
output[count] = new ObjectOutputStream(socket.getOutputStream());
input[count] = new ObjectInputStream(socket.getInputStream());
new ServerThread(socket, ms).start();
count++;
}
}
server.close();
for(int i = 0; i<=ms.count; i++)
{
output[i].close();
input[i].close();
}
}
catch(Exception ex){}
}
}
Java Code://Client public class client extends JFrame implements Runnable { /** * @param args */ private static bingo bngo=null; private Font fnt=null; private JLabel lblKelimeler=null; private JLabel lblGelenRakam=null; public int serverdanGelenSayi=-1;//İNİTİAL -1 KALSIN public static Socket connection = null; public static ObjectOutputStream output = null; //niye static? -> 3client instance'ı için de aynı input-output kullanıyoruz. class'tan bağımsız. public static ObjectInputStream input = null; public int getServerdanGelenSayi() { return serverdanGelenSayi; } public void setServerdanGelenSayi(int serverdanGelenSayi) { this.serverdanGelenSayi = serverdanGelenSayi; } public void setLblKelimeler(String kelime) { this.lblKelimeler.setText(kelime); } public client() { bngo = new bingo();//TREAD,JPANEL bngo.clnt=this; Container c=new JPanel();//ELEMANLARINIZI BUNA EKLEYIN TEXTBOX ŞU BU BorderLayout bl=new BorderLayout(); c.setLayout(bl); //FRAME E EKLEMELER c.add(bngo,BorderLayout.CENTER); lblKelimeler=new JLabel(); fnt= lblKelimeler.getFont(); lblKelimeler.setFont(new Font(fnt.getName(), fnt.getStyle(), fnt.getSize() + 30)); JPanel pnlKelime=new JPanel(); pnlKelime.add(lblKelimeler,BorderLayout.CENTER); c.add(pnlKelime,BorderLayout.SOUTH); lblGelenRakam=new JLabel(); c.add(lblGelenRakam,BorderLayout.EAST); // c.setVisible(true); add(c); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } public void setLblGelenRakam(int sayi) { this.lblGelenRakam = lblGelenRakam; ImageIcon btnImage=null; if(sayi>80) btnImage = new ImageIcon("images/_000"+String.valueOf(90-sayi)+"_"+String.valueOf(sayi)+".jpg"); else btnImage = new ImageIcon("images/_00"+String.valueOf(90-sayi)+"_"+String.valueOf(sayi)+".jpg"); lblGelenRakam.setIcon(btnImage); } public void START() { Thread ch=new Thread(this); ch.start(); } public static void main(String[] args) { // TODO Auto-generated method stub //new client().setVisible(true); //BINGO THREAD UNUTMAYIN client c1 = new client(); c1.setVisible(true); c1.START(); Thread th=new Thread(bngo); th.start(); } public void run() { String sentence = null; //Scanner inFromUser = new Scanner(System.in); ArrayList<Socket> clients = new ArrayList<Socket>(); Socket clientSocket= null; while(true) { try { connection = new Socket("localhost", 444); output = new ObjectOutputStream(connection.getOutputStream()); //socket'ten çıkan output akışını buna yönlendiriyo. input = new ObjectInputStream(connection.getInputStream()); int serverdanGelenSayi = 0; setLblGelenRakam(serverdanGelenSayi); // the server's number will be shown here } catch(Exception ex) { System.out.println(ex.getMessage()); } } } }
Similar Threads
-
Multi Client Server applications, troubles with File I/O
By zackpudil in forum NetworkingReplies: 3Last Post: 01-29-2010, 06:53 AM -
Multi-user chat server and client
By 435.mahesh in forum Java SoftwareReplies: 6Last Post: 04-25-2009, 12:45 AM -
Multi Client/Server Chat Question
By Kodak07 in forum NetworkingReplies: 3Last Post: 03-29-2009, 10:50 PM -
Multi Client TCP or UDP
By hunterbdb in forum NetworkingReplies: 8Last Post: 10-17-2008, 04:10 AM


LinkBack URL
About LinkBacks

Bookmarks