筑波大学 システム情報系 情報工学域
新城 靖
<yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~syspro/2019/2019-06-12
/echo-server-java-thread.html
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~syspro/2019/
http://www.coins.tsukuba.ac.jp/~yas/
23: public static void echo_server( int portno ) throws IOException
24: {
25: ServerSocket acc = new ServerSocket( portno );
26: print_my_host_port( portno );
27: while( true )
28: {
29: stdout.printf("accepting incoming connections (hash== %s) ...\n",
30: acc.hashCode());
31: Socket com = acc.accept();
32: tcp_peeraddr_print( com );
33: Thread th = new Thread( new EchoServerWorker(com) );
34: th.start();
35: }
36: }
全体の差分は、以下のようになっている。
$ diff -c EchoServerSingle.java EchoServer.java
*** EchoServerSingle.java 2019-07-22 20:48:30.000000000 +0900 --- EchoServer.java 2019-07-27 14:57:38.000000000 +0900 ***************
*** 1,26 ****
/*
! EchoServerSingle.java -- 文字列を送受信するサーバ(TCP/IP, Java版, スレッドなし)
! ~yas/syspro/ipc/EchoServerSingle.java
! Created on 2004/05/09 20:00:24
*/
import java.net.*;
import java.io.*;
! class EchoServerSingle
{
public static void main(String argv[]) throws IOException
{
if( argv.length != 1 )
{
! stderr.printf("Usage: %% java EchoServerSingle port\n");
System.exit( -1 );
}
int portno = Integer.parseInt( argv[0] );
! echo_server_single( portno );
}
! public static void echo_server_single( int portno ) throws IOException
{
ServerSocket acc = new ServerSocket( portno );
print_my_host_port( portno );
--- 1,26 ----
/*
! EchoServer.java -- 文字列を送受信するサーバ(TCP/IP, Java版)
! ~yas/syspro/ipc/EchoServer.java
! Created on 2004/02/14 16:22:13
*/
import java.net.*;
import java.io.*;
! class EchoServer
{
public static void main(String argv[]) throws IOException
{
if( argv.length != 1 )
{
! stderr.printf("Usage: %% java EchoServer port\n");
System.exit( -1 );
}
int portno = Integer.parseInt( argv[0] );
! echo_server( portno );
}
! public static void echo_server( int portno ) throws IOException
{
ServerSocket acc = new ServerSocket( portno );
print_my_host_port( portno );
***************
*** 30,37 ****
acc.hashCode();
Socket com = acc.accept();
tcp_peeraddr_print( com );
! EchoServerWorker esw = new EchoServerWorker(com);
! esw.run();
}
}
public static void print_my_host_port( int portno )
--- 30,37 ----
acc.hashCode();
Socket com = acc.accept();
tcp_peeraddr_print( com );
! Thread th = new Thread( new EchoServerWorker(com) );
! th.start();
}
}
public static void print_my_host_port( int portno )
))
$ cp ~yas/syspro/ipc/EchoServer{,Worker}.java .
$ ls *java
EchoServer.java EchoServerWorker.java
$ javac EchoServer.java EchoServerWorker.java
$ ls *class
EchoServer.class EchoServerWorker.class
$ java EchoServer 1231
run telnet aloe30.local 1231
accepting incoming connections (hash== 1173230247) ...
connection (hash== 856419764) from 2001:2f8:3a:1711:0:0:230:39:53148
accepting incoming connections (hash== 1173230247) ...
received (com hash==856419764) 5 characters, [012
]
connection (hash== 621009875) from 2001:2f8:3a:1711:0:0:230:40:63522
accepting incoming connections (hash== 1173230247) ...
received (com hash==621009875) 5 characters, [abc
]
received (com hash==621009875) 5 characters, [def
]
received (com hash==856419764) 5 characters, [345
]
connection (com hash==856419764) closed.
connection (com hash==621009875) closed.
^C
$
クライアント側(その1)。
$ telnet aloe30 1231
Trying 2001:2f8:3a:1711::230:30...
Connected to aloe30.
Escape character is '^]'.
012
012
345
345
^]
telnet> ^D
Connection closed.
$
クライアント側(その2)。
$ telnet aloe30 1231
Trying 2001:2f8:3a:1711::230:30...
Connected to aloe30.
Escape character is '^]'.
abc
abc
def
def
^]
telnet> ^D
Connection closed.
$
Thread th = new Thread( new EchoServerWorker(com) );
th.setDaemon(true);
th.start();