zoukankan      html  css  js  c++  java
  • Android-TCP编程

    以下是PC端代码

    package com.example.sxb.myapplication;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    /**
    * Created by Administrator on 2017/12/13.
    */
    public class pcChat {
    public static void main(String[] args)
    {

    //收
    new Thread(){
    @Override
    public void run() {
    try {
    while(true){
    //模拟器5554发,PC收
    ServerSocket ss = new ServerSocket(7777);//PC端6666端口接收消息
    Socket s = ss.accept ();
    InputStream in = s.getInputStream();
    byte[] buf = new byte[1024];
    int num = in.read(buf);
    String str = new String(buf,0,num);
    System.out.println(s.getInetAddress().toString()+":"+str);
    s.close();
    ss.close();}
    } catch (IOException e) {
    e.printStackTrace();
    }



    }
    }.start();
    //发
    new Thread(){
    @Override
    public void run() {
    try {
    while(true){
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = null;
    System.out.println("请输入你要发送的内容:");
    str = br.readLine();
    System.out.println("你发送的消息:"+str);
    //PC发,模拟器5554收
    Socket s = new Socket("localhost",4444);//向模拟器4444端口号发送消息
    OutputStream out = s.getOutputStream();
    out.write(str.getBytes());
    s.close();}
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }.start();
    }

    }

    以下是模拟器端代码:
    package com.example.sxb.myapplication;
    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.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class DochatActivity extends AppCompatActivity {
    TextView tv_getMessage;
    EditText et_setMessage;
    Button bt_send;
    String str;
    Socket s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dochat);
    tv_getMessage=(TextView) findViewById(R.id.getMessage);
    et_setMessage=(EditText) findViewById(R.id.et_setMessage);
    bt_send=(Button) findViewById(R.id.bt_send);
    new Thread(){
    @Override
    public void run() {
    try {
    //收
    while(true){
    //模拟器5554收,模拟器5556发
    ServerSocket ss = new ServerSocket(4444);//模拟器4444接收消息
    s = ss.accept ();
    InputStream in = s.getInputStream();
    byte[] buf = new byte[1024];
    int num = in.read(buf);
    str = new String(buf,0,num);
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    tv_getMessage.setText(s.getInetAddress().toString()+":"+str);
    }
    });
    s.close();
    ss.close();}
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }.start();
    }
    public void click(View v){
    switch (v.getId()){
    case R.id.bt_send:

    new Thread(){
    @Override
    public void run() {
    try {
    //发
    //模拟器5554发,PC收
    Socket s = new Socket("10.0.2.2",7777);//向PC端7777发送消息
    //模拟器5554发,模拟器5556收
    // Socket s=new Socket("10.0.2.2",6666);//模拟器6666 6666端口号收
    OutputStream out = s.getOutputStream();
    out.write(et_setMessage.getText().toString().getBytes());
    s.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }.start();
    }
    }
    }
    以下是布局文件:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:id="@+id/getMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="即将接收消息..."
    />
    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <EditText
    android:id="@+id/et_setMessage"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:hint="请输入消息"
    />
    <Button
    android:id="@+id/bt_send"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:text="发送"
    android:layout_toRightOf="@+id/et_setMessage"
    android:onClick="click"/>
    </RelativeLayout>
    </LinearLayout>


    写完代码以后的操作步骤:
    1.在cmd输入指令:telnet,检查telnet有没有打开;
    2.控制面板》程序》程序和功能》打开或关闭Windows功能》勾选Telnet服务器和telnet客户端;
    3.telnet localhost 5554(注意:打开模拟器5554之后操作这一步);
    4.去 C:Users eusoft.emulator_console_auth_token 下面用记事本打开复制token;
    5.在cmd 输入 :auth 44kZIm47P+BkAPhI;(不同电脑可能不一样,看token里面的数据)
    6.在cmd输入:redir add tcp:4444:4444;
    (端口重映射 )
    tcp:电脑端口号:模拟器端口号



  • 相关阅读:
    东拼西凑 vim配置-更新
    oh-my-zsh
    switch变种玩法
    每天一个linux命令(5):rm 命令
    每天一个linux命令(4):mkdir命令
    每天一个linux命令(3):pwd命令
    ES6学习之let
    Window.scrollTo()
    如何调整滚动条的样式
    移动端实现滚动的四种方案
  • 原文地址:https://www.cnblogs.com/helloy-Net/p/8615796.html
Copyright © 2011-2022 走看看