1、效果如下:
1.软件没有单独的服务端与客户端,不需要电脑充当服务器,代码简单。
2.可以向自己发送信息
3.第一个输入框是发送信息的目标IP,第二个是发送信息
4.最下面是接收信息。
5.使用的是Socket。
2.代码
1.AndroidManifest.xml (先添加权限)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
具体在AndroidManifest.xml如下位置:
2.activity_main.xml (在layout下添加该布局文件,默认有所以替换代码就可以)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="NetChat"
android:textSize="40sp" />
<TextView
android:id="@+id/text_showip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ip地址无"
android:gravity="center"
android:textSize="20sp"
android:layout_marginBottom="10dp"
/>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="发送信息" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10sp"
>
<EditText
android:id="@+id/edit_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="输入目标ip地址" />
<EditText
android:id="@+id/edit_sendsome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="输入发送的信息" />
<TextView
android:id="@+id/text_showmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="没有收到信息"
android:gravity="center"
android:textSize="20sp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</LinearLayout>
3.MainActivity.java (默认有,只需要替换代码,注意要导入自己的包名)
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button send;
EditText edit_ip, edit_someSend;
TextView ip_show, msg_show;
Handler Handler;
ServerSocket server = null;
Socket accept = null;
InputStream inputStream = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_layout();//初始化页面
init_Handler();//初始化线程
handleServer();//接受线程
}
@Override
protected void onPause() {
super.onPause();
Handler = null;
System.out.println("onPause");
}
@Override
protected void onStart() {
super.onStart();
init_Handler();//防止null
System.out.println("onStart");
}
//页面初始
public void init_layout() {
send = findViewById(R.id.btn_send);
edit_ip = findViewById(R.id.edit_ip);
edit_someSend = findViewById(R.id.edit_sendsome);
ip_show = findViewById(R.id.text_showip);
msg_show = findViewById(R.id.text_showmsg);
send.setOnClickListener(this);
edit_ip.setText(Getip(MainActivity.this));
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_send) {
handleSend(edit_someSend.getText().toString());
ip_show.setText("你的ip:" + Getip(MainActivity.this));
}
}
public void handleServer() {
new Thread() {
@Override
public void run() {
Server();
}
}.start();
}
public void handleSend(String msg) {
new Thread() {
@Override
public void run() {
Send(msg);
}
}.start();
}
//发送信息函数
public void Send(String msg) {
//创建套接字 确定要连接的服务器ip与端口口
if (msg.equals("")) return;
Socket socket = null;
try {
socket = new Socket(edit_ip.getText().toString(), 12450);
InputStream inputStream = null;
System.out.println("发送:" + msg);
inputStream = socket.getInputStream();
//向服务器发送消息
OutputStream outputStream = null;
outputStream = socket.getOutputStream();
outputStream.write(msg.getBytes("utf-8"));
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//接受信息函数
public void Server() {
// 创建服务器端
String msgContent;
try {
server = new ServerSocket(12450);
// 等待连接
while (true) {
accept = server.accept();
// 读取数据
inputStream = accept.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while (((msgContent) = reader.readLine()) != null) {
System.out.println("服务器接受:" + msgContent);
Message msg = new Message();
msg.what = 1;
msg.obj = msgContent;
Handler.sendMessage(msg);
}
}
} catch (
IOException e) {
e.printStackTrace();
}
}
//线程函数
@SuppressLint("HandlerLeak")
private void init_Handler() {
Handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
if (msg.obj != null) {
msg_show.setText(msg.obj.toString());
}
break;
default:
break;
}
}
};
}
/**
*///获取设备IP
public String Getip(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = Formatter.formatIpAddress(ipAddress);
return ip;
}
}
欧克了,哈哈,很简单,系不系。点个关注吧。