[Java] Kết nối máy server đến client qua socket trong java

Thảo luận trong 'Java' bắt đầu bởi Trần Văn Cường, 9/3/17.

  1. Trần Văn Cường

    Trần Văn Cường I love CNTT Thành viên BQT Thành viên BQT

    Tham gia ngày:
    8/11/15
    Bài viết:
    3,693
    Đã được thích:
    43
    Điểm thành tích:
    48
    Giới tính:
    Nam
    Nghề nghiệp:
    Sinh Viên
    Nơi ở:
    Quảng Ninh thân yêu!
    Web:

    Server:
    CODE:
    Select All
    package Socket;
    import java.io.IOException;
    import java.net.*;
    import java.util.Scanner;
    import java.io.*;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class 
    Server {

        public static 
    void main(String[]ds)   {
        try{
               
    ServerSocket svsocket = new ServerSocket(1234);
               
    Socket socket svsocket.accept();
               
    System.out.print("đã kết nối với client");
              final 
    BufferedReader read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              
    BufferedWriter write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
             
    Thread T_read = new Thread(new Runnable()
             {
                 @
    Override
                 
    public void run (){
                     while(
    true)
                     try{
                         
    String mes 
                                 
    mesread.readLine();
                                 if  (
    mes!=null&& !mes.isEmpty())
                                 {
                                     
    System.out.println(mes);
                                 }
                     }catch (
    IOException ex)
                     {
                         
    System.out.println(ex);
                     }
                         }
              });
                     
    T_read.start();
                     while(
    true){
                         
    System.out.println("cnttqn.com");
                         
    String mes=new Scanner(System.in).nextLine();
                                 
    write.write(mes);
                                 
    write.newLine();
                                 
    write.flush();
                                         }}
                     catch(
    IOException ex)
                             {
                             
    System.out.println(ex);
                             }
        }
    }
    Client:
    CODE:
    Select All
    package Socket;
    import java.io.IOException;
    import java.net.*;
    import java.util.Scanner;
    import java.io.*;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class 
    client {
        public static 
    void main(String[]ds){
            
                try {
                    
    Socket socket = new Socket("localhost",1234);
             final 
    BufferedReader read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             
    BufferedWriter write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
             
    Thread T_read = new Thread(new Runnable()
             {
                 @
    Override
                 
    public void run (){
                     while(
    true)
                     try{
                         
    String mes 
                                 
    mesread.readLine();
                                 if  (
    mes!=null&& !mes.isEmpty())
                                 {
                                     
    System.out.println(mes);
                                 }
                     }catch (
    IOException ex)
                     {
                         
    System.out.println(ex);
                     }
                         }
              });
                     
    T_read.start();
                     while(
    true){
                         
    System.out.println("cntt");
                         
    String mes=new Scanner(System.in).nextLine();
                                 
    write.write(mes);
                                 
    write.newLine();
                                 
    write.flush();
                                         }}
                catch(
    IOException ex){
               
    System.out.print(ex);
               }
      
    }
    }
    Hướng dẫn chi tiết bằng Video

     

    Bình Luận Bằng Facebook

    data-href="https://cnttqn.com/threads/java-ket-noi-may-server-den-client-qua-socket-trong-java.4085.html"