|
||||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||
java.lang.Objectorg.maachang.shm.ServerShm
public class ServerShm
SharedMemory用サーバ処理.
通常のSharedMemoryでは、1つのメモリー空間として提供された形のものとして利用しますが、この場合、
複数のクライアントと、1つのサーバでの通信の場合は、接続数単位で、共有メモリを作るか、1つの
共有メモリーを自前で管理し、複数対1の通信を実現する必要があります。
また、複数対1のアクセスを行いたい場合、使い方のイメージとしては、java.net.ServerSocketのような
感じが、もっとも単純で、利用しやすい形であると思います。
このオブジェクトは、java.net.ServerSocketのようにacceptで接続を待ち、そこから取得された通信用オブジェクト
を使って、相互に、通信を行うことができます。
<例:サーバ側処理>
public static final void main( String[] args ) throws Exception {
ArrayList threadMan = new ArrayList() ;
ServerShm server = new ServerShm( "test","test",10,-1 ) ;
for( ;; ) {
ShmConnector c = server.accept() ;
if( c != null ) {
threadMan.add( new ConnThread( c ) ) ;
}
}
}
class ConnThread extends Thread {
private ShmConnector con = null ;
public ConnThread( ShmConnector con ) {
this.con = con ;
this.setDaemon( true ) ;
this.start() ;
}
public void run() {
for( ;; ) {
if( con == null || con.isConnect() == false ) {
break ;
}
try {
Thread.sleep( 30L ) ;
int len = in.available() ;
if( len > 0 ) {
ByteArrayOutputStream bo = new ByteArrayOutputStream() ;
for( ;; ) {
if( in.available() <= 0 ) {
break ;
}
int n = in.read() ;
if( n <= -1 ) {
break ;
}
bo.write( n ) ;
}
byte[] bin = bo.toByteArray() ;
bo.close() ;
System.out.println( "read("+con.getId()+"):len:" + bin.length + "=" + new String( bin,"UTF8" ) ) ;
out.write( "server:".getBytes( "UTF8" ) ) ;
out.write( bin ) ;
out.flush() ;
}
} catch( Exception e ) {
e.printStackTrace() ;
try {
con.close() ;
} catch( Exception ee ) {
}
con = null ;
}
}
}
}
<例:クライアント側処理>
public static final void main( String[] args ) throws Exception {
BufferedReader be = new BufferedReader( new InputStreamReader( System.in ) ) ;
ClientShm shm = new ClientShm( "test","test",10,-1 ) ;
ShmConnector conn = shm.connect() ;
for( ;; ) {
OutputStream output = conn.getOutputStream() ;
InputStream input = conn.getInputStream() ;
System.out.print( "send:" ) ;
String in = be.readLine() ;
if( in == null || in.length() <= 0 ) {
continue ;
}
output.write( in.getBytes( "UTF8" ) ) ;
output.flush() ;
for( ;; ) {
int len = input.available() ;
if( len > 0 ) {
ByteArrayOutputStream bo = new ByteArrayOutputStream() ;
for( ;; ) {
if( input.available() <= 0 ) {
break ;
}
int n = input.read() ;
if( n <= -1 ) {
break ;
}
bo.write( n ) ;
}
byte[] bin = bo.toByteArray() ;
System.out.println( "read("+conn.getId()+"):" + new String( bin,"UTF8" ) ) ;
bo.close() ;
break ;
}
Thread.sleep( 30L ) ;
}
}
}
ClientShm,
SharedMemory| コンストラクタの概要 | |
|---|---|
ServerShm(java.lang.String semName,
java.lang.String shareName,
int length,
int timeout)
コンストラクタ. |
|
| メソッドの概要 | |
|---|---|
ShmConnector |
accept()
新しい接続を取得. |
ShmConnector |
accept(int timeout)
新しい接続を取得. |
void |
close()
オブジェクトクローズ. |
java.lang.String |
getSemaphoreName()
セマフォ名を取得. |
java.lang.String |
getShareName()
共有メモリ名を取得. |
int |
getTimeout()
タイムアウト値を取得. |
boolean |
isUse()
オブジェクトが利用可能かチェック. |
int |
maxLength()
最大コネクション数を取得. |
int |
size()
現在の接続数を取得します. |
| クラス java.lang.Object から継承されたメソッド |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| コンストラクタの詳細 |
|---|
public ServerShm(java.lang.String semName,
java.lang.String shareName,
int length,
int timeout)
throws java.lang.Exception
semName - 対象のセマフォー名を設定します.shareName - 対象の共有名を設定します.length - 最大接続数を設定します.timeout - タイムアウト値を設定します.
java.lang.Exception - 例外.| メソッドの詳細 |
|---|
public void close()
public ShmConnector accept()
throws java.lang.Exception
java.lang.Exception - 例外.
public ShmConnector accept(int timeout)
throws java.lang.Exception
timeout - タイムアウト値を設定します.
java.lang.Exception - 例外.public int maxLength()
public int getTimeout()
public java.lang.String getSemaphoreName()
public java.lang.String getShareName()
public int size()
public boolean isUse()
|
||||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド | |||||||||