私は2つのEC2インスタンスの間にサーバーソケット接続を確立しようとしています。あるマシンではServer.javaコードがあり、もう1つのマシンにはClient.javaコードがあります。 エラーは表示されませんが、クライアントコードはサーバーに接続できません。EC2マシン間のソケット接続
Server.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class MasterServer {
@SuppressWarnings("unused")
public static void main(String[] args) throws IOException {
// Selector: multiplexor of SelectableChannel objects
Selector selector = Selector.open();
// ServerSocketChannel: selectable channel for stream-oriented listening sockets
ServerSocketChannel channel = ServerSocketChannel.open();
InetSocketAddress addr = new InetSocketAddress(1111);
// Binds the channel's socket to a local address and configures the socket to listen for connections
channel.bind(addr);
// Adjusts this channel's blocking mode.
channel.configureBlocking(false);
int ops = channel.validOps();
SelectionKey selectKy = channel.register(selector, ops, null);
// Infinite loop..
// Keep server running
while(true){
log("i'm a server and i'm waiting for new connection and buffer select...");
// Selects a set of keys whose corresponding channels are ready for I/O operations
selector.select();
// token representing the registration of a SelectableChannel with a Selector
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while (iterator.hasNext()) {
SelectionKey myKey = iterator.next();
// Tests whether this key's channel is ready to accept a new socket connection
if (myKey.isAcceptable()) {
SocketChannel client = channel.accept();
// Adjusts this channel's blocking mode to false
client.configureBlocking(false);
// Operation-set bit for read operations
client.register(selector, SelectionKey.OP_READ);
log("Connection Accepted: " + client.getLocalAddress() + "\n");
// Tests whether this key's channel is ready for reading
} else if (myKey.isReadable()) {
SocketChannel client = (SocketChannel) myKey.channel();
ByteBuffer buffer = ByteBuffer.allocate(256);
client.read(buffer);
String result = new String(buffer.array()).trim();
log("Message received: " + result);
if (result.equals("Crunchify")) {
client.close();
log("\nIt's time to close connection as we got last company name 'Crunchify'");
log("\nServer will keep running. Try running client again to establish new connection");
}
}
iterator.remove();
}
}
}
private static void log(String str) {
System.out.println(str);
}
}
Client.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
public class Client {
public static void main(String[] args) throws IOException, InterruptedException {
InetSocketAddress addr = new InetSocketAddress("52.201.235.57", 1111);
SocketChannel client = SocketChannel.open(addr);
log("Connecting to Server on port 1111...");
ArrayList<String> companyDetails = new ArrayList<String>();
// create a ArrayList with companyName list
companyDetails.add("Facebook");
companyDetails.add("Twitter");
companyDetails.add("IBM");
companyDetails.add("Google");
companyDetails.add("Crunchify");
for (String companyName : companyDetails) {
byte[] message = new String(companyName).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(message);
client.write(buffer);
log("sending: " + companyName);
buffer.clear();
// wait for 2 seconds before sending next message
Thread.sleep(2000);
}
client.close();
}
private static void log(String str) {
System.out.println(str);
}
}
私は私のプライム目的であるEC2のマシン上でローカルにではなく、それらを接続することができています。私はそれらを実行すると、私は、サーバーの状態
を以下の取得:
^C[[email protected] ec2-user]# java Server
i'm a server and i'm waiting for new connection and buffer select...
クライアントの場合:私は思う接続がないよう
^C[[email protected] ec2-user]# java Client
は、私は何も出力を取得していません。
まず、クライアントマシンからサーバに到達可能かどうかを確認します。 'telnet 52.201.235.57 1111' – Chidchai
これは、試行52.201.235.57 ....を返します。 –