.listen NodeJS中的方法参数

时间:2015-07-25 06:05:21

标签: node.js

NodeJS noob签入。我正在查看.listen方法的文档,该方法具有以下形式:

package com.example.bermud06.p036_1sqlitequery;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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.EditText;
import android.widget.RadioGroup;


    public class MainActivity extends Activity implements View.OnClickListener {

        final String LOG_TAG="myLogs";
       String name[]={"Китай","США","Бразилия","Россия",
        "Япония","Германия","Египет","Италия","Франция","Канада"};

      //  String name[]={"China","USA","Brasil","Russia",
      //          "Japon","Germany","Egypt","Italy","France","Canade"};
        int people[]={1400,311,195,142,128,82,80,60,66,35};
        String region[]={"Азия","Америка","Америка","Европа","Азия","Европа","Африка","Европа","Европа","Америка"}  ;
      //  String region[]={"Asia","America","America","Europe","Asia","Europe","Africa","Europe","Europe","America"};

        Button btnAll,btnFunc,btnPeople,btnSort,btnGroup,btnHaving;
        EditText etFunc,etPeople, etRegionPeople;
        RadioGroup rgSort;


        DBHelper dbHelper;
        SQLiteDatabase db;

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

            btnAll =(Button) findViewById(R.id.btnAll);
            btnFunc=(Button) findViewById(R.id.btnFunc);
            btnGroup=(Button) findViewById(R.id.btnGroup);
            btnHaving=(Button) findViewById(R.id.btnHaving);
            btnPeople=(Button) findViewById(R.id.btnPeople);
            btnSort=(Button) findViewById(R.id.btnSort);

            btnAll.setOnClickListener(this);
            btnFunc.setOnClickListener(this);
            btnGroup.setOnClickListener(this);
            btnHaving.setOnClickListener(this);
            btnPeople.setOnClickListener(this);
            btnSort.setOnClickListener(this);

            etFunc=(EditText)findViewById(R.id.etFunc);
            etRegionPeople=(EditText)findViewById(R.id.etRegionPeople);
            etPeople=(EditText)findViewById(R.id.etPeople);

            rgSort=(RadioGroup)findViewById(R.id.rgSort);

            dbHelper=new DBHelper(this);

            db=dbHelper.getWritableDatabase();

            Cursor c=db.query("mytable",null,null,null,null,null,null);

            if (c.getCount()==0)
            {
                ContentValues cv=new ContentValues();

                for (int i=0;i<name.length;i++){
                    cv.put("name",name[i]);
                    cv.put("people",people[i]);
                    cv.put("region",region[i]);
                    Log.d(LOG_TAG,"id = "+db.insert("mytable",null,cv));
                }

            }
            c.close();
            dbHelper.close();

            onClick(btnAll);


        }

        @Override
        public void onClick(View v) {

            db=dbHelper.getWritableDatabase();

            String sFunc=etFunc.getText().toString();
            String sPeople=etPeople.getText().toString();
            String sRegionPeople=etRegionPeople.getText().toString();

            String[] columns=null;
            String selection=null;
            String[] selectionargs=null;
            String groupBy=null;
            String having=null;
            String orderBy=null;

            Cursor c=null;

            switch (v.getId())
            {
                case R.id.btnAll:
                    Log.d(LOG_TAG,"--- Проверка ---");
                    c=db.query("mytable",null,null,null,null,null,null);
                    break;

                case R.id.btnFunc:
                    Log.d(LOG_TAG,"--- ??????? "+sFunc+" ---");
                    columns=new String[]{sFunc};
                    c=db.query("mytable",columns,null,null,null,null,null);
                    break;

                case R.id.btnPeople:

                    Log.d(LOG_TAG,"--- ????????? ?????? "+sPeople+" ---");
                    selection="people > ?";
                    selectionargs=new String[]{sPeople};

                    c=db.query("mytable",null,selection,selectionargs,null,null,null);
                    break;
                case R.id.btnGroup:

                    Log.d(LOG_TAG,"--- ????????? ?? ??????? ---");
                    columns=new String[]{"region","sum(people) as people"};
                    groupBy="region";
                    c=db.query("mytable",columns,null,null,groupBy,null,null);
                    break;
                case R.id.btnHaving:
                    Log.d(LOG_TAG,"--- ?????? ? ?????????? ?????? "+sRegionPeople+" ---");
                    columns=new String[]{"region","sum(people) as people"};
                    groupBy="region";
                    having="sum(people) > "+sRegionPeople;
                    c=db.query("mytable",columns,null,null,groupBy,having,null);
                    break;

                case R.id.btnSort:
                    switch (rgSort.getCheckedRadioButtonId())
                    {
                        case R.id.rName:
                            Log.d(LOG_TAG,"--- ?????????? ?? ???????????? ---");
                            orderBy="name";
                            break;

                        case R.id.rPeople:
                            Log.d(LOG_TAG,"--- ?????????? ?? ????????? ---");
                            orderBy="people";
                            break;
                        case R.id.rRegion:
                            Log.d(LOG_TAG,"--- ?????????? ?? ??????? ---");
                            orderBy="region";
                            break;
                    }

                    c=db.query("mytable",null,null,null,null,null,orderBy);
                    break;

            }
            if (c!=null)
            {
                if (c.moveToFirst())
                {
                    String str;

                    do {
                        str="";
                        for (String cn : c.getColumnNames()){
                            str=str.concat(cn+" = "+
                            c.getString(c.getColumnIndex(cn))+"; ");
                        }
                        Log.d(LOG_TAG,str);

                    }while (c.moveToNext());

                }
                c.close();
            } else {
                Log.d(LOG_TAG,"Cursor is null");

                dbHelper.close();
            }
        }

        @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);
        }

        class DBHelper extends SQLiteOpenHelper{

            public DBHelper(Context cntx){
                super(cntx,"myDB",null,1);
            }
            @Override
            public void onCreate(SQLiteDatabase db) {
                Log.d(LOG_TAG,"--- onCreate database ---");

                try{
                db.execSQL("create table mytable (" +
                        "id integer primary key autoincrement,"+"name text," +
                        "people integer,"+"region text"+");");
                    }
                catch (Exception e)
                {
                    Log.d(LOG_TAG,e.toString());
                }
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            }
        }
    }

port / hostname的解释说明:

  

开始接受指定端口和主机名上的连接。如果   hostname被省略,服务器将接受指向的连接   任何IPv4地址

我不完全理解&#39;如果省略主机名,服务器将接受指向任何IPv4地址的连接。部分。服务器如何接受与任何IP地址的连接?这是服务器接受的内容而不是它能接受的内容,这让我觉得这很奇怪吗?

1 个答案:

答案 0 :(得分:0)

问题是运行服务器的当前主机有多个IP地址(多个网络或多个IP地址用于一个网卡)。见multihoming

因此,该语句意味着如果不提供主机名,则无论连接的目标是哪个IP地址,都将接受到达此特定主机的任何传入连接。