android socket客户端app

连接来自java写的服务器,连接成功后在textview中输出“连接服务器成功!”

服务器端:

import java.io.*;
import java.net.*;

public class MyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(30000);
        while(true) {
            Socket s = ss.accept();
            PrintStream ps = new PrintStream(s.getOutputStream());
            ps.println("连接服务器成功!");
            ps.close();
            s.close();
        }
    }
}
客户端:
package com.example.yanzhuang.caculation;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.net.Socket;

public class Main6Activity extends AppCompatActivity implements View.OnClickListener {
    TextView tv1;
    TextView tv2;
    TextView tv3;
    EditText et1;
    EditText et2;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);


        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);

        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);

        button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(this);




    }


    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn:
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        String s1 = null;
                        String s2 = null;

                        s1 = et1.getText().toString();
                        s2 = et2.getText().toString();

                        try {
                            Socket socket = new Socket(s1, Integer.parseInt(s2));
                            BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                            socket.getInputStream()));
                            String line = br.readLine();
                            Log.d("Main6Activity", "line:" + line);
                            tv3.setText(line);
                            br.close();
                            socket.close();
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            default :
                break;
        }
    }
}
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_centerHorizontal="true"
        android:text="IP:"
        android:textSize="30sp"
        android:layout_marginLeft="20dp"
        />
    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv1"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et1"
        android:layout_marginTop="30dp"
        android:layout_centerHorizontal="true"
        android:text="port:"
        android:textSize="30sp"
        android:layout_marginLeft="20dp"/>
    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv2"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp" />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et2"
        android:layout_marginTop="30dp"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:layout_marginLeft="20dp"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/tv3"
        android:layout_centerHorizontal="true"
        android:text="确认"
        android:textSize="30sp"
        android:layout_marginLeft="20dp"/>
</RelativeLayout>
app界面:

最终目标是要写个tcp调试助手的。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值