Java serves as a server and android serves as a client for data transmission

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.