使用Eclipse搭建简易Android服务器

一、环境搭建

1、  所需软件列表

a、 Tomcat

b、Eclipse

c、JDK

2、环境配置

Tomcat配置、JDK安装,注意如果Eclipse中没有安装Tomcat插件(Windows-->Preferences中没有Server、Tomcat这项),可参考 https://blog.csdn.net/suyimin2010/article/details/80050202安装 Tomcat插件。


二、项目配置

1、服务端

1.1     Eclipse中 File-->new--->Dynamic Web Project 新建一个Web Project

1.2     在Java Resources 下新建一个包和Main类,写入如下代码: 

package com.chatfree.server;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
    public static final int PORT = 8888;//监听的端口号   

    public static void main(String[] args) {
        System.out.println("服务器启动...\n");
        Main server = new Main();
        server.init();
    }

    public void init() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                // 一旦有堵塞, 则表示服务器与客户端获得了连接  
                Socket client = serverSocket.accept();
                // 处理这次连接  
                new HandlerThread(client);
            }
        } catch (Exception e) {
            System.out.println("服务器异常: " + e.getMessage());
        }
    }

    private class HandlerThread implements Runnable {
        private Socket socket;
        public HandlerThread(Socket client) {
            socket = client;
            new Thread(this).start();
        }

        public void run() {
            try {
                // 读取客户端数据  
                DataInputStream input = new DataInputStream(socket.getInputStream());
                String clientInputStr = input.readUTF();//这里要注意和客户端输出流的写方法对应,否则会抛 EOFException
                // 处理客户端数据  
                System.out.println("客户端发过来的内容:" + clientInputStr);

                // 向客户端回复信息  
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                System.out.print("请输入:\t");
                // 发送键盘输入的一行  
                String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
                out.writeUTF(s);

                out.close();
                input.close();
            } catch (Exception e) {
                System.out.println("服务器 run 异常: " + e.getMessage());
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (Exception e) {
                        socket = null;
                        System.out.println("服务端 finally 异常:" + e.getMessage());
                    }
                }
            }
        }
    }
}  

1.3 在第一步环境配置中配置好Tomcat后,点击Eclipse中的Tomcat图标,运行Tomcat

1.4  在Main类上右键  Run as-->Java Application 服务即可运行

2、客户端      

新建一个Android项目,在MainActivity中写入以下代码,注意修改你的IP的地址:

package com.example.zgp.chatfree;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;


public class MainActivity extends ActionBarActivity {
    private TextView myTextView;
    private Button mBtnConnect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myTextView = (TextView) findViewById(R.id.tv_info);
        mBtnConnect=(Button)findViewById(R.id.btn_connect);
        mBtnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                            Socket socket = null;
                            try {
                                //创建一个流套接字并将其连接到指定主机上的指定端口号
                                socket = new Socket("192.168.1.91", 8888);

                                //读取服务器端数据
                                DataInputStream input = new DataInputStream(socket.getInputStream());
                                //向服务器端发送数据
                                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                                String str = "I am Client";
                                out.writeUTF(str);

                                final String ret = input.readUTF();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        myTextView.setText(ret);
                                        Toast.makeText(MainActivity.this,ret,Toast.LENGTH_SHORT).show();
                                    }
                                });
                                System.out.println("服务器端返回过来的是: " + ret);


                                out.close();
                                input.close();
                            } catch (Exception e) {
                                System.out.println("客户端异常:" + e.getMessage());
                            } finally {
                                if (socket != null) {
                                    try {
                                        socket.close();
                                    } catch (IOException e) {
                                        socket = null;
                                        System.out.println("客户端 finally 异常:" + e.getMessage());
                                    }
                                }
                            }
                        }

                }).start();


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

布局文件如下:



    
   

运行Android项目,点击上面的Button,Eclipse 服务端控制台即可看到输出,同是在控制台输入字符,即可看到app中文本框中的内容改变。

 

 

 

 

你可能感兴趣的:(服务器搭建)