Socket通信

サーバ側

受信・送信を行う共通処理

class EchoThread extends Thread {

private Socket socket;

public EchoThread(Socket socket) {

this.socket = socket;

System.out.println("Remoteの接続:" + socket.getRemoteSocketAddress());

}

@Override

public void run() {

try {

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

System.out.println("Clientから受信:" + line);

out.println(line);

System.out.println("Clientに送信:" + line);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if(socket != null) {

try {

System.out.println("Remoteの切断:" +

socket.getRemoteSocketAddress());

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

class EchoChannelThread extends Thread {

private static final int BUF_SIZE = 1000;

private SocketChannel channel;

public EchoChannelThread(SocketChannel channel) {

this.channel = channel;

System.out.println("Remoteの接続:" +

channel.socket().getRemoteSocketAddress());

}

@Override

public void run() {

ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);

Charset charset = Charset.forName("UTF-8");

try {

if(channel.read(buf) < 0) {

return;

}

buf.flip();

String input = charset.decode(buf).toString();

System.out.println("Clientから受信:" + input);

buf.flip();

channel.write(buf);

System.out.println("Clientに送信:" + input);

} catch (IOException e) {

e.printStackTrace();

} finally {

if(channel != null && channel.isOpen()) {

try {

System.out.println("Remoteの切断:" +

channel.socket().getRemoteSocketAddress());

channel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

Socket版

class Server {

public static void start() {

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(8080);

System.out.println("Serverのport:" + serverSocket.getLocalPort());

while(true) {

Socket socket = serverSocket.accept();

new EchoThread(socket).start();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if(serverSocket != null) {

try {

serverSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

SSLSocket版

セキュアなデータ交換の場合(完全性保護、認証、機密性)

class SSLServer {

public static void start() {

SSLServerSocket sslServerSocket = null;

try {

SSLServerSocketFactory sslServerSocketFactory =

(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();

sslServerSocket =

(SSLServerSocket) sslServerSocketFactory.createServerSocket(8080);

System.out.println("Serverのport:" +

sslServerSocket.getLocalPort());

while(true) {

SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();

new EchoThread(sslSocket).start();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if(sslServerSocket != null) {

try {

sslServerSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

SocketChannel版

class ChannelServer {

public static void start() {

ServerSocketChannel serverChannel = null;

try {

serverChannel = ServerSocketChannel.open();

serverChannel.socket().bind(new InetSocketAddress(8081));

System.out.println("Serverのport:" +

serverChannel.socket().getLocalPort());

while(true) {

SocketChannel channel = serverChannel.accept();

new EchoChannelThread(channel).start();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if(serverChannel != null && serverChannel.isOpen()) {

try {

serverChannel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

クライアント側

受信・送信を行う共通処理

class Echo {

public static void doEcho(Socket socket) {

try {

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

BufferedReader stdIn = new BufferedReader(

new InputStreamReader(System.in));

String line;

while ((line = stdIn.readLine()) != null) {

System.out.println("Serverに送信:" + line);

out.println(line);

System.out.println("Serverから受信:" + in.readLine());

}

} catch (IOException e) {

e.printStackTrace();

}

}

public static void doEcho(SocketChannel channel) {

ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);

Charset charset = Charset.forName("UTF-8");

try {

BufferedReader stdIn = new BufferedReader(

new InputStreamReader(System.in));

String line = stdIn.readLine();

channel.write(charset.encode(CharBuffer.wrap(line)));

System.out.println("Serverに送信:" + line);

while(channel.isConnected()) {

buf.clear();

if(channel.read(buf) < 0){

return;

}

buf.flip();

System.out.println("Serverから受信:" +

charset.decode(buf).toString());

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

Socket版

class Client {

public static void start() {

Socket socket = null;

try {

socket = new Socket("localhost", 8080);

Echo.doEcho(socket);

} catch (IOException e) {

e.printStackTrace();

} finally {

if(socket != null) {

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

SSLSocket版

class SSLClient {

public static void start() {

SSLSocket sslSocket = null;

try {

SSLSocketFactory sslSocketFactory =

(SSLSocketFactory) SSLSocketFactory.getDefault();

sslSocket =

(SSLSocket) sslSocketFactory.createSocket("localhost", 8080);

Echo.doEcho(sslSocket);

} catch (IOException e) {

e.printStackTrace();

} finally {

if(sslSocket != null) {

try {

sslSocket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

SocketChannel版

class ChannelClient {

public static void start() {

SocketChannel channel = null;

try {

channel = SocketChannel.open(

new InetSocketAddress("localhost", 8081));

Echo.doEcho(channel);

} catch (IOException e) {

e.printStackTrace();

} finally {

if(channel != null && channel.isOpen()) {

try {

channel.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

★Tip

Socket sc = new Socket("www.yahoo.co.jp", 80);

String ip = sc.getInetAddress(); // 接続先アドレス

String port sc.getPort(); // 接続先ポート

String local = sc.getLocalAddress(); // ローカルアドレス