import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class MainClass {
public final static int PORT = 2345;
public static void main(String[] args) throws Exception {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
SocketAddress port = new InetSocketAddress(PORT);
serverChannel.socket().bind(port);
while (true) {
SocketChannel clientChannel = serverChannel.accept();
String response = "Hello " + clientChannel.socket().getInetAddress() + " on port "
+ clientChannel.socket().getPort() + "\r\n";
response += "This is " + serverChannel.socket() + " on port "
+ serverChannel.socket().getLocalPort() + "\r\n";
byte[] data = response.getBytes("UTF-8");
ByteBuffer buffer = ByteBuffer.wrap(data);
while (buffer.hasRemaining())
clientChannel.write(buffer);
clientChannel.close();
}
}
}
|