السلام عليكم
أنا عندي كود لبرنامج شات بين server واكتر من clientبس المشكله انو السيرفر لما يبعت مسج بتوصل فقط لآخر clientعمل اتصال مع الserver كيف بدي اتخلص من المشكله بحيث اخلي الserver يرسل لجميع الكليتنس في نفس الوقت مع العلم اني يستخدم TCPيعني مش حقدر استخدم ملتي كاست
وهذا كود الserver
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame implements ActionListener
{
private static JTextField t=new JTextField(10);
//create textfield with lenght 10
static JTextArea a= new JTextArea(20,40);
//create textarea with row 20 and column 40
private JButton b= new JButton("send");
//create button send
private JButton b1=new JButton("Exit");
//create button exit
String data;
//Declare the string to be sent
static String st;
static BufferedReader in;
//buferr for read and resive
static PrintWriter out;
//buferr use for write and send
//
Container pp=getContentPane();
//container
Server()//constructor
{//begin block
super("server");// title of the frame
JPanel p= new JPanel();
//create panel p to hold text area
p.setLayout(new BorderLayout());
//to arrange componant in the panel
p.add(a,BorderLayout.CENTER);
//add textarea in the panel and arrange it in
//the center
JPanel p1= new JPanel();
//add panel p1 to hold buttons, textfield and
//label
p1.setLayout(new GridLayout(2,1));
//layout to arrange componant
p1.add(new Label("Enter Message"));
//creat and add label to the panel p1
p1.add(t);
//add text field to the panel p1
p1.add(b,BorderLayout.SOUTH);
//add button to the panel
p1.add(b1,BorderLayout.SOUTH );
//add button to the panel
pp.add(p,BorderLayout.CENTER);
//add panel p to the panel pp
pp.add(p1,BorderLayout.SOUTH);
//add panel p1 to the panel pp
b.addActionListener(this);
//add addActionListener(this) to make event
b1.addActionListener(this);
//add addActionListener(this) to make event
}//end block
public static void read()
{
try{
st=in.readLine();
System.out.println(st);
}
catch(Exception ex) {
System.err.println(ex);}
}
public static void write()
{
try{
//out = new PrintWriter(skt.getOutputStream(),true);
out.println(t.getText());
//write in the file(buffer)
a.append("Server says : "+t.getText()+"\n");
//display what server say in the text area
}
catch(Exception ex) {
System.err.println(ex);}
}
public static void a_append()
{
a.append("client says : "+st+"\n");
}
public void actionPerformed(ActionEvent event)
{
//cheak if it button send
if(event.getSource()==b)
{
Server.write();
/* out.println(t.getText());
//write in the file(buffer)
a.append("Server says : "+t.getText()+"\n");
//display what server say in the text area*/
}
//cheak if it button exit
if(event.getSource()==b1)
{
System.exit(0);//exit
}
}
public static void main(String args[])
{
Server d=new Server();
d.pack();//for sutible size to display
d.setVisible(true);//to be display
try { //begin of try block
ServerSocket srvr = new ServerSocket(1234); //create aserver socket
int i=1;
//start listening for connections on the server socket
while(true)
{
Socket skt = srvr.accept();
d.in= new BufferedReader(new InputStreamReader(skt.getInputStream()));
d.out = new PrintWriter(skt.getOutputStream(),true);
d.a.append("Start thread for client " + i+"\n");
InetAddress clientInetAddress = skt.getInetAddress();
d.a.append("Client " + i + "'s host name is "+ clientInetAddress.getHostName()+"\n");
d.a.append("Client " + i + "'s IP Address is "+ clientInetAddress.getHostAddress()+"\n");
ThreadHandler thread=new ThreadHandler(skt, i);
thread.start();
i++;
//create string and read aline
}//end of while loop
} //end of try block
//catch exception block
catch(Exception ex) {
System.err.println(ex);
} //end of catch block
}
}
class ThreadHandler extends Thread
{
private Socket skt;
private int counter;
public ThreadHandler(Socket socket,int i)
{
skt=socket;
counter=i;
}
public void run()
{
try
{
// Continuously serve the client
while (true)
{
Server.read();
// st=Server.in.readLine();
Server.a_append();
//a.append("client says : "+Server.st+"\n");
Server.write();
}
}
catch(Exception ex) {
Server.a.append("connection is cut .. try again!\n");
}
}
}أما الclient:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.*;
public class Client extends JFrame implements ActionListener
{
private JTextField t=new JTextField(10);
//create textfield with lenght 10
private JTextArea a= new JTextArea(20,40);
//create textarea with row 20 and column 40
private JButton b= new JButton("send");
//create button send
private JButton b1=new JButton("Exit");
//create button exit
String data;
//Declare the string to be sent
String st;
BufferedReader in;
//for read and resive
PrintWriter out;
//for write and send
Container pp=getContentPane();
Client()
{
super("Client");// title of the frame
JPanel p= new JPanel();
//create panel p to hold text area
p.setLayout(new BorderLayout());
//to arrange componant in the panel
p.add(a,BorderLayout.CENTER);
//add textarea in the panel and arrange it in
//the center
JPanel p1= new JPanel();
//add panel p1 to hold buttons, textfield and
//label
p1.setLayout(new GridLayout(2,1));
//layout to arrange componant
p1.add(new Label("Enter Message"));
//creat and add label to the panel p1
p1.add(t);
//add text field to the panel p1
p1.add(b,BorderLayout.SOUTH);
//add button to the panel
p1.add(b1,BorderLayout.SOUTH );
//add button to the panel
pp.add(p,BorderLayout.CENTER);
//add panel p to the panel pp
pp.add(p1,BorderLayout.SOUTH);
//add panel p1 to the panel pp
b.addActionListener(this);
//add addActionListener(this) to make event
b1.addActionListener(this);
//add addActionListener(this) to make event
}
public void actionPerformed(ActionEvent event)
{
//cheak if it button send
if(event.getSource()==b)
{
out.println(t.getText());
//write in the file(buffer)
a.append("client says : "+t.getText()+"\n");
//display what client say in the text area
}
//cheak if it button exit
if(event.getSource()==b1)
{
System.exit(0);//exit
}
}
public static void main(String args[])
{
Client d=new Client();
d.pack();//for sutible size to display
d.setVisible(true); //to be display
try { //begin of try block
//create asocket to connect to the server
Socket skt = new Socket("Localhost", 1234);
//create abuffer input stream to recieve data from the server
d.in= new BufferedReader(new InputStreamReader(skt.getInputStream()));
//create abuffer output stream to send data to the server
d.out = new PrintWriter(skt.getOutputStream(),true);
//continuous send and recieve data from the server
while(true)
{
d.st=d.in.readLine(); //create string and read aline
d.a.append("Server says : "+d.st+"\n");
}//end of while loop
} //end of try block
//catch exception block
catch(Exception e) {
d.a.append("connection is cut .. try again!\n"); //output error msg to screen
} //end of catch block
}
}