HỎI Ứng dụng console - Chat Client vs Server

pirate91

New Member
5/1/20
3
1
3
30
Chào các bác,
Tình hình là em đang học viết 1 chương trình chat giữa Client và Server nhưng em chưa biết làm thế nào để có thể nhận và gửi tin nhắn liên tục mà chỉ có thể gửi và nhận từng tin nhắn xen kẽ nhau
Mong các bác giúp đỡ !!!

Client
Java:
package baitap.cuoichuong.ClientServer;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ClientExample {

    private String svName;
    private int port;
    private Socket client;
    private OutputStream outputToServer;
    private DataOutputStream output;
    private InputStream inputFromServer;
    private DataInputStream input;
    private String msg;
    private boolean status = false;

    public ClientExample() {

    }

    public void nhap() {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhap IP server: ");
        svName = sc.nextLine();
        System.out.print("Nhap Port servr: ");
        port = sc.nextInt();
    }

    private void connectToServer() {
        nhap();

        try {
            client = new Socket(InetAddress.getByName(svName), port);
            System.out.println("Ket noi toi " + svName + " tai port " + port);
            status = true;
        } catch (UnknownHostException e) {
            System.out.println("Khong tim thay dia chi server!!");
        } catch (ConnectException e) {
            System.out.println("Khong ket noi duoc voi server " + svName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(client.getOutputStream());
            output.flush();

            input = new DataInputStream(client.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + client.getInetAddress().getHostName());

        } catch (ConnectException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void sendMessages() {
        Scanner sc = new Scanner(System.in);
        do {

            System.out.print(client.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();

            try {
                output.writeUTF(msg);
                output.flush();
                System.out.print("");
                receiveMessages();
                msg = sc.nextLine();

            } catch (Exception e) {
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!!!");
            }

        } while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
        do {
            try {
                msg = input.readUTF();
                System.out.println(msg);

                sendMessages();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KET NOI!");
            }

        } while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        connectToServer();
        if (status == true) {
            getStreams();
            sendMessages();
            receiveMessages();
        }
    }
}
Server
Java:
package baitap.cuoichuong.ClientServer;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;

public class ServerExample {
    private ServerSocket svSocket;
    private Socket sv;
    private DataOutputStream output;
    private DataInputStream input;
    private boolean status = false;
    private String msg;

    public ServerExample() {
    }

    public ServerExample(int port) throws IOException {
        svSocket = new ServerSocket(port);
        svSocket.setSoTimeout(30000);
    }

    private boolean waitForConnection() {

        System.out.println("Dang cho ket noi tai port "
                + svSocket.getLocalPort() + "...");

        try {
            sv = svSocket.accept();
            System.out.println("Da thiet lap ket noi voi "
                    + sv.getInetAddress().getHostName());
            status = true;

        } catch (SocketTimeoutException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return status;
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(sv.getOutputStream());
            output.flush();

            input = new DataInputStream(sv.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + sv.getInetAddress().getHostName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMessages() {
        Scanner sc = new Scanner(System.in);

        do {
            System.out.print(sv.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();

            try {
                output.writeUTF(sv.getLocalAddress().getHostName() + " >>> "
                        + msg);
                output.flush();
                System.out.println("");
                receiveMessages();
                msg = sc.nextLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!");
            }
        } while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
        do {
            try {
                msg = input.readUTF();
                System.out.println(sv.getInetAddress().getHostName() + " >>> "
                        + msg);
                sendMessages();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KHONG DOC DUOC TIN NHAN!");
            }
        } while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        waitForConnection();

        if (status == true) {
            getStreams();
            sendMessages();
            receiveMessages();
        }
    }
}
 

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Your codes have a lot of redundancy and the communication flow is a bit unsynchronized. That is the reason why the codes won't work properly. The fundamental communication flow is:
1) server startet and wait for clients BEFORE it can communicate with them. Your server startet and want to send message...to WHOM? And that is the problem on the server site.
2) client and server communicate alternately. Meaning: client sends -> server listens, server sends -> client listens.
3) the send method shouldn't run in a loop or embed the receiving. It only confuses the socket communication.

I have modified your codes and make them working.
Java:
public class ServerExample {
    private ServerSocket svSocket;
    private Socket sv;
    private DataOutputStream output;
    private DataInputStream input;
    private boolean status = false;
    private String msg;
    public static void main(String... a) throws Exception {
      (new ServerExample(1000)).run();
    }

    public ServerExample() {
    }

    public ServerExample(int port) throws IOException {
        svSocket = new ServerSocket(port);
        svSocket.setSoTimeout(30000);
    }

    private boolean waitForConnection() {

        System.out.println("Dang cho ket noi tai port "
                + svSocket.getLocalPort() + "...");

        try {
            sv = svSocket.accept();
            System.out.println("Da thiet lap ket noi voi "
                    + sv.getInetAddress().getHostName());
            status = true;

        } catch (SocketTimeoutException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return status;
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(sv.getOutputStream());
            output.flush();

            input = new DataInputStream(sv.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + sv.getInetAddress().getHostName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMessages() {
        Scanner sc = new Scanner(System.in);

        //do {
            System.out.print(sv.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();
            if ("EXIT!!!".equals(msg)) System.exit(0);

            try {
                output.writeUTF(sv.getLocalAddress().getHostName() + " >>> "
                        + msg);
                output.flush();
                //System.out.println("");
                //receiveMessages();
                //msg = sc.nextLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!");
            }
        //} while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
        //do {
        while (true) {
            try {
                msg = input.readUTF();
                System.out.println(sv.getInetAddress().getHostName() + " >>> "
                        + msg);
                sendMessages();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KHONG DOC DUOC TIN NHAN!");
            }
        }
        //} while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        waitForConnection();

        if (status == true) {
            getStreams();
            //sendMessages();   // causes Hanging....
            receiveMessages();
        }
    }
}
Java:
public class ClientExample {

    private String svName;
    private int port;
    private Socket client;
    private OutputStream outputToServer;
    private DataOutputStream output;
    private InputStream inputFromServer;
    private DataInputStream input;
    private String msg;
    private boolean status = false;
    public static void main(String... a) throws Exception {
      (new ClientExample()).run();
    }

    public ClientExample() {

    }

    public void nhap() {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhap IP server: ");
        svName = sc.nextLine();
        System.out.print("Nhap Port servr: ");
        port = sc.nextInt();
    }

    private void connectToServer() {
        nhap();

        try {
            client = new Socket(InetAddress.getByName(svName), port);
            System.out.println("Ket noi toi " + svName + " tai port " + port);
            status = true;
        } catch (UnknownHostException e) {
            System.out.println("Khong tim thay dia chi server!!");
        } catch (ConnectException e) {
            System.out.println("Khong ket noi duoc voi server " + svName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(client.getOutputStream());
            output.flush();

            input = new DataInputStream(client.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + client.getInetAddress().getHostName());

        } catch (ConnectException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void sendMessages() {
        Scanner sc = new Scanner(System.in);
        //do {

            System.out.print(client.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();
            if ("EXIT!!!".equals(msg)) System.exit(0);

            try {
                output.writeUTF(msg);
                output.flush();
                //System.out.print("");
                //receiveMessages();
                //msg = sc.nextLine();

            } catch (Exception e) {
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!!!");
            }

        //} while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
      //do {
        while (true) {
            try {
                msg = input.readUTF().trim();
                System.out.println(msg);
                 if ("EXIT!!!".equals(msg)) return;
                sendMessages();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KET NOI!");
            }

        } 
        //} while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        connectToServer();
        if (status == true) {
            getStreams();
            sendMessages();
            receiveMessages();
        }
    }
}
 
  • Like
Reactions: pirate91

pirate91

New Member
5/1/20
3
1
3
30
Your codes have a lot of redundancy and the communication flow is a bit unsynchronized. That is the reason why the codes won't work properly. The fundamental communication flow is:
1) server startet and wait for clients BEFORE it can communicate with them. Your server startet and want to send message...to WHOM? And that is the problem on the server site.
2) client and server communicate alternately. Meaning: client sends -> server listens, server sends -> client listens.
3) the send method shouldn't run in a loop or embed the receiving. It only confuses the socket communication.

I have modified your codes and make them working.
Java:
public class ServerExample {
    private ServerSocket svSocket;
    private Socket sv;
    private DataOutputStream output;
    private DataInputStream input;
    private boolean status = false;
    private String msg;
    public static void main(String... a) throws Exception {
      (new ServerExample(1000)).run();
    }

    public ServerExample() {
    }

    public ServerExample(int port) throws IOException {
        svSocket = new ServerSocket(port);
        svSocket.setSoTimeout(30000);
    }

    private boolean waitForConnection() {

        System.out.println("Dang cho ket noi tai port "
                + svSocket.getLocalPort() + "...");

        try {
            sv = svSocket.accept();
            System.out.println("Da thiet lap ket noi voi "
                    + sv.getInetAddress().getHostName());
            status = true;

        } catch (SocketTimeoutException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return status;
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(sv.getOutputStream());
            output.flush();

            input = new DataInputStream(sv.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + sv.getInetAddress().getHostName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMessages() {
        Scanner sc = new Scanner(System.in);

        //do {
            System.out.print(sv.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();
            if ("EXIT!!!".equals(msg)) System.exit(0);

            try {
                output.writeUTF(sv.getLocalAddress().getHostName() + " >>> "
                        + msg);
                output.flush();
                //System.out.println("");
                //receiveMessages();
                //msg = sc.nextLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!");
            }
        //} while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
        //do {
        while (true) {
            try {
                msg = input.readUTF();
                System.out.println(sv.getInetAddress().getHostName() + " >>> "
                        + msg);
                sendMessages();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KHONG DOC DUOC TIN NHAN!");
            }
        }
        //} while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        waitForConnection();

        if (status == true) {
            getStreams();
            //sendMessages();   // causes Hanging....
            receiveMessages();
        }
    }
}
Java:
public class ClientExample {

    private String svName;
    private int port;
    private Socket client;
    private OutputStream outputToServer;
    private DataOutputStream output;
    private InputStream inputFromServer;
    private DataInputStream input;
    private String msg;
    private boolean status = false;
    public static void main(String... a) throws Exception {
      (new ClientExample()).run();
    }

    public ClientExample() {

    }

    public void nhap() {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhap IP server: ");
        svName = sc.nextLine();
        System.out.print("Nhap Port servr: ");
        port = sc.nextInt();
    }

    private void connectToServer() {
        nhap();

        try {
            client = new Socket(InetAddress.getByName(svName), port);
            System.out.println("Ket noi toi " + svName + " tai port " + port);
            status = true;
        } catch (UnknownHostException e) {
            System.out.println("Khong tim thay dia chi server!!");
        } catch (ConnectException e) {
            System.out.println("Khong ket noi duoc voi server " + svName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getStreams() {
        try {
            output = new DataOutputStream(client.getOutputStream());
            output.flush();

            input = new DataInputStream(client.getInputStream());

            System.out.println("DA THIET LAP I/O STREAMS VOI "
                    + client.getInetAddress().getHostName());

        } catch (ConnectException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void sendMessages() {
        Scanner sc = new Scanner(System.in);
        //do {

            System.out.print(client.getLocalAddress().getHostName() + " >>> ");
            msg = sc.nextLine();
            if ("EXIT!!!".equals(msg)) System.exit(0);

            try {
                output.writeUTF(msg);
                output.flush();
                //System.out.print("");
                //receiveMessages();
                //msg = sc.nextLine();

            } catch (Exception e) {
                System.out.println("LOI KHONG GUI DUOC TIN NHAN!!!");
            }

        //} while (msg.equals("EXIT!!!") == false);
    }

    private void receiveMessages() {
      //do {
        while (true) {
            try {
                msg = input.readUTF().trim();
                System.out.println(msg);
                 if ("EXIT!!!".equals(msg)) return;
                sendMessages();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.err.println("LOI KET NOI!");
            }

        }
        //} while (msg.equals("EXIT!!!") == false);
    }

    public void run() {
        connectToServer();
        if (status == true) {
            getStreams();
            sendMessages();
            receiveMessages();
        }
    }
}
Thank you very much, but I want to make my app can send messages continuously, example:
Client >>> Cộng đồng Java
Client >>> pirate91
Client >>> Joe Vip
Do you have any way ???
Thanks !!!
 

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Try this:
Java:
import java.io.*;
import java.net.*;

public class ClientExample extends Thread{

    private String svName;
    private int port;
    private Socket client;
    private OutputStream outputToServer;
    private DataOutputStream output;
    private InputStream inputFromServer;
    private DataInputStream input;
    private String msg;
    private boolean status = false;
    public static void main(String... a) throws Exception {
      (new ClientExample()).start();
    }

    public ClientExample() {

    }

    public void run() {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        System.out.print("Nhap IP server: ");
        svName = sc.nextLine();
        System.out.print("Nhap Port servr: ");
        port = sc.nextInt();
        try {
            client = new Socket(InetAddress.getByName(svName), port);
            System.out.println("Ket noi toi " + svName + " tai port " + port);
            InputStream is = client.getInputStream();
            OutputStream os = client.getOutputStream();
            String ls = System.lineSeparator();
            byte[] buf = new byte[256];
            int nl = ls.length();
            while (true) {
              System.out.print("Send to Server:");
              int le = System.in.read(buf);
              if (le <= nl) continue;
              os.write(buf, 0, le);
              os.flush();
              // listen to server
              String msg = new String(buf, 0, le-nl).trim();
              if ("EXIT".equalsIgnoreCase(msg)) break;
              for (int i = 0; i < le; ++i) buf[i] = ' '; // clear buf
              le = is.read(buf);
              msg = new String(buf, 0, le).trim();
              System.out.print("Received from Server:"+msg+ls+"Reply toServer:");
              if ("EXIT".equalsIgnoreCase(msg)) break;
            }
            is.close();
            os.close();
        } catch (UnknownHostException e) {
            System.out.println("Khong tim thay dia chi server!!");
        } catch (ConnectException e) {
            System.out.println("Khong ket noi duoc voi server " + svName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Java:
import java.io.*;
import java.net.*;

public class ServerExample {
    private ServerSocket svSocket;
    private Socket sv;
    private DataOutputStream output;
    private DataInputStream input;
    private boolean status = false;
    private String msg;
 
    private class Peer extends Thread {
      public Peer(Socket soc) {
        this.soc = soc;
      }
      private Socket soc;
      public void run() {
        try {
          byte[] buf = new byte[256];
          InputStream is = soc.getInputStream();
          OutputStream os = soc.getOutputStream();
          String ls = System.lineSeparator();
          int nl = ls.length();
          long id = Thread.currentThread().getId();
          while (true) {
            int le = is.read(buf);
            if (le <= nl) continue;
            String msg = new String(buf, 0, le-nl).trim();
            System.out.print("Received from Client "+id+":"+msg+ls+"Reply to:");
            for (int i = 0; i < le; ++i) buf[i] = ' '; // clear buf
            le = System.in.read(buf);
            if (le <= nl) continue;
            os.write(buf, 0, le);
            os.flush();
            if ("EXIT".equalsIgnoreCase(new String(buf, 0, le-nl).trim())) break;
          }
          is.close();
          os.close();
        } catch (Exception ex) {
          System.out.println("Exception:"+ex.toString());
        }
      }
    }
    
    public static void main(String... a) throws Exception {
      (new ServerExample(1000)).waitForClient();
    }

    public ServerExample() {
    }

    public ServerExample(int port) throws IOException {
        svSocket = new ServerSocket(port);
        //svSocket.setSoTimeout(30000);
    }

    private void waitForClient() {

        System.out.println("Dang cho ket noi tai port "
                + svSocket.getLocalPort() + "...");

        try {
            // start to server Client
            while (true) {
              Socket soc = svSocket.accept();
              System.out.println("Da thiet lap ket noi voi "
                    + soc.getInetAddress().getHostName());
              (new Peer(soc)).start();
            }

        } catch (SocketTimeoutException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Server: Multi-threading. Client: single Thread
CS.jpg
 
  • Like
Reactions: pirate91

pirate91

New Member
5/1/20
3
1
3
30
Try this:
Java:
import java.io.*;
import java.net.*;

public class ClientExample extends Thread{

    private String svName;
    private int port;
    private Socket client;
    private OutputStream outputToServer;
    private DataOutputStream output;
    private InputStream inputFromServer;
    private DataInputStream input;
    private String msg;
    private boolean status = false;
    public static void main(String... a) throws Exception {
      (new ClientExample()).start();
    }

    public ClientExample() {

    }

    public void run() {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        System.out.print("Nhap IP server: ");
        svName = sc.nextLine();
        System.out.print("Nhap Port servr: ");
        port = sc.nextInt();
        try {
            client = new Socket(InetAddress.getByName(svName), port);
            System.out.println("Ket noi toi " + svName + " tai port " + port);
            InputStream is = client.getInputStream();
            OutputStream os = client.getOutputStream();
            String ls = System.lineSeparator();
            byte[] buf = new byte[256];
            int nl = ls.length();
            while (true) {
              System.out.print("Send to Server:");
              int le = System.in.read(buf);
              if (le <= nl) continue;
              os.write(buf, 0, le);
              os.flush();
              // listen to server
              String msg = new String(buf, 0, le-nl).trim();
              if ("EXIT".equalsIgnoreCase(msg)) break;
              for (int i = 0; i < le; ++i) buf[i] = ' '; // clear buf
              le = is.read(buf);
              msg = new String(buf, 0, le).trim();
              System.out.print("Received from Server:"+msg+ls+"Reply toServer:");
              if ("EXIT".equalsIgnoreCase(msg)) break;
            }
            is.close();
            os.close();
        } catch (UnknownHostException e) {
            System.out.println("Khong tim thay dia chi server!!");
        } catch (ConnectException e) {
            System.out.println("Khong ket noi duoc voi server " + svName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Java:
import java.io.*;
import java.net.*;

public class ServerExample {
    private ServerSocket svSocket;
    private Socket sv;
    private DataOutputStream output;
    private DataInputStream input;
    private boolean status = false;
    private String msg;

    private class Peer extends Thread {
      public Peer(Socket soc) {
        this.soc = soc;
      }
      private Socket soc;
      public void run() {
        try {
          byte[] buf = new byte[256];
          InputStream is = soc.getInputStream();
          OutputStream os = soc.getOutputStream();
          String ls = System.lineSeparator();
          int nl = ls.length();
          long id = Thread.currentThread().getId();
          while (true) {
            int le = is.read(buf);
            if (le <= nl) continue;
            String msg = new String(buf, 0, le-nl).trim();
            System.out.print("Received from Client "+id+":"+msg+ls+"Reply to:");
            for (int i = 0; i < le; ++i) buf[i] = ' '; // clear buf
            le = System.in.read(buf);
            if (le <= nl) continue;
            os.write(buf, 0, le);
            os.flush();
            if ("EXIT".equalsIgnoreCase(new String(buf, 0, le-nl).trim())) break;
          }
          is.close();
          os.close();
        } catch (Exception ex) {
          System.out.println("Exception:"+ex.toString());
        }
      }
    }
   
    public static void main(String... a) throws Exception {
      (new ServerExample(1000)).waitForClient();
    }

    public ServerExample() {
    }

    public ServerExample(int port) throws IOException {
        svSocket = new ServerSocket(port);
        //svSocket.setSoTimeout(30000);
    }

    private void waitForClient() {

        System.out.println("Dang cho ket noi tai port "
                + svSocket.getLocalPort() + "...");

        try {
            // start to server Client
            while (true) {
              Socket soc = svSocket.accept();
              System.out.println("Da thiet lap ket noi voi "
                    + soc.getInetAddress().getHostName());
              (new Peer(soc)).start();
            }

        } catch (SocketTimeoutException e) {
            System.out.println("Qua thoi gian cho!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Server: Multi-threading. Client: single Thread
View attachment 3135
Thank you very much !!!
 

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
My pleasure. However if you are interested in GUI Chatting using MulticastingSocket then click and down load the zip @HERE