Android socket 局域网通信
1.客户端ACG(服务端类似)
本来是想仿照手机软件的,没成功,这个app有过渡界面(SplashActivity)还有主界面(HomeActivity),主界面仿照微信的界面滑动功能(左右滑动切换界面)。
1.1.创建程序,编写界面代码(fragment_acg.xml)
界面只需要两个textview(一个显示客户端发出的信息,另一个显示客户端收到服务的发来的信息),一个edittext和一个button。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp">
<LinearLayout
android:id="@+id/lll"
android:padding="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DADADA"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:id="@+id/bt"
android:layout_toRightOf="@id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>
</RelativeLayout>
1.2.编写界面交互代码(AcgFragment.class)
使用socket是需要在子线程中使用,不能在主线程中(主线程不能访问网络),子线程中使用socket更新ui时在通过Message、Handle发送消息队列到主线程进行ui更新。
public class AcgFragment extends Fragment implements View.OnClickListener {
private View v;
private EditText editText;
private Button button;
private TextView textView,textViewv;
private Socket socket;
private String time;
private String content;
private Handler handler;
private OutputStream os;
private InputStream is;
private BufferedReader reader;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_acg, container, false);
initView();
handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
textView.append("\n那个人:" + msg.obj.toString() + "\n\n");
break;
case 1:
textViewv.append("\n\n这个人:" + msg.obj.toString() + "\n");
break;
}
}
};
return v;
}
private void initView() {
editText = v.findViewById(R.id.et);
button = v.findViewById(R.id.bt);
button.setOnClickListener(this);
textView = v.findViewById(R.id.tv);
textViewv = v.findViewById(R.id.tvv);
new Thread() {
@Override
public void run() {
super.run();
try {
//第一个参数是指定服务端的IP地址,第二个是端口(服务端的也要一样)
socket = new Socket("192.168.1.6", 2500);
os = socket.getOutputStream();
is = socket.getInputStream();
//接收服务端发来的信息
reader = new BufferedReader(new InputStreamReader(is));
while (true) {
Message msg = new Message();
msg.what = 0;
msg.obj = reader.readLine();
//当接收服务端信息成功时,子线程发信息到主线程更新ui
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void ClientSend() {
//回复服务端
new Thread() {
public void run() {
try {
os.write((content + "\n").getBytes("UTF-8"));
Message msg = new Message();
msg.what = 1;
msg.obj = content ;
//当发送信息到服务端成功时,子线程发信息到主线程更新ui
handler.sendMessage(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public void onClick(View v) {
content = editText.getText().toString();
editText.setText("");
if(content!=""){
ClientSend();
}
}
}
1.3.添加访问网络权限
使用socket需要网络操作,所以要添加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>