android客户端消息发送和接受服务端响应的显示

服务端主代码:

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);

    }
}

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值