通过Socket进行Android Studio客户端和Eclipse服务器进行数据传输
服务器代码
```java
package Serversocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Serversocket_Android {
private static final int PORT = 9999; //设置端口号
private List<Socket> mList = new ArrayList<Socket>(); //用ArrayList实现多个用户
private ServerSocket server = null;//服务器
private ExecutorService mExecutorService = null;
private String receiveMsg;//接受的信息
private String sendMsg;//发送的信息
public static void main(String[] args) {
new Serversocket_Android();
}
public Serversocket_Android() {
try {
server = new ServerSocket(PORT);
mExecutorService = Executors.newCachedThreadPool();
System.out.println("服务器已启动...");
Socket client = null;
while (true) {
client = server.accept();
mList.add(client);//客户端加入ArrayList
mExecutorService.execute(new Service(client));
}
} catch (Exception e) {
e.printStackTrace();
}
}
class Service implements Runnable {
private Socket socket;
private BufferedReader in = null;
private PrintWriter printWriter=null;
public Service(Socket socket) {
this.socket = socket;
try {
printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF-8")), true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream(),"UTF-8"));
printWriter.println("成功连接服务器"+"(服务器发送)");
System.out.println("成功连接服务器");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
if ((receiveMsg = in.readLine())!=null) {
System.out.println("receiveMsg:"+receiveMsg);
if (receiveMsg.equals("0")) {
System.out.println("客户端请求断开连接");
printWriter.println("服务端断开连接"+"(服务器发送)");
mList.remove(socket);
in.close();
socket.close();
break;
} else {
sendMsg = "我已接收:" + receiveMsg + "(服务器发送)";
printWriter.println(sendMsg);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
客户端代码实现
界面布局代码
<?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"
tools:context="com.example.socket.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="客户端"
android:textColor="@color/deepskyblue"
android:textSize="20sp" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:hint="服务器IP:"
android:id="@+id/ServerIP" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:hint="通信端口:"
android:id="@+id/ServerPort" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/connect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:text="自动连接"
android:textColor="@color/royalblue"/>
<Button
android:id="@+id/disconnect"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
android:text="连接服务器"
android:textColor="@color/royalblue"/>
</LinearLayout>
<TextView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:text="请在服务器开启的情况下操作"
/>
<!--临时测试专用区域-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height