如下是PC端代碼:java
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\neusoft\.emulator_console_auth_token 下面用記事本打開復制token;
5.在cmd 輸入 :auth 44kZIm47P+BkAPhI;(不一樣電腦可能不同,看token裏面的數據)
6.在cmd輸入:redir add tcp:4444:4444;
(端口重映射 )
tcp:電腦端口號:模擬器端口號