和客户端线程,也就是刚才说的子线程代码:

  1. ackage com.example.eetalk;   
  2.    
  3. import java.io.DataInputStream;   
  4. import java.io.DataOutputStream;   
  5. import java.io.IOException;   
  6. import java.net.Socket;   
  7. import java.net.UnknownHostException;   
  8.    
  9. import android.os.Handler;   
  10. import android.os.Message;   
  11.    
  12. public class ClientThread extends Thread {   
  13.     private Handler handler;   
  14.     private DataOutputStream dos;   
  15.     private DataInputStream dis;   
  16.     private Socket socket;   
  17.    
  18.     public ClientThread(Handler handler) {   
  19.         this.handler = handler;   
  20.     }   
  21.    
  22.     public void run() {   
  23.         try {   
  24.             connect();   
  25.             dos.writeInt(1);   
  26.             dos.writeInt(5);   
  27.             dos.write("hello".getBytes());   
  28.             while (true) {   
  29.                 //读取服务器发过来的数据   
  30.                 int msgType = dis.readInt();   
  31.                 //   
  32.                 System.out.println("121");   
  33.                     //   
  34.                     int len = dis.readInt();   
  35.                     //   
  36.                     byte[] bytes = new byte[len];   
  37.                     dis.readFully(bytes);   
  38.                     //   
  39.                     String content = new String(bytes, "GB2312");   
  40.                     Message msg = new Message();   
  41.                     msg.what = 1;   
  42.                     //   
  43.                     msg.obj = content;   
  44.                     //   
  45.                     handler.sendMessage(msg);   
  46.                    
  47.             }   
  48.         } catch (IOException e) {   
  49.             e.printStackTrace();   
  50.         } finally {   
  51.             //close();   
  52.         }   
  53.    
  54.     }   
  55.    
  56.     private void connect() throws UnknownHostException, IOException {   
  57.         //   
  58.         socket = new Socket("172.27.35.3"8888);   
  59.         //   
  60.         dis = new DataInputStream(socket.getInputStream());   
  61.            
  62.         dos = new DataOutputStream(socket.getOutputStream());   
  63.    
  64.     }   
  65.    
  66.     private void close() {   
  67.         if (dis != null) {   
  68.             try {   
  69.                 dis.close();   
  70.             } catch (IOException e) {   
  71.             }   
  72.         }   
  73.         if (dos != null) {   
  74.             try {   
  75.                 dos.close();   
  76.             } catch (IOException e) {   
  77.             }   
  78.         }   
  79.         if (socket != null) {   
  80.             try {   
  81.                 socket.close();   
  82.             } catch (IOException e) {   
  83.             }   
  84.         }   
  85.     }   
  86.     //发送到服务器的   
  87.     public boolean sendText(String content){   
  88.         try {   
  89.         //   
  90.         byte[] bytes = content.getBytes("GB2312");   
  91.         //   
  92.         dos.writeInt(1);   
  93.         //   
  94.         dos.writeInt(bytes.length);   
  95.         //   
  96.         dos.write(bytes);   
  97.         dos.flush();   
  98.         return true;   
  99.         } catch (IOException e) {   
  100.         e.printStackTrace();   
  101.         }   
  102.         return false;   
  103.         }   
  104. }   

记住你的手机和你的电脑一定要在同一个局域网,这里可以看出手机的客户端做法与电脑是差不多的,上面手机客户端连接的ip地址是你的电脑连入的ipv4地址,可以在cmd命令里恰如ipconfig来搜寻。

做到这里,将你的app下载到手机,打开服务器,打开手机的应用程序,就可以聊天了,记住打开网络

美美聊天了:

这是服务器

这是手机客户端

做到这里,就可以和手机互发图片和文字啦,这是小编下期的实现目标 。


相关内容