ポート番号
| 日本語 | 港番号 |
| 英語 | port number |
| ふりがな | ぽーとばんごう |
| フリガナ | ポートバンゴウ |
ネットワーク接続時の「接続口番号」。
サーバーがネットワーク上で接続を待ち受ける時には、「ポート番号」と呼ばれる番号を指定して待ち受ける。接続する側はこの番号を指定して接続する。そうすることで、ひとつのホストが複数の接続を行うことができる。
主なポート番号はIANAによってプロトコル毎に決められている。HTTPは80、SMTPは25、POP3は110となっている。
ただし、ポート番号に強制力はなく、基本的には他のサーバーのポート番号と合わなければよい。
URLでは、ポート番号はホストの後に「:ポート番号」という形で指定する。
指定しない場合、プロトコルの標準ポート番号で接続する。
HTTPの場合、通常は80を使用する。
ただし、開発時には8080を使用することが多い。たとえば、Tomcatは、インストール直後は8080でアクセスするようになっている。
サーバーがネットワーク上で接続を待ち受ける時には、「ポート番号」と呼ばれる番号を指定して待ち受ける。接続する側はこの番号を指定して接続する。そうすることで、ひとつのホストが複数の接続を行うことができる。
主なポート番号はIANAによってプロトコル毎に決められている。HTTPは80、SMTPは25、POP3は110となっている。
ただし、ポート番号に強制力はなく、基本的には他のサーバーのポート番号と合わなければよい。
URLでは、ポート番号はホストの後に「:ポート番号」という形で指定する。
指定しない場合、プロトコルの標準ポート番号で接続する。
HTTPの場合、通常は80を使用する。
ただし、開発時には8080を使用することが多い。たとえば、Tomcatは、インストール直後は8080でアクセスするようになっている。
// Sample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。HTTPは通常80。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。HTTPは通常80。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}
// Sample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
public class Sample
{
public static void main( String[] args )
{
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
try
{
// プロトコル。
String protocol = "http";
// ホストを示すドメイン名。
String host = "www.yahoo.co.jp";
// ポート番号。HTTPは通常80。
int port = 80;
// ファイルパス。
String filePath = "/index.html";
// アクセスするためのURLクラスを作ります。
URL url = new URL( protocol, host, port, filePath );
System.out.println( url.toString() );
// http://www.yahoo.co.jp:80/index.html
// Content-Typeの文字コード(注:実際には、HEADで文字コードを取得してから指定します)。
String charSet = "EUC-JP";
// リクエストのメソッド。
String method = "GET";
// 指定されたURLを元にリクエストを発行します。
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod( method );
// リクエストの結果を取得します。
InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
bufferedReader = new BufferedReader( inputStreamReader );
// 全行出力します。
while( true )
{
String oneLine = bufferedReader.readLine();
if( oneLine == null )
{
break;
}
System.out.println( oneLine );
}
// <html>
// <head>
// <title>Yahoo! JAPAN</title>
// (以下略)
}
catch( IOException e )
{
e.printStackTrace();
}
finally
{
if( bufferedReader != null )
{
try
{
bufferedReader.close();
}
catch( IOException e )
{
e.printStackTrace();
}
}
if( httpURLConnection != null )
{
httpURLConnection.disconnect();
}
}
}
}




