JAVA Socket Accept hangs - java

Below code hangs does not go past beyond below messages and is hanging at socket.accept().
Its part of an android app and INTERNET permissions are already added to manifest. Tried putting accept in while(true) loop...but that did not work:
09-23 15:55:43.737 24079-25436: Trying to open port 5556...
09-23 15:55:43.739 24079-25436: >>> Opening Port : 5556
private Socket socket = null;
private ServerSocket server = null;
private static final String TAG = "MyActivity";
public boolean openPort(String host , int port){
try {
server = new ServerSocket(port);
} catch (IOException e) {
Log.e(TAG , "Error initializing serverSocket: " , e);
}
Log.e(TAG , ">>> Opening Port : " + port);
boolean bmsg = false;
try {
socket = server.accept();
Log.e(TAG , ">>> Opened Port : " + port);
return true;
} catch(IOException e){
Log.e(TAG, "Error opening port: ", e);
return bmsg;
} catch(SecurityException s){
Log.e(TAG, "sError opening port: ", s);
return bmsg;
} catch(IllegalBlockingModeException b){
Log.e(TAG, "bError opening port: ", b);
return bmsg;
}
}
Above class is being called from below async task on button click:
private class openPort extends AsyncTask{
#Override
protected Object doInBackground(Object[] objects) {
Log.i(TAG , "Trying to open port 5556...");
//host is localhost
//port is 5556
OpenClosePort ocp = new OpenClosePort();
ocp.openPort(host , port);
return null;
}
}

Related

Connect to mining pool

I am trying to etablish a connection to a monero mining pool. I know that the mining pools using the stratum protocol. But the only thing I receive is a connection timeout if I try to create a socket:
try{
InetAddress address = InetAddress.getByName("pool.supportxmr.com");
Log.d("miner","Attempting to connect to " + address.toString() + " on port " + port + ".");
Socket socket = new Socket(address, 3333);
Log.d("miner", "Connection success");
}catch (IOException e){
e.printStackTrace();
}
SupportXmr is just an example. Its not working with any pool. What am I doing wrong?
Try with port 80. Make sure you wrote INTERNET permission to AndroidManifest and use AsnycTask.
private class AsyncExec extends AsyncTask<Void,Void,Void>{
#Override
protected Void doInBackground(Void... voids) {
int port=80;
try
{
InetAddress address = InetAddress.getByName("pool.supportxmr.com");
Log.d("miner","Attempting to connect to " + address.toString() + " on port " + port + ".");
Socket socket = new Socket(address, 3333);
Log.d("miner", "Connection success");
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
- Also don't forget to call new AsyncExec().execute().

Android and java sockets. Sockets connection attempted many times before success

I am trying to get into IoT and want to make an app send a string from an android phone to a Linux pc. The app does this by implementing an asynctask:
//From the java docs, slightly modified
private Void sendThroughSocket(String s, String host, int port) {
Log.d("E", "In send through socket");
final String hostName = host;//Host is the address of the receiver, can be IP or domain
int portNumber = port;
//Check if device is connected to internet
try {
Socket clientsocket = new Socket(hostName, portNumber); //one of 2-way point communication
Log.d("E", "Created Socket: ");
DataOutputStream DOS = new DataOutputStream(clientsocket.getOutputStream());
if (clientsocket.isConnected())
Log.d("E", "Socket connected");
DOS.writeUTF(s);
clientsocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
new Runnable() {
public void run() {
Toast.makeText(context, "Don't know about host " + hostName, Toast.LENGTH_SHORT).show();
}
};
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
//Toast can not be run using asynctask since it acesses UI
new Runnable() {
public void run() {
Toast.makeText(context, "Couldn't get I/O for the connection to " + hostName + " , check the port", Toast.LENGTH_SHORT).show();
}
};
} catch (Exception e) {
Log.d("E", "#fares" + e.getClass().getName().toString());
}
return null;
}
On 3 occasions do I try to send the string, all triggered from my seekbar (I send the seek bar progress value as my string):
public void onProgressChanged(SeekBar seekBar, int i, boolean b)
public void onStartTrackingTouch(SeekBar seekBar)
public void onStopTrackingTouch(SeekBar seekBar)
The implementation for all 3 is the same:
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
progressvalue = i;
textView.setText("Brightness = " + progressvalue + " %");
if (((RecieverPort.equals("Please enter port Number")) || (RecieverIP.equals("Please enter receiver IP")))) {
//Make sure toast isn't persistent
if (IPPortToastcount++ == 0)
Toast.makeText(MainActivity.this, "Please set both IP and Port ", Toast.LENGTH_SHORT).show();
} else {
//Check if connected to internet
if (!isConnectedtoInternet(MainActivity.this)) {
if (ConnectivityToastCount++ < 1)
Toast.makeText(MainActivity.this, "You are not connected to the Internet", Toast.LENGTH_SHORT).show();
} else {
//Send text over wifi
SendText ST = new SendText(getApplicationContext());
ST.execute(String.valueOf(progressvalue), RecieverIP, RecieverPort);
ST.cancel(false);
}
}
}
Mainly
//Send text over wifi
SendText ST = new SendText(getApplicationContext());
ST.execute(String.valueOf(progressvalue), RecieverIP, RecieverPort);
ST.cancel(false);
The server side (my pc) is pretty simple:
int portNumber = 44339; // Choose unused port on router
//Open a socket
try {
//System.out.println("in try statement");
try (ServerSocket serverSocket1 = new ServerSocket(portNumber)) {
portNumber = serverSocket1.getLocalPort();
System.out.println("Created socket at port " + portNumber);
Socket clientSocket = serverSocket1.accept();
System.out.println("Accepted");
DataInputStream DIS = new DataInputStream(clientSocket.getInputStream()); //get input from socket
//System.out.println("Created reader");
String inputLine;
// System.out.println("About to read");
while (DIS.available() > 0) {
inputLine = DIS.readUTF();
System.out.println(inputLine);
}
}
} catch (Exception e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getClass().getSimpleName());
}
This kind of works, except it takes a very long time(seconds) before the server socket is accepted. It also only works in onprogresschanged , which leads me to believe that multiple attempts of
//Send text over wifi
SendText ST = new SendText(getApplicationContext());
ST.execute(String.valueOf(progressvalue), RecieverIP, RecieverPort);
ST.cancel(false);
are needed before one succeeds at creating the socket and connecting to the pc. How can I make sure that one tap, or one call of the function will be enough to send the string?
Sorry for the long post but it's my first time asking :)
Edit: my new server code:
try {
//System.out.println("in try statement");
try ( ServerSocket serverSocket1 = new ServerSocket(portNumber))
{
portNumber = serverSocket1.getLocalPort();
System.out.println("Created socket at port " + portNumber);
while(true){
Socket clientSocket = serverSocket1.accept();
// System.out.println("Accepted");
DataInputStream DIS = new DataInputStream(clientSocket.getInputStream()); //get input from socket
//System.out.println("Created reader");
//String inputLine;
//System.out.println("About to read");
System.out.println(DIS.readUTF());
}
}
} catch (Exception e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getClass().getSimpleName());
}
Worth noting that even in the while true loop, the server will always display 4 numbers then stop.
Edit: here is an example log:
10-06 20:08:09.145 26372-26372/com.example.fares.ledslider D/E: #fares in test method value = 50
10-06 20:08:26.475 26372-26372/com.example.fares.ledslider D/E: #Fares in start tracking
10-06 20:08:26.722 26372-27004/com.example.fares.ledslider D/E: #fares Socket connected
10-06 20:08:26.810 26372-26764/com.example.fares.ledslider D/E: #fares Socket connected
10-06 20:08:27.241 26372-27003/com.example.fares.ledslider D/E: #fares Socket connected
10-06 20:08:27.304 26372-26372/com.example.fares.ledslider D/E: #fares in stop tracking
ST.cancel() can be removed. Also not waiting for availability looks not good.

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.

Using a Handler to pass Object to the UI thread in order to make some UI updates

I have this client class "T_Client" in a client-server implementation
T_Client:
public class T_Client{
private static final String TAG = "T_Client";
private static String serverIP = "192.168.2.5";
private static int port = 4444;
private InetAddress serverAddr = null;
private Socket sock = null;
private boolean running = false;
private ObjectInputStream in;
private ObjectOutputStream out;
Object objIn;
public void send(MessageCustom _msg) {
if (out != null) {
try {
out.writeObject(_msg);
out.flush();
// out.close();
Log.i("Send Method", "Outgoing : " + _msg.toString());
} catch (IOException ex) {
Log.e("Send Method", ex.toString());
}
}
}
public void stopClient() {
running = false;
}
public void run() {
running = true;
try {
// here you must put your computer's IP address.
serverAddr = InetAddress.getByName(serverIP);
Log.i("TCP Client", "C: Connecting...");
// create a socket to make the connection with the server
sock = new Socket(serverAddr, port);
try {
// send the message to the server
out = new ObjectOutputStream(sock.getOutputStream());
// receive the message which the server sends back
in = new ObjectInputStream(sock.getInputStream());
Log.i("TCP Client", "C: Connected.");
// in this while the client listens for the messages sent by the
// server
while (running) {
objIn = in.readObject();
Log.i("Object Read", objIn.toString());
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + objIn
+ "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
// the socket must be closed. It is not possible to reconnect to
// this socket
// after it is closed, which means a new socket instance has to
// be created.
out.close();
in.close();
sock.close();
Log.i(TAG, "Closing socket: " + sock);
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
}
And I want to get the Object from the server(objIn) and pass it to the UI thread so I can do some UI updates there. From searching around a bit, I think that I have to use a Handler, but can't really wrap my head around the implementation.
Could someone give me a simple example of how to implement a handler in my case, and make the call in a Main Activity? Or point me to a simple tutorial to get me started, cause I've tried to follow the one over at the Android Developers site but it's too complicated.

TCP Server-Server communication

I'm developing an Android application (client) and want it to connect with my Java server using TCP communication, and so far everything is going well.
Server Code:
import java.net.*;
import java.io.*;
import globalvariables.GlobalVariables;
import interface_package.ServerInterface;
import java.util.Timer;
/**
*
* #author wsserver
*/
public class ThreadedAndroidServer {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
GlobalVariables.init();
//Prevzemi staticen interface
GlobalVariables.sinterface = new ServerInterface();
GlobalVariables.sinterface.show();
//INFINITE LOOP
while(true)
int port = GlobalVariables.portNo;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("Server has started listening on port " + port);
GlobalVariables.sinterface.setServerStatus("Server has started listening on port " + port);
} catch (IOException e) {
System.out.println("Error: Cannot listen on port " + port + " : " + e);
GlobalVariables.sinterface.setServerStatus("Error: Cannot listen on port " + port + " : " + e);
System.exit(1);
}
while (true) // infinite loop - loops once for each client
{
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept(); //waits here (forever) until a client connects
System.out.println("Server has just accepted socket connection from a client");
GlobalVariables.sinterface.setServerStatus("Server has just accepted socket connection from a client");
} catch (IOException e) {
System.out.println("Accept failed: " + e);
GlobalVariables.sinterface.setServerStatus("Accept failed: " + e);
break;
}
// Create the Handle Connection object - our new thread object - only create it
ThreadedHandleConnection con = new ThreadedHandleConnection(clientSocket);
if (con == null) //If it failed send and error message
{
try {
ObjectOutputStream os = new ObjectOutputStream(clientSocket.getOutputStream());
os.writeObject("error: Cannot open socket thread");
os.flush();
os.close();
} catch (Exception ex) //failed to even send an error message
{
System.out.println("Cannot send error back to client: " + ex);
GlobalVariables.sinterface.setServerStatus("Cannot send error back to client: " + ex);
}
} else {
con.start();
} // otherwise we have not failed to create the HandleConnection object
// start this thread now
}
try // do not get here at the moment
{
System.out.println("Closing server socket.");
GlobalVariables.sinterface.setServerStatus("Closing server socket.");
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close server socket. " + e.getMessage());
GlobalVariables.sinterface.setServerStatus("Could not close server socket. " + e.getMessage());
}
}
}
Connection Handler:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tcpServer_package;
import java.net.*;
import java.io.*;
import java.util.*;
import busimesslogic_package.Functions;
/**
*
* #author wsserver
*/
public class ThreadedHandleConnection extends Thread {
private Socket clientSocket; // Client socket object
private ObjectInputStream is; // Input stream
private ObjectOutputStream os; // Output stream
// The constructor for the connecton handler
public ThreadedHandleConnection(Socket clientSocket) {
this.clientSocket = clientSocket;
}
// The main thread execution method
public void run() {
try {
this.is = new ObjectInputStream(clientSocket.getInputStream());
this.os = new ObjectOutputStream(clientSocket.getOutputStream());
while (this.readCommand()) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Receive and process incoming command from client socket
private boolean readCommand() {
String wholeCommand = null;
try {
wholeCommand = (String) is.readObject();
} catch (Exception e) {
wholeCommand = null;
}
if (wholeCommand == null) {
this.closeSocket();
return false;
}
System.out.println("Received: "+wholeCommand);
//GET COMMAND PARAMETARS
String[] commParams = wholeCommand.split(";");
//GET COMMAND TYPE
int type = Integer.parseInt(commParams[0]);
//SELECT COMMAND PROCEDURE
Functions functions = new Functions();
String IPaddress = clientSocket.getRemoteSocketAddress().toString();
IPaddress = IPaddress.substring(IPaddress.indexOf("/")+1, IPaddress.indexOf(":"));
switch (type) {
case 1: {
String sendText = getTextToSend();
send(sendText);
break;
}
default:{
sendError("0;"+wholeCommand);
break;
}
}
System.gc();
return true;
}
// Send a message back through to the client socket as an Object
private void send(Object o) {
try {
System.out.println("Sending " + o);
this.os.writeObject(o);
this.os.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Send a pre-formatted error message to the client
public void sendError(String msg) {
this.send("error:" + msg); //remember a string IS-A object!
}
// Close the client socket
public void closeSocket() //close the socket connection
{
try {
this.os.close();
this.is.close();
this.clientSocket.close();
} catch (Exception ex) {
System.err.println(ex.toString());
}
}
}
Android Client Code:
package com.example.zpbitolaoperator;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import android.util.Log;
public class TCPClient{
//Comunication variables
private static Socket socket;
private static ObjectOutputStream os;
private static ObjectInputStream is;
//Server parametars
static String serverIP;
static int port;
//Communication status
static boolean connectionStatus = false;
public static boolean connectToServer() {
try // open a new socket to port: and create streams
{
serverIP = GlobalVariables.serverIP;
port = GlobalVariables.portNo;
socket = new Socket(serverIP, port);
os = new ObjectOutputStream(socket.getOutputStream());
is = new ObjectInputStream(socket.getInputStream());
Log.d("DEBUG", "Connected to Server");
connectionStatus = true;
return true;
} catch (Exception ex) {
Log.d("DEBUG", "Failed to Connect to Server " +ex.toString(), ex.getCause());
connectionStatus = false;
return false;
}
}
public static boolean closeConnection(String message){
try{
socket.close();
Log.d("DEBUG", "Closed connection to Server");
return true;
} catch (Exception ex){
Log.d("DEBUG", "Failed to close connection to Server " +ex.toString(), ex.getCause());
return false;
}
}
private static String sendMessage(String message) {
String returnString;
send(message);
returnString = (String) receive();
if (returnString != null) {
Log.d("DEBUG", "Server returned: " + returnString);
}else{
return returnString = "ERROR";
}
return returnString;
}
// method to send a generic object.
private static void send(Object o) {
try {
Log.d("DEBUG", "Sending: " + o);
os.writeObject(o);
os.flush();
} catch (Exception ex) {
Log.d("DEBUG", "Sending to server " +ex.toString(), ex.getCause());
}
}
// method to receive a generic object.
private static Object receive() {
Object o = null;
try {
o = is.readObject();
} catch (Exception ex) {
Log.d("DEBUG", "Receive from server " +ex.toString(), ex.getCause());
}
return o;
}
/**
* Isprakja poraka do server
* #param message
* #return ili ERROR ili poraka
*/
public synchronized static String sendToServer(String message) {
String rez = "";
try {
rez = sendMessage(message);
} catch (Exception ex) {
Log.d("DEBUG", "Send to server " +ex.toString(), ex.getCause());
}
return rez;
}
public static boolean getCommunicationStatus(){
return connectionStatus;
}
}
I am sending String data to my server (comand;param1;param2....), the server proces that data and returns some data do the android application. The android application is the TCP Client and the java application is the server. For every connection the server creates thread (infinite) that handles that connection. The problem is that my server cant send something without the client sending the request first. The ObjectInputStream readObject() blocks the thread until the client send some data. I want to use the same socket to send in the other direction (java -> android and the android application send some data back) .I know that this can be done by opening another socket where the Android will be the server and the java application will be the client. Is this posible and how?
If you want to be able to concurrently receive data from a client and send data to the client then you'll have to either use two threads per client (one to receive and one to send) or use non-blocking IO.

Categories