Java/ソケットサンプル
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
''[[FrontPage]]''
* Java/ソケットサンプル [#w8ef8ee2]
Javaでクライアントとして動作するサンプルを解説する。~
[[C++/ソケットサンプル1(Linuxサーバ)]]~
[[C++/ソケットサンプル2(Windowsサーバ・クライアント)]]
** 制約 [#zb8aca34]
サンプルとして、ホスト名"clove.la"でアクセスできること。~
サンプルとして、ポート"57891"でアクセスできること。~
コンパイルのコマンドラインは"javac cl_javaCl.java"
~
~
ファイル名: cl_javaCl.java
import java.io.*; ...
import java.net.*; ...
...
public class cl_javaCl ...
{ ...
private final int ARG_NUM = 2; ...
private final int MAX_SESSIONS = 2; ...
private final int RET_OK = 0; ...
private final int RET_NG = (-1); ...
private final int IND_TRUE = 10; ...
private final int IND_FALSE = 0; ...
// private final int SV_PORT = 4519; ...
private final int SV_PORT = 57891; ...
private final int ZERO = 0; ...
private final int SOCK_TIMER = 5; ...
private final int DEF_BUFF_LEN = 1200; ...
private final int RETRY_COUNT = 4; ...
private InputStream INSO = null; ...
private OutputStream OUSO = null; ...
private Socket sock = null; ...
private int vc_kill_threads = IND_FALSE; ...
...
public static void main( String[] args ) ...
{ ...
cl_javaCl obj_javaCl = new cl_javaCl(); ...
obj_javaCl.start(); ...
return; ...
} ...
...
public int start() ...
{ ...
int rtnCod = RET_OK; ...
InputStreamReader INSR = new InputStreamReader( ...
BufferedReader BR = new BufferedReader( INSR ); ...
String strBuf = ""; ...
byte[] byteBuf = new byte[DEF_BUFF_LEN]; ...
int retryInd = ZERO; ...
...
Thread thCl = new Thread() ...
{ ...
public void run() ...
{ ...
try ...
{ ...
connect(); ...
} ...
catch( Exception e ) ...
{ ...
System.out.print( "connect error: " + e ...
} ...
} ...
}; ...
...
thCl.start(); ...
...
try ...
{ ...
for( ; ; ) ...
{ ...
if( (null != sock) && sock.isConnected() ) ...
{ ...
strBuf = BR.readLine(); ...
if( strBuf.equals("q!") ) ...
{ ...
vc_kill_threads = IND_TRUE; ...
strBuf = "disconnect request from cl...
byteBuf = strBuf.getBytes( "UTF8" );...
OUSO.write( byteBuf ); ...
Thread.sleep( 25 ); ...
disconnect(); ...
break; ...
} ...
...
byteBuf = strBuf.getBytes( "UTF8" ); ...
OUSO.write( byteBuf ); ...
Thread.sleep( 25 ); ...
} ...
else ...
{ ...
if( RETRY_COUNT > retryInd++ ) ...
{ ...
System.out.print( "connecting.\n\n" ...
Thread.sleep( 500 ); ...
continue; ...
} ...
System.out.print( "connection refused.\n...
break; ...
} ...
} ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "send error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
...
private int connect() ...
{ ...
int rtnCod = RET_OK; ...
int recvBytes; ...
String strBuf = ""; ...
byte[] byteBuf = new byte[DEF_BUFF_LEN]; ...
...
System.out.print( "This is Client's Thread.\n\n"...
...
try ...
{ ...
// sock = new Socket( "localHost", SV_PORT ); ...
sock = new Socket( "clove.la", SV_PORT ); ...
INSO = sock.getInputStream(); ...
OUSO = sock.getOutputStream(); ...
...
for( ; ; ) ...
{ ...
if( (null != sock) && sock.isConnected() ) ...
{ ...
recvBytes = INSO.read( byteBuf ); ...
if( IND_TRUE == vc_kill_threads ) ...
{ ...
break; ...
} ...
...
if( ZERO >= recvBytes ) ...
{ ...
continue; ...
} ...
strBuf = new String( byteBuf, 0, recvByt...
System.out.printf( "recvBytes = %d.\n\n"...
System.out.printf( "client rec: %s\n\n",...
} ...
else ...
{ ...
break; ...
} ...
} ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "recv error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
...
private int disconnect() ...
{ ...
int rtnCod = RET_OK; ...
...
try ...
{ ...
// sock.close(); ...
sock.shutdownOutput(); ...
Thread.sleep( 1000 ); ...
sock.close(); ...
sock = null; ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "disconnect error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
} ...
動作確認
>javac cl_javaCl.java
>java cl_javaCl
connecting.
This is Client's Thread.
q
recvBytes = 1.
client rec: q
q!
>
*** 修正履歴 [#ib87905f]
~
*** 課題 [#i2f80d76]
shutdownOutput()について理解要。~
~
終了行:
''[[FrontPage]]''
* Java/ソケットサンプル [#w8ef8ee2]
Javaでクライアントとして動作するサンプルを解説する。~
[[C++/ソケットサンプル1(Linuxサーバ)]]~
[[C++/ソケットサンプル2(Windowsサーバ・クライアント)]]
** 制約 [#zb8aca34]
サンプルとして、ホスト名"clove.la"でアクセスできること。~
サンプルとして、ポート"57891"でアクセスできること。~
コンパイルのコマンドラインは"javac cl_javaCl.java"
~
~
ファイル名: cl_javaCl.java
import java.io.*; ...
import java.net.*; ...
...
public class cl_javaCl ...
{ ...
private final int ARG_NUM = 2; ...
private final int MAX_SESSIONS = 2; ...
private final int RET_OK = 0; ...
private final int RET_NG = (-1); ...
private final int IND_TRUE = 10; ...
private final int IND_FALSE = 0; ...
// private final int SV_PORT = 4519; ...
private final int SV_PORT = 57891; ...
private final int ZERO = 0; ...
private final int SOCK_TIMER = 5; ...
private final int DEF_BUFF_LEN = 1200; ...
private final int RETRY_COUNT = 4; ...
private InputStream INSO = null; ...
private OutputStream OUSO = null; ...
private Socket sock = null; ...
private int vc_kill_threads = IND_FALSE; ...
...
public static void main( String[] args ) ...
{ ...
cl_javaCl obj_javaCl = new cl_javaCl(); ...
obj_javaCl.start(); ...
return; ...
} ...
...
public int start() ...
{ ...
int rtnCod = RET_OK; ...
InputStreamReader INSR = new InputStreamReader( ...
BufferedReader BR = new BufferedReader( INSR ); ...
String strBuf = ""; ...
byte[] byteBuf = new byte[DEF_BUFF_LEN]; ...
int retryInd = ZERO; ...
...
Thread thCl = new Thread() ...
{ ...
public void run() ...
{ ...
try ...
{ ...
connect(); ...
} ...
catch( Exception e ) ...
{ ...
System.out.print( "connect error: " + e ...
} ...
} ...
}; ...
...
thCl.start(); ...
...
try ...
{ ...
for( ; ; ) ...
{ ...
if( (null != sock) && sock.isConnected() ) ...
{ ...
strBuf = BR.readLine(); ...
if( strBuf.equals("q!") ) ...
{ ...
vc_kill_threads = IND_TRUE; ...
strBuf = "disconnect request from cl...
byteBuf = strBuf.getBytes( "UTF8" );...
OUSO.write( byteBuf ); ...
Thread.sleep( 25 ); ...
disconnect(); ...
break; ...
} ...
...
byteBuf = strBuf.getBytes( "UTF8" ); ...
OUSO.write( byteBuf ); ...
Thread.sleep( 25 ); ...
} ...
else ...
{ ...
if( RETRY_COUNT > retryInd++ ) ...
{ ...
System.out.print( "connecting.\n\n" ...
Thread.sleep( 500 ); ...
continue; ...
} ...
System.out.print( "connection refused.\n...
break; ...
} ...
} ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "send error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
...
private int connect() ...
{ ...
int rtnCod = RET_OK; ...
int recvBytes; ...
String strBuf = ""; ...
byte[] byteBuf = new byte[DEF_BUFF_LEN]; ...
...
System.out.print( "This is Client's Thread.\n\n"...
...
try ...
{ ...
// sock = new Socket( "localHost", SV_PORT ); ...
sock = new Socket( "clove.la", SV_PORT ); ...
INSO = sock.getInputStream(); ...
OUSO = sock.getOutputStream(); ...
...
for( ; ; ) ...
{ ...
if( (null != sock) && sock.isConnected() ) ...
{ ...
recvBytes = INSO.read( byteBuf ); ...
if( IND_TRUE == vc_kill_threads ) ...
{ ...
break; ...
} ...
...
if( ZERO >= recvBytes ) ...
{ ...
continue; ...
} ...
strBuf = new String( byteBuf, 0, recvByt...
System.out.printf( "recvBytes = %d.\n\n"...
System.out.printf( "client rec: %s\n\n",...
} ...
else ...
{ ...
break; ...
} ...
} ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "recv error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
...
private int disconnect() ...
{ ...
int rtnCod = RET_OK; ...
...
try ...
{ ...
// sock.close(); ...
sock.shutdownOutput(); ...
Thread.sleep( 1000 ); ...
sock.close(); ...
sock = null; ...
} ...
catch( Exception e ) ...
{ ...
System.out.println( "disconnect error:" + e ); ...
} ...
...
return( rtnCod ); ...
} ...
} ...
動作確認
>javac cl_javaCl.java
>java cl_javaCl
connecting.
This is Client's Thread.
q
recvBytes = 1.
client rec: q
q!
>
*** 修正履歴 [#ib87905f]
~
*** 課題 [#i2f80d76]
shutdownOutput()について理解要。~
~
ページ名: