A small project started to work for a long time.
You need to use a windows computer as a server and exchange data with it on the android end to implement some services.
For simplicity, use java to write this server. I have never done it before. Try the water. Simple code, purely looking for ideas.
Server code:
package com.test;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class MyServer {public static void main(String[] args) {try {ServerSocket server = new ServerSocket(8888);while (true) {System.out.println("execute 1\n");Socket client = server.accept();System.out.println("execute 2\n");OutputStream out = client.getOutputStream();System.out.println("execute 3\n");String msg = "hello android";out.write(msg.getBytes());System.out.println("execute 4\n");client.close();}} catch (IOException e) {e.printStackTrace();}}}
Use Geely's number 8888 as the port and wait for the android end to connect and send a sentence to android.
Android code:
package com.teat;import java.io.IOException;import java.io.InputStream;import java.net.Socket;import java.net.UnknownHostException;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class TestSocketActivity extends Activity {private TextView myTextView;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);myTextView = (TextView) findViewById(R.id.textView1);try {myTextView.setText("0");Socket socket = new Socket("192.168.1.100", 8888);myTextView.setText("1");InputStream in = socket.getInputStream();byte[] buffer = new byte[in.available()];myTextView.setText("2");in.read(buffer);myTextView.setText("3");String msg = new String(buffer);myTextView.setText(msg);} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
For convenience, ip addresses are written to death. You can use the ipconfig command in the dos window to view the local ip address of your computer, then, you can change the ip address in the following line of code to the ip address of your computer.
Socket socket = new Socket("192.168.1.100", 8888);
If the communication is normal, the "hello android" from the server is displayed ";
Code download: http://download.csdn.net/detail/jason0539/7011951
Author: jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)