文章目录
- 一、创建工程
- 二、添加网络权限
- 三、添加布局代码
- 四、添加逻辑代码
- 五、通信测试
- 六、源码分享
一、创建工程
二、添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
三、添加布局代码
activity_main.xml
<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"
android:background="#FFFFFF">
<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="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<EditText
android:id="@+id/ui_ipText"
android:layout_width="320dp"
android:layout_height="50dp"
android:hint="IP" />
<Button
android:id="@+id/ui_connectBtn"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="连接"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
</LinearLayout>
<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="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<EditText
android:id="@+id/ui_portText"
android:layout_width="320dp"
android:layout_height="50dp"
android:hint="Port" />
<Button
android:id="@+id/ui_disconnectBtn"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="断开"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
</LinearLayout>
<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="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<EditText
android:id="@+id/ui_receiveText"
android:layout_width="match_parent"
android:layout_height="300dp"
android:hint="receive message" />
<Button
android:id="@+id/ui_clearBtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="清除"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
</LinearLayout>
<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="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:background="#FFFFFF">
<EditText
android:id="@+id/ui_sendText"
android:layout_width="match_parent"
android:layout_height="220dp"
android:hint="send message" />
<Button
android:id="@+id/ui_sendBtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="发送"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
</LinearLayout>
</LinearLayout>
四、添加逻辑代码
MainActivity.java
package com.example.tcp_client_android;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
String _IP;
int _Port;
connectthread netThread;
Socket socket = null;
EditText IPArea = null;
EditText PortArea = null;
Button connectBtn;
Button disconnectBtn;
EditText receiveArea = null;
EditText sendArea = null;
Button clearBtn;
Button sendBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init_UI();
}
public void Init_UI() {
IPArea=findViewById(R.id.ui_ipText);
PortArea=findViewById(R.id.ui_portText);
receiveArea=findViewById(R.id.ui_receiveText);
sendArea=findViewById(R.id.ui_sendText);
connectBtn=findViewById(R.id.ui_connectBtn);
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_IP=IPArea.getText().toString();
String c=PortArea.getText().toString();
if("".equals(_IP)||"".equals(c)){
Toast.makeText(MainActivity.this,"请输入ip和端口号",Toast.LENGTH_SHORT).show();
}else{_Port=Integer.valueOf(c);
netThread = new connectthread();
netThread.start();}
}
});
disconnectBtn=findViewById(R.id.ui_disconnectBtn);
disconnectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
clearBtn=findViewById(R.id.ui_clearBtn);
clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
sendBtn=findViewById(R.id.ui_sendBtn);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sendMsg = sendArea.getText().toString();
sendAction(sendMsg);
}
});
}
//==============================================================================================
//子线程中进行网络相关操作
class connectthread extends Thread {
OutputStream outputStream=null;
InputStream inputStream=null;
@Override
public void run() {
//连接
try {
socket=new Socket(_IP, _Port);
runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
{
public void run()
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
}
});
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
{
public void run()
{
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
}
});
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
{
public void run()
{
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
}
});
}
if(socket!=null){
//获取输出流对象
try {
outputStream=socket.getOutputStream();
outputStream.write(123);
} catch (IOException e) {
e.printStackTrace();
}
try{
while (true)
{
final byte[] buffer = new byte[1024];//创建接收缓冲区
inputStream = socket.getInputStream();
final int len = inputStream.read(buffer);//数据读出来,并且返回数据的长度
runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
{
public void run()
{
// TODO Auto-generated method stub
receiveArea.append(new String(buffer,0,len)+"\r\n");
//Toast.makeText(MainActivity.this,new String(buffer,0,len)+"\r\n",Toast.LENGTH_SHORT).show();
}
});
}
}
catch (IOException e) {
}
}
};
}
public void sendAction(String msg) {
//子线程中进行网络操作
new Thread(new Runnable() {
@Override
public void run() {
if(socket!=null){
try {
netThread.outputStream.write(msg.getBytes());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}else{
runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
{
public void run()
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"请先建立连接",Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
}
五、通信测试
六、源码分享
链接:https://pan.baidu.com/s/1jqviFiIaOCKd0ucxRgQC0g 提取码:qyv8