Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn tham khảo code sau xem, Kevin lấy từ internet chưa có time test thử nhé:


    Mã:
    // Chat clientimport java.net.*;import java.io.*;import java.util.*;import java.awt.*; class chatClient extends Frame implements Runnable{    Socket soc;        TextField tf;    TextArea ta;    Button btnSend,btnClose;    String sendTo;    String LoginName;    Thread t=null;    DataOutputStream dout;    DataInputStream din;    chatClient(String LoginName,String chatwith) throws Exception    {        super(LoginName);        this.LoginName=LoginName;        sendTo=chatwith;        tf=new TextField(50);        ta=new TextArea(50,50);        btnSend=new Button("Send");        btnClose=new Button("Close");        soc=new Socket("127.0.0.1",5217);         din=new DataInputStream(soc.getInputStream());         dout=new DataOutputStream(soc.getOutputStream());                dout.writeUTF(LoginName);         t=new Thread(this);        t.start();     }    void setup()    {        setSize(600,400);        setLayout(new GridLayout(2,1));         add(ta);        Panel p=new Panel();                p.add(tf);        p.add(btnSend);        p.add(btnClose);        add(p);        show();            }    public boolean action(Event e,Object o)    {        if(e.arg.equals("Send"))        {            try            {                dout.writeUTF(sendTo + " "  + "DATA" + " " + tf.getText().toString());                            ta.append("
    " + LoginName + " Says:" + tf.getText().toString());                    tf.setText("");            }            catch(Exception ex)            {            }            }        elseif(e.arg.equals("Close"))        {            try            {                dout.writeUTF(LoginName + " LOGOUT");                System.exit(1);            }            catch(Exception ex)            {            }                    }                return super.action(e,o);    }    publicstaticvoid main(String args[]) throws Exception    {        chatClient Client1=new chatClient(args[0],args[1]);        Client1.setup();                    }        publicvoid run()    {                while(true)        {            try            {                ta.append( "
    " + sendTo + " Says :" + din.readUTF());                            }            catch(Exception ex)            {                ex.printStackTrace();            }        }    }}
    Mã:
    // Chat Serverimport java.net.*;import java.util.*;import java.io.*; class chatServer{    static Vector ClientSockets;    static Vector LoginNames;        chatServer() throws Exception    {        ServerSocket soc=new ServerSocket(5217);        ClientSockets=new Vector();        LoginNames=new Vector();         while(true)        {                Socket CSoc=soc.accept();                    AcceptClient obClient=new AcceptClient(CSoc);        }    }    publicstaticvoid main(String args[]) throws Exception    {                chatServer ob=new chatServer();    } class AcceptClient extends Thread{    Socket ClientSocket;    DataInputStream din;    DataOutputStream dout;    AcceptClient (Socket CSoc) throws Exception    {        ClientSocket=CSoc;         din=new DataInputStream(ClientSocket.getInputStream());        dout=new DataOutputStream(ClientSocket.getOutputStream());                String LoginName=din.readUTF();         System.out.println("User Logged In :" + LoginName);        LoginNames.add(LoginName);        ClientSockets.add(ClientSocket);            start();    }     publicvoid run()    {        while(true)        {                        try            {                String msgFromClient=new String();                msgFromClient=din.readUTF();                StringTokenizer st=new StringTokenizer(msgFromClient);                String Sendto=st.nextToken();                                String MsgType=st.nextToken();                int iCount=0;                    if(MsgType.equals("LOGOUT"))                {                    for(iCount=0;iCount<LoginNames.size();iCount++)                    {                        if(LoginNames.elementAt(iCount).equals(Sendto))                        {                            LoginNames.removeElementAt(iCount);                            ClientSockets.removeElementAt(iCount);                            System.out.println("User " + Sendto +" Logged Out ...");                            break;                        }                    }                    }                else                {                    String msg="";                    while(st.hasMoreTokens())                    {                        msg=msg+" " +st.nextToken();                    }                    for(iCount=0;iCount<LoginNames.size();iCount++)                    {                        if(LoginNames.elementAt(iCount).equals(Sendto))                        {                                Socket tSoc=(Socket)ClientSockets.elementAt(iCount);                                                        DataOutputStream tdout=new DataOutputStream(tSoc.getOutputStream());                            tdout.writeUTF(msg);                                                        break;                        }                    }                    if(iCount==LoginNames.size())                    {                        dout.writeUTF("I am offline");                    }                    else                    {                                            }                }                if(MsgType.equals("LOGOUT"))                {                    break;                }             }            catch(Exception ex)            {                ex.printStackTrace();            }                                            }            }}}

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Dear all,
    Ý tưởng chat nhiều client như sau nhé:
    sever :
    - bạn start 1 listener ( kế thừa runable và while = true )
    - Nếu bạn thấy 1 connection đến thì bạn apcept nó và đẩy vào queue sử lý
    - 1 tiến trình sử lý lấy từ queue ra và start 1 thread để đáp ứng với yêu cầu client

    Đây là cách đơn giản nhất cho mutithread nhé bạn, bạn làm thử nếu mắc gọi mình hỗ trợ nhé. Sau khi làm ok bạn có thể tham khảo các giải pháp khách như java nio, threadpool

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •