Server is not connecting - java

I have a void method called startServerConnection() that connects the server to a port, in this case 7777. This method is called inside an action listener for a button in another class, ClientGUI.
I'm pretty sure the code is correct, but for some reason the only output I get is "Waiting for connection..."
public void startServerConnection(){
try{
serverSocket = new ServerSocket(portNumber);
while(true){
System.out.println("Waiting for a connection...");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection established on port: "+clientSocket.getLocalPort());
ClientConnection clientConnection = new ClientConnection(clientSocket);
Thread thread = new Thread(clientConnection);
thread.start();
}
}
catch(Exception e){
e.printStackTrace();
return;
}
}
EDIT
Client class, connectClient method:
public void connectClient(String user){
try{
host = clientSocket.getInetAddress();
clientSocket = new Socket(host,port);
new ClientHandler(clientSocket).run();
String accepted = "Connection for host "+host+" accepted on port: "+clientSocket.getPort();
}
catch(Exception e){
//sendMessage("Connection error: "+e);
//serverGUI.appendEventsLog("Client "+new ClientGUI(username, port)+" failed to connect");
}
}
Any ideas on what's wrong?

Update:
public void connectClient(String user){
try{
clientSocket = new Socket(host,port);
// Use PrintWriter to send data out to server
// Use BufferedReader to receive data from server
}
catch(Exception e){
//sendMessage("Connection error: "+e);
//serverGUI.appendEventsLog("Client "+new ClientGUI(username, port)+" failed to connect");
}
}
host is the IP address or the hostname, if server/client are running on the same machine, you can either use "127.0.0.1" or "localhost"; port is a value of int, in your case is 7777
Original:
accept() is a blocking function. the code afterwards would not go thorough until a connection is established.
You have to build a client side and request for connection, once the server and client is connected, you will see "Connection established on port.."
public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

Related

Android socket connection between hotspot and client

I am making an app that checks for certain connections (looking for a list of SSIDs) and creates an hotspot for said connection if it doesn't find any. Basically it should act as a local area network.
I am successful in creating the hotspot, and after that it listens on a server socket for incoming connections:
Thread socketThread = new Thread(new Runnable() {
#Override
public void run() {
int port = Constants.PORT + socketList.size()-1;
try {
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(port);
Log.d(TAG, "Socket listening for connections: " + port);
Socket client = serverSocket.accept();
Log.d(TAG, "Server: connection done");
InputStream inputstream = client.getInputStream();
DataInputStream commStream = new DataInputStream(inputstream);
byte[] b = new byte[16];
while (commStream.read(b,0, 16) != -1)
Log.d(TAG, new String(b, "ASCII"));
serverSocket.close();
Log.d(TAG, "Server socket done");
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
});
socketList.add(socketThread);
socketThread.start();
If I create the hotspot and connect to it with my pc I am able to use netcat and connect with said socket:
netcat 192.168.43.1 8988
Where 192.168.43.1 is the default Android IP address for the hotspot and 8988 is the port I'm using.
However, when I try to do the same through another device running the app and connecting to the hotspot, it doesn't work.
Here's the client code:
clientThread = new Thread(new Runnable() {
#Override
public void run() {
int port = Constants.PORT ;
try {
Socket socket =new Socket();
socket.bind(null);
socket.connect((new InetSocketAddress("192.168.43.1", port)), 20000);
OutputStream stream = socket.getOutputStream();
DataOutputStream commStream = new DataOutputStream(stream);
commStream.write("1234567890123456".getBytes());
socket.close();
Log.d(TAG, "Client socket done");
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
});
clientThread.start();
It doesn't even connect to the server socket, it just waits until timeout. Is there anything I'm doing wrong here?
Thanks in advance.
You cannot create a server socket with IP(192.168.43.1) on the device which has created the hotspot. So just reverse the server socket on another device that has been connected to the hotspot.
e.g. Let's say you have created hotspot on Device A and Device B has been connected to A so create the server socket on Device B with assigned local IP and create Socket instance in A by finding the IP of Device B.

Java Socket/P2P questions

I'm learning about java sockets in a class of mine. I'm having issues with the communication between the peers. My question is why am I receiving this exception:
Exception java.net.ConnectionException: Connection refused: connect
for example:
in my main thread
ServerSocket listenSock = new ServerSocket(Integer.parseInt(argv[2]),0,InetAddress,getByName(null));
//Create Server loop to process inbound connections
while(true){
//accept an incoming client by saving it to a socket to use to communicate with it
Socket clientSock = listenSock.accept();
/waa/create a thread to handle the inbound client so that we can return to accept()
InboundHandler inboundMsg = new InboundHandler(clientSock); // listens to the peers outbound socket
Thread inThread = new Thread(inboundMsg);
inThread.start();
// add the peer to the list.
}
in a seperate thread I have a
public InboundHandler(Socket socket) throws Exception{
this.socket = socket; //save the socket object to "this" instance of the class
input = new BufferedReader(new InputStreamReader(socket.getInputStream())); // listens from the clients speaking socket
}
public void run(){
while(true){
try{
String line = input.readLine();
System.out.println(line);
}catch(IOException e){
System.out.println("Could not read input. " + e);
}
}
and finally I have a thread that sends out messages
for(int i=0;i<portList.size(); i++){
if(nameList.get(i)!=Username){
Socket peerSocket= new Socket(InetAddress.getByName(null),portList.get(i));
writer = new BufferedWriter(new OutputStreamWriter(peerSocket.getOutputStream()));
writer.write(Username + " has connected");
writer.flush();
}
}
}
catch(IOException e){
System.out.println("Some IO problem. " + e);
}
My program compiles, and when I run it, I receive the Exception
java.net.ConnectionException: Connection refused: connect
From what it's sounding like, is the port that my one peer is trying to speak to, isn't being heard by the other peers. But this is more guessing
As for the while(true) I am going to change that later, with a boolean statement.

Interrupt server thread when client closes TCP connection

I have a multithreaded TCP Server in Java which allows connections from several clients and starts a new ServerThread for each connected Client:
Server Class:
while (!Thread.currentThread().isInterrupted()) {
try {
// Create a new thread for each incoming connection.
Socket clientSocket = serverSocket.accept();
ServerThread serverThread = new ServerThread(clientSocket, this);
serverThread.run();
} catch (IOException e) {
e.printStackTrace();
}
}
After a specific timeout, a client closes its socket. How can I interrupt the ServerThread which was connected with the client?
clientsocket.isClosed() and !clientSocket.isConnected() don't work for some reason.
Finally, I got it working with the following snippets (the solution is the socket in the resource block and the endless in.readLine() == null):
Server class
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
// Create a new thread for each incoming connection.
Socket clientSocket = serverSocket.accept();
ServerThread serverThread = new ServerThread(clientSocket, this);
serverThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ServerThread class:
public void run() {
try (Socket socket = clientSocket; // Enable auto-close for socket...
PrintWriter out = ...; BufferedReader in = ...;) {
...
while (!clientSocket.isClosed() && !isInterrupted()) {
if (in.readLine() == null) {
break;
}
}
System.err.println("Client with port " + clientSocket.getPort() + " closed connection to server.");
} catch (IOException e) {
e.printStackTrace();
}
}
Client class: I use the same try with resource block as in the ServerThread class
If the client closes a TCP socket uncleanly without sending an explicit FIN (for example, if the client crashes) then the server will not know about it until it next tries to send a packet to the client (at which point the client will sent an RST packet to tell the server the socket was closed).
Assuming you have control of both client and server code, the most robust way to check the connection is to implement a heartbeat mechanism between the two so they are regularly pinging a small piece of data between them to check the validity of the connection.
Keep alive socket option is the standard way to watch persistent TCP connection.
If your server was in the blocking read operation during the abort you'll get java.io.IOException: read failed. In the asynchronous case you'll receive read key with -1.
In the write state you'll get: Connection reset by peer: socket write error.
Just handle the exceptions or error codes to shutdown the thread if connection is not recoverable.

JVM_Bind error in client/server java app

I am trying to build a java microblogging app
I have finished the code but cannot connect to my own computer due to the following error (I google it and someone said I need to change the port number. I changed the port number and nothing happened)
Exception in thread "Thread-4" java.net.BindException: Address already in use: JVM_Bind
Below is the code for the server:
public class server extends Thread {
public Socket client = null;
public ServerSocket server = null;
private int port = 4444;
public void run(){
while (true){ //loop waits for incomming connection
try { //start the server and listen to a port
server = new ServerSocket(port);
}catch (IOException e){
System.err.println(e);
System.exit(-1);
}
try { //establish a connection when receiving request
client = server.accept();
}catch (IOException e){
System.err.println(e);
System.exit(1);
}
Thread t = new Thread(new Connection(client));
t.start();
}
}
}
And this is the code to start the server and listen to port 4444
Server server = new Server();
server.start(); //to listen to a port
Thank you
You must create the ServerSocket before entering the loop. At present you are trying to create it every iteration, which doesn't make sense, and you aren't closing it, so the second creation fails.

set option for sockets in java

I have a server in Java which listens for incoming connection to a specific port. And everything works as expected, my clients connect to the server and I'm able to send data between them.
My problem is that, when I shut down my client, turn it on again and try to reconnect, it won't connect (my server stays on all the time).
For me to reconnect, I have to restart my server again.
So I tried doing this on my server side:
InetSocketAddress serverAddr = new InetSocketAddress(serverIpAddress, serverPort);
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
//I tries setting up a reuse option
serverSocket.bind(serverAddr);
Even after setReuseAddress() my client won't connect unless I restart my server!
Has anyone any idea of how could that be done?
EDIT2:
try {
while(true){
clientSocket = serverSocket.accept();
System.out.println("S-a conectat clientul de monitorizare!");
os=new ObjectOutputStream(clientSocket.getOutputStream());
try{
coord=(Coordinate)queue.take();
System.out.println(coord.getLat()+coord.getLon()+coord.getVit()+coord.getwId()+coord.getime());
os.writeObject(coord);
os.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
} catch (IOException e) {
System.out.println(e);
try {
clientSocket.close();
os.close();
}catch(Exception e1) {
e1.printStackTrace();
}
}
New edit:
Thread pool server:
Main:
ThreadPooledServer server = new ThreadPooledServer(queue,7001);
new Thread(server).start();
ThreadPooledServer:
public class ThreadPooledServer implements Runnable {
protected ExecutorService threadPool =
Executors.newFixedThreadPool(5);
public void run() {
openServerSocket();
while (!isStopped()) {
Socket clientSocket = null;
try {
System.out.println("Serverul asteapta clienti spre conectare");
clientSocket = this.serverSocket.accept();
clientconnection++;
System.out.println("Serverul a acceptat clientul cu numarul:"
+ clientconnection);
} catch (IOException e) {
if (isStopped()) {
System.out.println("Server Stopped.");
return;
}
throw new RuntimeException("Error accepting client connection",
e);
}
WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);
this.threadPool.execute(workerRunnable);
}
System.out.println("Server Stopped.");
}
public synchronized void stop() {
this.isStopped = true;
try {
this.threadPool.shutdown();
}
catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
serverPort);
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(serverAddr);
} catch (IOException e) {
throw new RuntimeException("Cannot open port", e);
}
}
this.serverSocket.close();
In your run method you accept one client and then go in to an endless loop, trying to write data to the ObjectOutputStream. When the client closes the connection an exception is thrown because you can no longer write to the stream. At this point we're out of the endless loop(while(true) { .. }) and the run method ends.
If you want to keep accepting clients I suggest you move the while loop to the top of your code, above the accept to be exact.
Pseudo-ish code below(note: I'm not catching any exceptions etc.):
while (true)
{
// Wait for a client to connect..
Socket clientSocket = serverSocket.accept();
// Write to the client..
ObjectOutputStream os = new ObjectOutputStream(clientSocket.getOutputStream());
os.writeObject(coord);
os.flush();
}
Is your server single threaded for a purpose (do you only accept one client at a time) ? Usually, servers will spawn a separate thread for every connections, so it can listen more often for incoming connections, and so if the client's connection throws any errors, it won't affect the listening socket. At the moment, your server will listen to only one connection, and if an exception occurs handling the client's connection, simply move on and never listen again. In pseudocode, a typical server is like :
Server listening thread (main thread)
try {
create server socket and bind to port
while server is online
listen for incoming connections
if the client connection is accepted [1]
start client thread
catch exception
handle exception
finally
make sure the server socket is disconnected
cleanup
Server client connection thread
write to client socket to initialize connection
try
while scoket is opened
read data
data treatment
write response
catch exceptions
handle exception [2]
finally
close client socket
cleanup
[1] if your server handles only one client, it should refuse the connection, so the client doesn't wait for no reason
[2] if the exception is not about the socket, the client should be warned by a final write to the socket before closing it
Client thread (on the client's side)
try
connect to server
protocol handshake (optional) [4]
while socket is connected
client server communication
catch exception
handle excpetion
finally
close socket
[4] since the server should write to the socket first, the client should read from it for any welcome message or error messages before attempting to write anything.

Categories

Resources