Android 网络编程基础之简单聊天程序

时间:2023-02-13 18:37:02

前一篇讲了Android的网络编程基础,今天写了一个简单的聊天程序分享一下

首先是服务端代码:

 package com.jiao.socketdemo;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket; public class ChatServer extends Thread {
private ServerSocket server = null;
private static final int PORT = 5550;
private BufferedWriter writer;
private BufferedReader reader; public ChatServer() throws IOException { CreateSocket();//初始化socket
} @Override
public void run() { Socket client;
String txt; try {
while (true) {
// 获取应答消息
client = ResponseSocket();
while (true) {
// 接受客户端信息
txt = ReceiveMsg(client); System.out.println("客户端发来的消息:" + txt); SendMsg(client, txt); if (true)
break;
}
CloseSocket(client);
}
} catch (Exception e) {
// TODO: handle exception
} } //关闭连接
private void CloseSocket(Socket client) throws IOException {
reader.close();
writer.close();
client.close(); } //封装发送消息的方法
private void SendMsg(Socket socket, String Msg) throws IOException {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(Msg + "\n");
writer.flush();
} //封装接受消息的方法
private String ReceiveMsg(Socket socket) throws IOException {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String txt = reader.readLine();
return txt;
} //接受消息
private Socket ResponseSocket() throws IOException {
Socket client = server.accept();
return client;
} //初始化socket
private void CreateSocket() throws IOException {
server = new ServerSocket(PORT, 100);
} }

启动服务端的代码如下:

 // 创建聊天服务器
ChatServer chatserver = new ChatServer();
// 检测服务器是否已经启动,如果没有则启动服务器
if (chatserver != null) {
chatserver.start();
}

以下是客户端的代码:

 package com.example.socketclient;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
public static int SERVER_PORT = 5550; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); new Thread() {
public void run() {
message();
}
}.start();
} private void message() {
try {
Socket client = RequestSocket("这里是服务器的IP", SERVER_PORT);
SendMsg(client, "client send");
String txt = ReceiveMsg(client);
System.out.println("服务端返回数据:" + txt);
} catch (Exception e) { }
}; // 客户端获取消息类
private Socket RequestSocket(String host, int port)throws UnknownHostException, IOException {
Socket socket = new Socket(host, port);
return socket;
} // 客户端发送消息类
private void SendMsg(Socket socket, String msg) throws IOException {
// 将要发送的消息写入Buffer 中
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 格式转换
writer.write(msg.replace("\n", " ") + "\n");
// 刷新,发送
writer.flush();
} // 客户端接收消息
private String ReceiveMsg(Socket socket) throws IOException {
// 写入读Buffer 中// 获取接收的消息到数据流中
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 读取消息到字符串中
String Msg = reader.readLine();
// 以字符串的方式返回消息
return Msg;
} }