Java Socket - hanging on socket creation
Java Socket - hanging on socket creation
I have an Android device (client) and Ubuntu 16.04 machine (server). I'm trying to establish a socket connection to send messages to and from each.
I had it working earlier on port 6943
, now it hangs somewhere. I am unsure how to proceed in debugging this, no exceptions or errors occur. It simply hangs up on Socket s = new Socket("192.168.0.216", 6943)
(client), because the line Log.d(TAG, "Creating I/O streams");
no longer runs.
6943
Socket s = new Socket("192.168.0.216", 6943)
Log.d(TAG, "Creating I/O streams");
Note: "xxx.xxx.x.xxx"
is the IP address of my Ubuntu machine.
"xxx.xxx.x.xxx"
Client-side code:
public class SocketManager {
public static String TAG = "SocketManagerClass";
public static void createSocket() {
try {
Scanner scn = new Scanner(System.in);
Log.d(TAG, "Creating new socket object");
Socket s = new Socket("xxx.xxx.x.xxx", 6943);
Log.d(TAG, "Creating I/O streams");
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while (true) {
Log.d(TAG, dis.readUTF());
String toSend = scn.nextLine();
dos.writeUTF(toSend);
if (toSend.equals("Exit")) {
Log.d(TAG, "Closing this connection");
s.close();
break;
}
String received = dis.readUTF();
Log.d(TAG, received);
}
scn.close();
dis.close();
dos.close();
} catch (Exception e) {
Log.e(TAG, "exception", e);
}
}
}
The client-side code is initiated inside MainActivity
like so:
MainActivity
public class ConnectSocket extends AsyncTask<Void, Void, Void> {
public Void doInBackground(Void... params) {
// Create instance of the socket manager class then execute its' function
try {
Log.d("ConnectSocket", "Creating instance and running func");
socketManager.createSocket();
} catch (Exception e) {
Log.e(TAG, "exception", e);
}
return null;
}
}
Then ran inside the onCreate
method of the MainActivity
as follows:
onCreate
MainActivity
ConnectSocket connectSocket = new ConnectSocket();
connectSocket.execute();
Server-side code (I am simply running this from the command line after compiling):
import java.net.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class SocketServer {
public static void main(String args) throws Exception {
ServerSocket serverSocket = new ServerSocket(6943);
System.out.println("Creating socket server...");
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
System.out.println("New socket accepted: " + socket);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
I can see the System.out.println("Creating socket server...");
line in the terminal.
System.out.println("Creating socket server...");
It must be an issue with the port 6943
. netstat -l | grep 6943
returns nothing, and port 6943
isn't in sight when I use netstat -l
. Did this port vanish into thin air? What is going on here?
6943
netstat -l | grep 6943
6943
netstat -l
@EJP no exception thrown! I was actually able to access the port again after a couple hours (no code was changed at all). This is a little alarming
– spoiledgoods
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What exception is thrown? NB The word is 'blocks', not 'hangs', in this case.
– EJP
yesterday