أويس
عدد المساهمات : 14 تاريخ التسجيل : 04/11/2011 العمر : 35
| موضوع: كود بلغة الجافا يمثل جانب الServer في برنامج دردشه مصغر الجمعة نوفمبر 04, 2011 9:48 pm | |
| بسم الله الرحمن الرحيم
السلام عليكم
شباب دا كود بسيط وأتمنى كل الشباب يستفيدو منو يعني بلاش كوبي بست نحاول نستفيد ونفهمو أول أما كود ال Clint حتلقوه في نفس المنتدى
كما لا ننسى وجود بعض الصور التي يجب أن ننتبه لها لأنها مستخدمه داخل الكود
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class Server extends JFrame implements ActionListener { JLabel label1,label2,label3,label4; JButton send,exit; JTextField field; JTextArea text,text2; JScrollPane pane,pane2; JPanel panel; Font font=new Font("Goudy Old Style",2,25); Font font2=new Font("Goudy Old Style",2,20); ServerSocket listenSocket; Socket clientSocket[]=new Socket[999999]; DataInputStream in; DataOutputStream out; String Clients[]=new String[9999999]; int count=0,count2=0; static UIManager.LookAndFeelInfo looks[]; String Cname; public Server() { super("********************************* Server *********************************"); looks = UIManager.getInstalledLookAndFeels(); try { UIManager.setLookAndFeel(looks[2].getClassName()); SwingUtilities.updateComponentTreeUI(this); } catch(Exception s) { } panel=new APanel(); panel.setLayout(null); label1=new JLabel("Wating for Client To Connect ....."); label1.setFont(font); label1.setBounds(50,100,500,50); panel.add(label1); label2=new JLabel("Clients Messages"); label2.setFont(font2); label2.setBounds(100,10,500,50); panel.add(label2); label2.setVisible(false); text=new JTextArea(); text.setFont(font2); text.setEditable(false); pane=new JScrollPane(text); pane.setBounds(50,50,280,360); panel.add(pane); pane.setVisible(false); label3=new JLabel("List of Connected Client"); label3.setFont(font2); label3.setBounds(450,10,500,50); panel.add(label3); label3.setVisible(false); text2=new JTextArea(); text2.setFont(font2); text2.setEditable(false); pane2=new JScrollPane(text2); pane2.setBounds(400,50,300,360); panel.add(pane2); pane2.setVisible(false); label4=new JLabel("Send Message For All Clients"); label4.setFont(font2); label4.setBounds(50,410,500,50); panel.add(label4); label4.setVisible(false); field=new JTextField(); field.setFont(font2); field.setBounds(50,470,300,30); panel.add(field); field.setVisible(false); send=new JButton("Send"); send.setBounds(370,465,100,40); send.setFont(font); panel.add(send); send.addActionListener(this); send.setVisible(false); exit=new JButton("Exit"); exit.setBounds(600,460,100,40); exit.setFont(font); panel.add(exit); exit.addActionListener(this); add(panel); setSize(750,600); setLocation(230,50); setVisible(true); setResizable(false); setDefaultCloseOperation(0); try { int serverPort=1990; listenSocket=new ServerSocket(serverPort); while(true) { clientSocket[count2]=listenSocket.accept(); Connection c=new Connection(clientSocket[count2++]); } } catch(IOException e) { e.printStackTrace(); } } class Connection extends Thread { Socket clientSocket1; public Connection(Socket aClientSocket) { try { clientSocket1=aClientSocket; in=new DataInputStream(clientSocket1.getInputStream()); out=new DataOutputStream(clientSocket1.getOutputStream()); this.start(); } catch(IOException e) { e.printStackTrace(); } } boolean flag=true; String data; public void run() { try { data=in.readUTF(); for(int i=0;i<count;i++) { if(Clients[i].equals(data)) { out.writeUTF("This ID Is Used"); flag=false; break; } else flag=true; } if(flag) { Clients[count]=data; out.writeUTF(data); for(int i=0;i<count2;i++) { try { out=new DataOutputStream(clientSocket[i].getOutputStream()); out.writeUTF(data+" Sign In"); } catch(Exception er) { continue; } } count++; text2.setText(""); for(int i=0;i<count;i++) { text2.append(Clients[i]+"\n"); } pane.setVisible(true); pane2.setVisible(true); label1.setVisible(false); label2.setVisible(true); label3.setVisible(true); label4.setVisible(true); send.setVisible(true); field.setVisible(true); Rec tm=new Rec(in,data); } } catch(Exception e) { e.printStackTrace(); } } } class Rec extends Thread { DataInputStream in; String Cname; public Rec(DataInputStream in0,String Name) { in=in0; Cname=Name; this.start(); } public void run() { try { while(true) { String f=in.readUTF(); text.append(f+"\n"); for(int i=0;i<count2;i++) { try { out=new DataOutputStream(clientSocket[i].getOutputStream()); out.writeUTF(f); } catch(Exception er) { continue; } } } } catch(Exception mm) { try { text.append(Cname+" Sign Out\n"); for(int i=0;i<count2;i++) { try { out=new DataOutputStream(clientSocket[i].getOutputStream()); out.writeUTF(Cname+" Sign Out"); } catch(Exception er) { continue; } } text2.setText(""); int in=0; String Names[]=new String[count-1]; for(int i=0;i<count;i++) { if(Clients[i].equals(Cname)) continue; else { Names[in++]=Clients[i]; } } for(int i=0;i<count;i++) Clients[i]=""; for(int i=0;i<count-1;i++) { Clients[i]=Names[i]; text2.append(Names[i]+"\n"); } } catch(Exception m) { m.printStackTrace(); } } } } public static void main(String args[]) { Server temp=new Server(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==send) { try { text.append("Server Says : "+field.getText()+"\n"); for(int i=0;i<count2;i++) { try { out=new DataOutputStream(clientSocket[i].getOutputStream()); out.writeUTF("Server Says : "+field.getText()); } catch(Exception ew) { continue; } } } catch(Exception ee) { ee.printStackTrace(); } } if(e.getSource()==exit) { int choose=JOptionPane.showOptionDialog(null,"Are You Sure ........",null,0,3,null,null,0); if(choose==0) { try { for(int i=0;i<count2;i++) clientSocket[i].close(); } catch(Exception mn) { mn.printStackTrace(); } System.exit(0); } } } class APanel extends JPanel { ImageIcon aboutimage = null; public APanel() { aboutimage=new ImageIcon("1.jpg"); setOpaque(false); } public void paint(Graphics g) { aboutimage.paintIcon(this, g, -300, -300); super.paint(g); } public Dimension getPreferredSize() { return new Dimension(aboutimage.getIconWidth(), aboutimage.getIconHeight()); } } }
| |
|