前一篇讲了Android的网络编程基础,今天写了一个简单的聊天程序分享一下
首先是服务端代码:
1 package com.jiao.socketdemo; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.net.ServerSocket; 9 import java.net.Socket; 10 11 public class ChatServer extends Thread { 12 private ServerSocket server = null; 13 private static final int PORT = 5550; 14 private BufferedWriter writer; 15 private BufferedReader reader; 16 17 public ChatServer() throws IOException { 18 19 CreateSocket();//初始化socket 20 } 21 22 @Override 23 public void run() { 24 25 Socket client; 26 String txt; 27 28 try { 29 while (true) { 30 // 获取应答消息 31 client = ResponseSocket(); 32 while (true) { 33 // 接受客户端信息 34 txt = ReceiveMsg(client); 35 36 System.out.println("客户端发来的消息:" + txt); 37 38 SendMsg(client, txt); 39 40 if (true) 41 break; 42 } 43 CloseSocket(client); 44 } 45 } catch (Exception e) { 46 // TODO: handle exception 47 } 48 49 } 50 51 //关闭连接 52 private void CloseSocket(Socket client) throws IOException { 53 reader.close(); 54 writer.close(); 55 client.close(); 56 57 } 58 59 //封装发送消息的方法 60 private void SendMsg(Socket socket, String Msg) throws IOException { 61 writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 62 writer.write(Msg + "\n"); 63 writer.flush(); 64 } 65 66 //封装接受消息的方法 67 private String ReceiveMsg(Socket socket) throws IOException { 68 reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 69 String txt = reader.readLine(); 70 return txt; 71 } 72 73 //接受消息 74 private Socket ResponseSocket() throws IOException { 75 Socket client = server.accept(); 76 return client; 77 } 78 79 //初始化socket 80 private void CreateSocket() throws IOException { 81 server = new ServerSocket(PORT, 100); 82 } 83 84 }
启动服务端的代码如下:
1 // 创建聊天服务器 2 ChatServer chatserver = new ChatServer(); 3 // 检测服务器是否已经启动,如果没有则启动服务器 4 if (chatserver != null) { 5 chatserver.start(); 6 }
以下是客户端的代码:
1 package com.example.socketclient; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.net.Socket; 9 import java.net.UnknownHostException; 10 import android.app.Activity; 11 import android.os.Bundle; 12 13 public class MainActivity extends Activity { 14 public static int SERVER_PORT = 5550; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 new Thread() { 22 public void run() { 23 message(); 24 } 25 }.start(); 26 } 27 28 private void message() { 29 try { 30 Socket client = RequestSocket("这里是服务器的IP", SERVER_PORT); 31 SendMsg(client, "client send"); 32 String txt = ReceiveMsg(client); 33 System.out.println("服务端返回数据:" + txt); 34 } catch (Exception e) { 35 36 } 37 }; 38 39 // 客户端获取消息类 40 private Socket RequestSocket(String host, int port)throws UnknownHostException, IOException { 41 Socket socket = new Socket(host, port); 42 return socket; 43 } 44 45 // 客户端发送消息类 46 private void SendMsg(Socket socket, String msg) throws IOException { 47 // 将要发送的消息写入Buffer 中 48 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 49 // 格式转换 50 writer.write(msg.replace("\n", " ") + "\n"); 51 // 刷新,发送 52 writer.flush(); 53 } 54 55 // 客户端接收消息 56 private String ReceiveMsg(Socket socket) throws IOException { 57 // 写入读Buffer 中// 获取接收的消息到数据流中 58 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 59 // 读取消息到字符串中 60 String Msg = reader.readLine(); 61 // 以字符串的方式返回消息 62 return Msg; 63 } 64 65 }
时间: 2024-10-12 08:18:05