电脑上的java程序
package hello;
import java.util.Arrays;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server{
public static void main(String[] args)throws IOException{
ServerSocket ss=new ServerSocket(30001);
while(true){ //循环读取手机发来的信息
//System.out.println("1");
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader((s.getInputStream())));
String line=br.readLine();
System.out.println(line);
br.close();
s.close();
}
}
}
android 代码:
将Edittext 的字符串传向电脑
public class MainActivity extends AppCompatActivity { //private Button mybu; private EditText edit; public void send1(View view) throws IOException { new Thread(){ @Override public void run(){ try { //电脑wifi ip地址 String host = "192.168.23.1"; //要连接的服务端IP地址 int port = 30001; //要连接的服务端对应的监听端口 //与服务端建立连接 Socket client = new Socket(host, port); edit =(EditText) findViewById(R.id.editView); String ls=edit.getText().toString(); OutputStream os=client.getOutputStream(); os.write(ls.getBytes("utf-8")); os.close(); client.close(); } catch (IOException e){ e.printStackTrace(); } } }.start(); }@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }