服务端主代码:
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8089, 2);
while (true) {
// 处理客户端的链接请求
Socket socket = ss.accept();// block阻塞
ClientHandler c = new ClientHandler(socket);
Thread t = new Thread(c, "aaa");//为一个新的连接开启一个新的进程
t.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
封装socket
public class ClientHandler implements Runnable {
private Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream is ;
OutputStream os;
try {
is = socket.getInputStream();
os = socket.getOutputStream();
// 读取客户端请求数据
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String hello = br.readLine();
InetAddress ia = socket.getInetAddress();
System.out.println(hello+"===="+ia.getHostAddress());
// 发回响应给客户端
PrintWriter pw = new PrintWriter(os, true);
pw.println("Server response=" + hello);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
android布局文件
<?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"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="100dp"
android:text="在这里输入数据"
/>
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="Send" />
<TextView
android:id="@+id/textView2"
android:gravity="center"
android:layout_width="250dp"
android:layout_height="100dp"
android:text="服务器返回的数据" />
</LinearLayout>
android 主要代码
public class MainActivity extends AppCompatActivity {
TextView textView1;
TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
}
public void send(View view) {
String sendText = textView1.getText().toString();
System.out.println("the send Text is " + sendText);
myFunc(sendText);
}
public void myFunc(final String myStr){
new AsyncTask<String,Void,String>(){
@Override
protected String doInBackground(String... strings) {
Socket socket = null;
try {
//开启一个客户端socket
socket = new Socket("192.168.1.132",8089);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);
pw.println(myStr);
//读取数据到textview
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String hello = br.readLine();
return hello;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String hello) {
textView2.setText(hello);
super.onPostExecute(hello);
}
}.execute(myStr);
}
}