Setting socket local port throws exception - java

Given this:
. . .
ServerSocket serverSocket = new ServerSocket(1111);
while (helperSockets.size() < Common.NUM_HELPERS) {
Socket helperSocket = serverSocket.accept();
. . .
This throws the exception:
new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort);
Exception:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
I'm trying to use this constructor to set the local port, so what am I doing wrong? Source below.
The constructor creates a thread that listens for clients via ServerSockets, while trying to connect to other clients. This client tries to connect with itself for testing. The problem is encountered upon first iteration of the for loop.
public class DisSemHelper extends Thread {
private int id;
private int semaphore;
private Clock clock;
private Vector<Socket> helperSockets;
private int localPort;
private int receivedSender;
private String receivedOperation;
private int receivedTimestamp;
/**
*/
public DisSemHelper(int id) {
this.id = id;
this.semaphore = 0;
this.clock = new Clock();
this.helperSockets = new Vector<Socket>();
this.receivedSender = -1;
this.receivedOperation = null;
this.receivedTimestamp = -1;
this.localPort = Common.portMap.get(id);
new ConnectionListener().start();
/* Create and store connections to all helpers */
for (int i=0; helperSockets.size() < Common.NUM_HELPERS; i++) {
Socket helperSocket = null;
// String portKey = "STREET_" + i;
/* Wait until target street socket is ready. Retry every second. */
Exception e = new ConnectException();
while (helperSocket == null) {
try {
Thread.sleep(1000);
helperSocket = new Socket("localhost", 2222, InetAddress.getLocalHost(), localPort);
} catch (ConnectException ce) {
e = ce;
e.printStackTrace();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
e.printStackTrace();
}
}
int remotePort = helperSocket.getPort();
int connectedHelperID = Common.portMap.indexOf(remotePort);
if (this.helperSockets.size() <= connectedHelperID) {
this.helperSockets.add(helperSocket);
System.out.println("Helper " + id + " added socket from outgoing: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort());
}
}
System.out.println(this.helperSockets);
}
private class ConnectionListener extends Thread {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(2222);
/* Listen for connections from other helpers */
while (helperSockets.size() < Common.NUM_HELPERS) {
Socket helperSocket = serverSocket.accept();
// TODO Will indexof int in list of Integers work?
int remotePort = helperSocket.getPort();
int connectedHelperID = Common.portMap.indexOf(remotePort);
// TODO Does this really work?
if (connectedHelperID == -1) {
helperSockets.add(helperSocket);
System.out.println("Helper " + id + " added socket from incoming: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

This is a WAG, but I'd try the other constructors in Socket.
Try new Socket(INetAddress.getLocalHost(), "111");
See:
http://ashishmyles.com/tutorials/tcpchat/index.html

Can you make a simple test-case and post the source? That'll help us troubleshoot this better.
new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort);
What exactly are you trying to do here? Why do you need to define what local host/port pair to use locally? A client should normally just specify the remote host/port and let the OS pick the local port. This is perhaps the problem.

Not a true solution, but I ended up working around setting the local port (because nobody seems to like that idea).

Related

Java - how can I check if the ServerSocket.accept()ed the connection?

I'm trying to write simple server-client chat solution. For test purposes, I'm creating an array of 2 serverThreads, which are responsible for sending and receiving messages from the clients connected.
I'd like a server to reject a connections after the number of connected clients reach a maximum value. However, even though the server do not accept the connection, the socket on client side is created. Methods socket.isBound and isConnected both return true value.
So back to the main question. Do you have any ideas how could I reject the client from connecting when the ServerSocket will not be able to .accept() additional connection?
Here's the code of the Server class.
public class Server {
private ServerSocket serverSocket = null;
private ServerThread serverThread[] = new ServerThread[2];
protected volatile int clientCount = 0;
public Server (int port){
try {
System.out.println("Binding to port " + port + " ...");
serverSocket = new ServerSocket (port);
System.out.println("Binded to port " + port + ".");
} catch (IOException e) {
System.out.println("Failed binding to the port: " + e.getMessage());
}
}
public void addThread (Socket socket){
System.out.println ("Client connected at socket: " + socket);
serverThread[clientCount] = new ServerThread (this, socket);
try {
serverThread[clientCount].open();
serverThread[clientCount].start();
} catch (IOException e) {e.getMessage();}
}
public void waitForClient () {
boolean isLogPrinted = false;
while (true){
try {
if (clientCount < serverThread.length){
System.out.println ("Waiting for connection...");
isLogPrinted = false;
addThread (serverSocket.accept());
clientCount++;
System.out.println("Client count: " + clientCount);
}
else {
if (!isLogPrinted){
System.out.println("MAXIMUM NUMBER OF CLIENTS REACHED! (" + clientCount + ").");
isLogPrinted = true;
}
}
} catch (IOException e) {
System.out.println("Error while waiting for new clients to connect: " + e.getMessage());
}
}
}
public synchronized void broadcastMessages (String msg){
for (int i = 0; i < clientCount; i++)
serverThread[i].sendMessage(msg);
}
public static void main (String args[]){
Server server = new Server (4200);
server.waitForClient();
}
}
I'd like a server to reject a connections after the number of connected clients reach a maximum value.
Close the server socket.
However, even though the server do not accept the connection, the socket on client side is created. Methods socket.isBound and isConnected both return true value.
Correct. That's because TCP maintains a 'backlog queue' of incoming connections which have been completed but not yet accepted by the server application.
So back to the main question. Do you have any ideas how could I reject the client from connecting when the ServerSocket will not be able to .accept() additional connection?
Close the server socket while the number of connections is at its maximum.
However due to the backlog this technique can never be perfect. There is no perfect solution. You could have the server immediately close excess connections, but the excess clients won't detect that until they try to send something. If you need perfection you will probably have to introduce an application protocol whereby the server sends something like 'ACCEPTED' or 'REJECTED' accordingly.
Instead of while true in you waitForClient method try this
private final int allowedClients = 10;
private int connectedClients = 0;
public void waitForClient () {
boolean isLogPrinted = false;
while (connectedClients <= allowedClients){
try {
if (clientCount < serverThread.length){
System.out.println ("Waiting for connection...");
isLogPrinted = false;
addThread (serverSocket.accept());
connectedClients++;
System.out.println("Client count: " + clientCount);
}
else {
if (!isLogPrinted){
System.out.println("MAXIMUM NUMBER OF CLIENTS REACHED! (" + clientCount + ").");
isLogPrinted = true;
}
}
} catch (IOException e) {
System.out.println("Error while waiting for new clients to connect: " + e.getMessage());
}
}
}
I know this is very late to answer, but I think it will help many.
You can check for the existing socket if any by below code.
SocketAddress socketAddress = new InetSocketAddress("localhost", 8091);
Socket socket = new Socket();
ServerSocket serverSocket = null;
try {
socket.connect(socketAddress);
} catch (IOException e) {
e.printStackTrace();
}
if(socket == null) {
try {
serverSocket = new ServerSocket(8091);
} catch (IOException e) {
e.printStackTrace();
}
}
if not found a active socket on the same port and IP then it will start a new server socket or you can change it start socket only else you can connect to the existing socket.

Java Proxy Socket not connecting to client

I will post my code below, a little background.
I am trying to connect to a gameserver on port 9339. my local port changes each time. The aim is to pass the packets through the proxy and display the info in the command line.
The client connects to the remote host using bluestacks which is running the game.
Code:
import java.io.*;
import java.net.*;
public class proxy {
public static void main(String[] args) throws IOException {
try {
String host = "gamea.clashofclans.com";
int remoteport = 9339;
ServerSocket ss = new ServerSocket(0);
int localport = ss.getLocalPort();
ss.setReuseAddress(true);
// Print a start-up message
System.out.println("Starting proxy for " + host + ":" + remoteport
+ " on port " + localport);
// And start running the server
runServer(host, remoteport, localport,ss); // never returns
System.out.println("Started proxy!");
} catch (Exception e) {
System.err.println(e);
}
}
/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
*/
public static void runServer(String host, int remoteport, int localport, ServerSocket ss)
throws IOException {
final byte[] request = new byte[2048];
byte[] reply = new byte[4096];
while (true) {
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
System.out.println("Client Accepted!");
final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try {
server = new Socket(host, remoteport);
System.out.println("Client connected to server.");
} catch (IOException e) {
PrintWriter out = new PrintWriter(streamToClient);
out.print("Proxy server cannot connect to " + host + ":"
+ remoteport + ":\n" + e + "\n");
out.flush();
client.close();
System.out.println("Client disconnected");
continue;
}
// Get server streams.
final InputStream streamFromServer = server.getInputStream();
final OutputStream streamToServer = server.getOutputStream();
// a thread to read the client's requests and pass them
// to the server. A separate thread for asynchronous.
Thread t = new Thread() {
public void run() {
int bytesRead;
try {
while ((bytesRead = streamFromClient.read(request)) != -1) {
streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
}
} catch (IOException e) {
}
// the client closed the connection to us, so close our
// connection to the server.
try {
streamToServer.close();
} catch (IOException e) {
}
}
};
// Start the client-to-server request thread running
t.start();
// Read the server's responses
// and pass them back to the client.
int bytesRead;
try {
while ((bytesRead = streamFromServer.read(reply)) != -1) {
streamToClient.write(reply, 0, bytesRead);
streamToClient.flush();
}
} catch (IOException e) {
}
// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
}
}
}
}
}
Basically the last thing that is printed out is "Starting proxy for gamea.clashofclans.com:9339 on port (whatever it chose).
Hopefully someone can help me.
I have this problem too, I don`t have enough time to correct this but i think using thread is that is why all mistake.
check your proxy for working on browser setting( May be proxy had problem)
If not,
I suggest to don`t use thread. maybe mutual exclusion occurs.
Your code is correct.It is working fine so you don't need any fix. What is happening is , your serverSocket in your proxy class is waiting for client to connect. that's why it is not going forward. What you need to do is, create a client and connect to it.
follow the step :
run your proxy.
then run your client
for the client, you can use this code,
public static void main(String[] args) throws IOException {
try {
int remoteport = 9339;
String host="127.0.0.1";
makeConnection(host, remoteport);
System.out.println("connection successful!");
} catch (Exception e) {
System.err.println(e);
}
}
public static void makeConnection(String host, int remoteport) throws IOException {
while (true) {
Socket client = null;
try {
client = new Socket(host, remoteport);
System.out.println("Client connected to server.");
break;
} catch (IOException e) {
System.err.println(e);
} finally {
if (client != null)
client.close();
if (client != null)
client.close();
}
}
}

Is it possible to use an applet as a server with Java?

I'm just getting into networking and I'm quite confused about how I can use a server from a server hosting company for a game. At the moment, the program allows a user to host the server from their computer and it works to some degree. The only problem is that when my friends connect to the game from other states their ping is super high and they lag horribly.
I'm now using a site that gives out free web hosting space. I've believe I've jumped through most of the permission hoops that I need in order to use sockets and the applet works just like it does while running on my computer.
Below is an excerpt from the Game class. If I use InetAddress.getLocalHost().getHostAddress() to get the IP, it gets the IP from the user who is using the client and makes them the server host. This works, but it doesn't solve my problem of lag.
Game.java
public synchronized void start() {
running = true;
thread = new Thread(this, NAME + "_main");
thread.start();
if (JOptionPane.showConfirmDialog(this, "Do you want to run the server") == 0) {
socketServer = new GameServer(this);
socketServer.start();
}
this.ipAddress = "31.170.163.198";
//this.ipAddress = InetAddress.getByName(ipAddress).toString();
System.out.println("IP: " + this.ipAddress);
socketClient = new GameClient(this, this.ipAddress);//Inet4Address.getLocalHost().getHostAddress());
try {
Player.setIp(Inet4Address.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
socketClient.start();
}
I want the server that contains the jar to host the server. We all ping test each other and we mostly all get timed out, however, when we ping the server's ip we all get decent ping. When I enter the Server's IP (I find this info in the control panel) manually the server/client fail to make a connection. I've tried using both the website IP and the server IP that are provided. I've also tried using the applet.getCodeBase().getHost() in order to retrieve the IP from the server but that IP failed to send/receive data as well.
Is what I'm trying to do possible? Is the server blocking me from using it in that way? Do I need to rewrite the whole program and look into java servlets to achieve my goal?
I apologize if the question is silly. I've been working on this for the past 3-4 days with very little progress. I've searched around a bit on using a server in this manner and I've found very little on the topic.
Below are some excerpts from other relevant pieces of code.
GameClient.java
package fraccas.java2dgame.net;
public class GameClient extends Thread {
private InetAddress ipAddress;
private DatagramSocket socket;
private Game game;
public long lastPing = 0;
public static int ping = 0;
public GameClient(Game game, String ipAddress) {
this.game = game;
try {
this.socket = new DatagramSocket();
this.ipAddress = InetAddress.getByName(ipAddress);
System.out.println("IP: " + InetAddress.getByName(ipAddress));
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
if (packet != null)
{
long current = System.currentTimeMillis();
ping = (int) (current - lastPing);
lastPing = current;
}
} catch (IOException e) {
e.printStackTrace();
}
this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort());
}
}
private void parsePacket(byte[] data, InetAddress address, int port) {
String message = new String(data).trim();
PacketTypes type = Packet.lookupPacket(message.substring(0, 2));
Packet packet = null;
switch (type) {
default:
case INVALID:
break;
case LOGIN:
packet = new Packet00Login(data);
handleLogin((Packet00Login) packet, address, port);
break;
case DISCONNECT:
packet = new Packet01Disconnect(data);
System.out.println("[" + address.getHostAddress() + ":" + port + "] "
+ ((Packet01Disconnect) packet).getUsername() + " has left the world...");
game.level.removePlayerMP(((Packet01Disconnect) packet).getUsername());
break;
case MOVE:
packet = new Packet02Move(data);
handleMove((Packet02Move) packet);
}
}
public void sendData(byte[] data) {
//if (!game.isApplet) {
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 3333);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
//}
}
private void handleLogin(Packet00Login packet, InetAddress address, int port) {
System.out.println("[" + address.getHostAddress() + ":" + port + "] " + packet.getUsername()
+ " has joined the game...");
PlayerMP player = new PlayerMP(game.level, packet.getX(), packet.getY(), packet.getUsername(), address, port);
game.level.addEntity(player);
}
private void handleMove(Packet02Move packet) {
this.game.level.movePlayer(packet.getUsername(), packet.getX(), packet.getY(), packet.getNumSteps(),
packet.isMoving(), packet.getMovingDir());
}
}
GameServer.java
package fraccas.java2dgame.net;
public class GameServer extends Thread {
private DatagramSocket socket;
private Game game;
private List<PlayerMP> connectedPlayers = new ArrayList<PlayerMP>();
public GameServer(Game game) {
this.game = game;
try {
this.socket = new DatagramSocket(3333);
} catch (SocketException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort());
}
}
private void parsePacket(byte[] data, InetAddress address, int port) {
String message = new String(data).trim();
PacketTypes type = Packet.lookupPacket(message.substring(0, 2));
Packet packet = null;
switch (type) {
default:
case INVALID:
break;
case LOGIN:
packet = new Packet00Login(data);
System.out.println("[" + address.getHostAddress() + ":" + port + "] "
+ ((Packet00Login) packet).getUsername() + " has connected...");
PlayerMP player = new PlayerMP(game.level, 100, 100, ((Packet00Login) packet).getUsername(), address, port);
this.addConnection(player, (Packet00Login) packet);
break;
case DISCONNECT:
packet = new Packet01Disconnect(data);
System.out.println("[" + address.getHostAddress() + ":" + port + "] "
+ ((Packet01Disconnect) packet).getUsername() + " has left...");
this.removeConnection((Packet01Disconnect) packet);
break;
case MOVE:
packet = new Packet02Move(data);
this.handleMove(((Packet02Move) packet));
}
}
public void addConnection(PlayerMP player, Packet00Login packet) {
boolean alreadyConnected = false;
for (PlayerMP p : this.connectedPlayers) {
if (player.getUsername().equalsIgnoreCase(p.getUsername())) {
if (p.ipAddress == null) {
p.ipAddress = player.ipAddress;
}
if (p.port == -1) {
p.port = player.port;
}
alreadyConnected = true;
} else {
// relay to the current connected player that there is a new
// player
sendData(packet.getData(), p.ipAddress, p.port);
// relay to the new player that the currently connect player
// exists
//packet = new Packet00Login(p.getUsername(), p.x, p.y);
Packet00Login packetNew = new Packet00Login(p.getUsername(), p.x, p.y);
sendData(packetNew.getData(), player.ipAddress, player.port);
}
}
if (!alreadyConnected) {
this.connectedPlayers.add(player);
}
}
public void removeConnection(Packet01Disconnect packet) {
this.connectedPlayers.remove(getPlayerMPIndex(packet.getUsername()));
packet.writeData(this);
}
public PlayerMP getPlayerMP(String username) {
for (PlayerMP player : this.connectedPlayers) {
if (player.getUsername().equals(username)) {
return player;
}
}
return null;
}
public int getPlayerMPIndex(String username) {
int index = 0;
for (PlayerMP player : this.connectedPlayers) {
if (player.getUsername().equals(username)) {
break;
}
index++;
}
return index;
}
public void sendData(byte[] data, InetAddress ipAddress, int port) {
//if (!game.isApplet) {
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port);
try {
this.socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
//}
}
public void sendDataToAllClients(byte[] data) {
for (PlayerMP p : connectedPlayers) {
sendData(data, p.ipAddress, p.port);
}
}
private void handleMove(Packet02Move packet) {
if (getPlayerMP(packet.getUsername()) != null) {
int index = getPlayerMPIndex(packet.getUsername());
PlayerMP player = this.connectedPlayers.get(index);
player.x = packet.getX();
player.y = packet.getY();
player.setMoving(packet.isMoving());
player.setMovingDir(packet.getMovingDir());
player.setNumSteps(packet.getNumSteps());
packet.writeData(this);
}
}
The applet does not have any errors in the console at the moment. It simply does not send/receive the data.
http://critterisland.bugs3.com/public_html/index.html (Applet Link)
http://gyazo.com/a91401545b1a741f2b9fc86471bd5762 (Image of Applet)
Thanks for you time,
Fraccas
The problem is that you're using a web host which is only able to provide the files to users as opposed to actually being able to run the software you've created.
To run your software independently in the way you're wanting, your easiest option is to rent a virtual private server (VPS) which is located appropriately for your users. However, you'll need to be able to do some basic configuration on the VPS (such as install Java), and then run your server as a console application.
Not much code needs to be changed to get it working. You could use the same jar file for both the server and the client by adding a command line argument which only starts the server (i.e. "-server"). From the server computer/VPS you would then start the server by running "java -jar CritterIslandTest.jar -server" which then only starts the server and handles communication. If the command line argument is not found you can then continue with the GUI logic.

How to make Asynchronous tcp server continue to listen on channel and not close it

I am trying to make a server that is able to handle 1000+ connections from clients. This is part academic, part hobby project, so I kinda want to make my own solution, but I am facing a problem: When I start to listen for connections, and someone connects, the TCP connection is getting closed by java after 5 seconds. I know that this is from my 5 second sleep, but if I remove it, it returns immediately.
Here is my server code (stripped down):
final int SERVER_PORT = 9000;
final String SERVER_IP = "10.0.0.201";
AsynchronousChannelGroup group = null;
try {
group = AsynchronousChannelGroup.withThreadPool(threadPool);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create asynchronous server-socket channel bound to the default group.
try(AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel
.open(group)) {
if ( asynchronousServerSocketChannel.isOpen() ) {
// Bind to local address
asynchronousServerSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT),
SERVER_SOCKET_CHANNEL_BACKLOG);
// Display a waiting message
System.out.println("Waiting for connections on ip:port " + SERVER_IP + ":" + SERVER_PORT);
while (true) { // Not good?
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel
.accept();
try(AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get()) {
final SocketAddress remoteAddress = asynchronousSocketChannel.getRemoteAddress();
System.out.println("Incoming connection from: " + remoteAddress);
final ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);
// Time to receive data.
asynchronousSocketChannel.read(incomingBuffer, incomingBuffer,
new CompletionHandler<Integer, ByteBuffer>() {
public void completed( Integer result, ByteBuffer buffer ) {
}
public void failed( Throwable exc, ByteBuffer buffer ) {
if ( exc instanceof AsynchronousCloseException ) {
// Someone closed the connection
// while we where listening on it.
System.out.println("We listened on the socket, but someone closed it.");
}
}
});
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e.toString());
}
} catch (IOException | InterruptedException | ExecutionException ex) {
System.err.println(ex);
}
}
} else {
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
} catch (IOException ex) {
System.err.println(ex);
}
}
When running this code, and connecting with netcat "nc 10.0.0.201 9000" the connection is reset from the java/server side after 5 seconds (immediately if removing the sleep).
How can I stop it from returning, and make it continue to listen?
Am I taking the right approch on solving my initial goal?
A working example that does what I want:
final int SERVER_PORT = 9000;
final String SERVER_IP = "10.0.0.201";
AsynchronousChannelGroup group = null;
try {
group = AsynchronousChannelGroup.withThreadPool(threadPool);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create asynchronous server-socket channel bound to the default group.
AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open(group);
if ( asynchronousServerSocketChannel.isOpen() ) {
// Bind to local address
asynchronousServerSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT),
SERVER_SOCKET_CHANNEL_BACKLOG);
// Display a waiting message
System.out.println("Waiting for connections on ip:port " + SERVER_IP + ":" + SERVER_PORT);
while (true) { // Not good?
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel
.accept();
final AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get();
final SocketAddress remoteAddress = asynchronousSocketChannel.getRemoteAddress();
System.out.println("Incoming connection from: " + remoteAddress);
final ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);
// Time to receive data.
asynchronousSocketChannel.read(incomingBuffer, incomingBuffer,
new CompletionHandler<Integer, ByteBuffer>() {
public void completed( Integer result, ByteBuffer buffer ) {
// Why flip it?
buffer.flip();
String msgReceived = Charset.defaultCharset().decode(buffer).toString();
System.out.print("Got stuff from the network: " + msgReceived);
// Empty the buffer, and listen for new
// messages.
incomingBuffer.clear();
asynchronousSocketChannel.read(incomingBuffer, incomingBuffer, this);
}
public void failed( Throwable exc, ByteBuffer buffer ) {
if ( exc instanceof AsynchronousCloseException ) {
// Someone closed the connection
// while we where listening on it.
System.out.println("We listened on the socket, but someone closed it.");
}
}
});
}
}
}

Server-Client communication problem

i'm working with an example of client-server programm on Java. I faced such a problem:
I start the server with 8080 port and a localhost, than I start a client and make a request. As soon as the request done both programms close theri sockets, so i can't repeat my actions. How can i use the same client and the same server to make more than one request?
public class Network extends Thread
{
MasterEdit ME = new MasterEdit();
private Socket _socket;
InputStream is; //Data streams
OutputStream os;
/**
* Network class constructor
*/
public Network(int port, int backlog, InetAddress address)
{
//We create an object of SocketFactory
SocketFactory sf = new SocketFactory();
//Save server socket
ServerSocket ss = null;
try
{
if(address == null) //If there is no host
{
if(backlog <= 0) //If backlog is not given we create it with port
{ ss = sf.createServerSocket(port);
System.out.println("Success");
}
else
ss = sf.createServerSocket(port, backlog); //If backlog is given we just create it
}
else
ss = sf.createServerSocket(port, backlog, address); //If everything is given we create it using data
}
catch(Exception e)
{
//Exception with creation of socket
System.err.println("Failed open server socket");
System.exit(1); //Stop program and send 1 as a exception-code
}
while(true) //Listening to the socket
{
try
{
StartThread(ss.accept()); //If client has connected we send him to the daemon
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* Start daemon-tool when client has connected
*/
private void StartThread(Socket ss)
{
_socket = ss; //initializing of global variable
setDaemon(true); //anounce that new potok is daemon
setPriority(NORM_PRIORITY); //set the priority
start(); //Start it
}
#Override
public void run()
{
byte buffer[] = new byte[64*1024]; //buffer in 64 kb
try
{
is = _socket.getInputStream();
os = _socket.getOutputStream(); //Initializing the output stream to a client
String toClient = SearchRequest(new String(buffer, 0, is.read(buffer)));
os.write(toClient.getBytes()); //Sending an answer
}
catch(Exception e)
{
e.printStackTrace();
}
}
private String SearchRequest(String request)
{
String info = ""; //Initializing of a variable
if(request.equalsIgnoreCase("info")) //Check the request
{
//Adding data
info += "Virtual Machine Information (JVM)n";
info += "JVM Name: " + System.getProperty("java.vm.name")+"n";
info += "JVM installation directory: " + System.getProperty("java.home")+"n";
info += "JVM version: " + System.getProperty("java.vm.version")+"n";
info += "JVM Vendor: " + System.getProperty("java.vm.vendor")+"n";
info += "JVM Info: " + System.getProperty("java.vm.info")+"n";
return info; //Give the answer
}
if(request.charAt(0)=='0') {
StringTokenizer rm = new StringTokenizer(request, " \t\n\r,:");
rm.nextToken();
ME.MasterDell(Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken()));
return "Successfully deleted";
}
if(request.charAt(0)=='1'){
StringTokenizer temp = new StringTokenizer(request, " \t\n\r,:");
temp.nextToken();
ME.MasterAdd(Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), temp.nextToken());
return "Successfully added";
}
this.ClostIt();
return "Bad request"; //bad request
}
public void ClostIt() {
try {
is.close();
os.close();
_socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
It's server part. It usess SocketFactory class but mainly it just creates a socket in the begining. In main programm i call new Network(PORT, BACKLOG, InetAddress.getByName(host));
I am guessing in your server program you don't have a loop but rather something like this:
public static void main( String args[] ) {
ServerSocket server = new ServerSocket(...);
Socket con = server.accept();
//process the client connection ...
//done, exit!
}
rather than
public static void main( String args[] ) {
ServerSocket server = new ServerSocket(...);
Socket con = null;
while( condition /* e.g. shutdown server message received */ ) {
con = server.accept();
//process the client connection ...
//then keep waiting for the next request
}
//done, exit!
}
Bear in mind the above sample only processes one client at a time! you will need to step into multi-threading for processing simultaneous clients.
This is a good starter for a multi threaded server
http://www.kieser.net/linux/java_server.html
Mark

Categories