重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

下のように URLConnections クラスと sockets クラスでは使用してるプロトコルが違うのか
googleからの反応が異なります
URLConnections クラスは反応を示さず
sockets クラスは読み込めます
そこで、URLConnectionsクラスとsocketsクラスが使用している
TCP/IP階層図では URLConnectionsクラスとsocketsクラスはどの階層に該当するのでしょうか?

import java.io.*;
import java.net.*;
import java.util.Date;

class URLConnections
{
public URLConnections()
{
try{
int character;
URL url = new URL( "http://www.google.co.jp" );
URLConnection urlconnection = url.openConnection();

int contentlength = urlconnection.getContentLength();
if( contentlength > 0 )
{
InputStream in = urlconnection.getInputStream();

while( (character = in.read() ) != -1 )
{
System.out.print( (char)character );
}
}
}catch(Exception e)
{
System.out.println( "URLConnections で例外発生");
}

}
}

class sockets
{
public sockets()
{
try {
System.out.println( "socket start" );
Socket s = new Socket( "www.google.co.jp", 80 );

BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
PrintWriter out = new PrintWriter( s.getOutputStream() );

out.print( "GET /index.html\n\n" );
out.flush();

String line;
while( (line=in.readLine())!= null ){
System.out.println( line );
}
System.out.println( "socket end" );
}catch(Exception e)
{
}
}
}

public class app {
public static void main( String[] args)
{
URLConnections u = new URLConnections();
System.out.println( "end1\n" );

sockets s = new sockets();
System.out.println( "end2\n" );
}

}

A 回答 (2件)

TCP/IP階層モデルでは、


どちらもアプリケーション層です。

ただし、OSI参照モデルでは、
ソケット通信はセッション層
HTTP通信はアプリケーション層
になります。

プロトコルはもちろんちがいますよ。
ソケット通信では、自分でプロトコルを実装するのですから。
    • good
    • 0

> URLConnections クラスは反応を示さず


APIドキュメントを開き
URLConnectionクラスのgetContentLengthメソッドの
説明を読むと良いわ。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!