Socket Initialization Failed with Android Studio - java

I am currently working on a project that requires an android app to connect to a java server that I have created. The problem is that the socket does not initialize. I have added the permissions to the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
My app code looks as follows:
Button Trigger:
View.OnClickListener btnDownloadListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Implement download code
try{
new ConnectionManager().execute("10.90.181.91" , "21002");
}catch(Throwable t){
}
}
};
ConnectionManager Background Function:
protected String doInBackground(String... args) {
try{
Log.i("Test", "background thread started");
int port = Integer.parseInt(args[1]);
InetAddress serverAddr = InetAddress.getByName(args[0]);
Socket connSock = new Socket(serverAddr, port);
Log.i("Test", "Created Socket");
}catch(Throwable t){
}
return "";
}
The log is outputting "background Thread Started", but never outputs "Created Socket"
The Server code is as follows:
Main method
try{
ServerSocket serverSocket = null;
boolean listeningSocket = true;
try {
serverSocket = new ServerSocket(21002);
} catch (IOException e) {
System.err.println("Could not listen on port: 21002");
}
int count = 0;
while(listeningSocket){
try{
Socket clientSocket = serverSocket.accept();
new ServerThread(clientSocket).start();
}catch(Throwable t){
}
}
System.out.println("You should not be here");
serverSocket.close();
}catch(Throwable t){
}
ServerThread
public class ServerThread extends Thread {
private Socket socket = null;
private String s;
private InputStream in;
private OutputStream out;
BufferedReader is;
BufferedWriter os;
public ServerThread(Socket s) {
socket = s;
System.out.println("Thread started: " + socket.getInetAddress());
try{
in = socket.getInputStream();
out = socket.getOutputStream();
out.flush();
is = new BufferedReader(new InputStreamReader(in));
os = new BufferedWriter(new OutputStreamWriter(out));
}catch(Throwable t){
t.printStackTrace();
}
}
public void run() {
System.out.println("Thread is running");
String dataType = "";
Boolean awaitingTransfer = false;
try {
while(true){
if(in.available() > 0 && !awaitingTransfer){
dataType = is.readLine();
System.out.println(dataType);
}
if(in.available() > 0 && awaitingTransfer){
try{
ArrayList<SpotCheck> tempList = new ArrayList<SpotCheck>();
while(in.available() > 0){
//tempList = (ArrayList<SpotCheck>) in.readObject();
}
ServerMain.manager.applyChanges(tempList);
awaitingTransfer = false;
}catch(Throwable t){
t.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
try {
in.close();
out.close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Thanks for the help!

As an amateur coder, i just wanted to ask you that, can you just add a toast to the "catch()" blog of your doInBackground like that
catch(Exception e)
{
runOnUiThread(new Runnable(){
#Override
public void run(){
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
});
}
so it can show your problem more specifically and it will become easier to handle maybe. but again i am not sure if it works, i mean using toast in asynctask classes is a little bit more tricky.

Related

Android Sockets not working in reality

I am trying to connect an Android device to a java server. It works perfectly when I use the emulator but when I port it onto my phone there is no connection.
The aim of the code is to send a value from client to server, perform a calculation on it and return it back to the client to be displayed.
This is my server code:
public class ServerTest {
public static final int PORT_NUMBER = 8000;
protected Socket socket;
private ServerTest(Socket socket) {
this.socket = socket;
System.out.println("New client connected from " + socket.getInetAddress().getHostAddress());
connect();
}
public void connect() {
InputStream in = null;
OutputStream out = null;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String request = br.readLine();
if (request.equals("end")) {
System.out.println("Message received: " + request + ". Ending connection.");
request = "End Connection";
out.write(request.getBytes());
in.close();
out.close();
socket.close();
System.exit(0);
} else {
System.out.println("Message received: " + request);
request = calculatePi(request);
System.out.println("Output: " + request);
out.write(request.getBytes());
}
} catch (IOException ex) {
System.out.println("Unable to get streams from client");
} finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println("Welcome. IP address is: " + getIP());
ServerSocket server = null;
try {
server = new ServerSocket(PORT_NUMBER);
while (true) {
new ServerTest(server.accept());
}
} catch (IOException ex) {
System.out.println("Unable to start server.");
} finally {
try {
if (server != null)
server.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static String getIP() {
String ip = "";
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
// *EDIT*
if (addr instanceof Inet6Address) continue;
ip = addr.getHostAddress();
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return ip;
}
and this is my client side code on device:
public class MainActivity extends AppCompatActivity {
TextView piResultTextView;
EditText addressEditText, messageEditText;
Button connectButton;
Handler handler = new Handler();
Results results;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
results = new Results();
addressEditText = findViewById(R.id.AddressEditText);
messageEditText = findViewById(R.id.MessageEditText);
connectButton = findViewById(R.id.ConnectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
connect();
}
});
piResultTextView = findViewById(R.id.PiResultTextView);
}
public void connect() {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
String hostAddress = addressEditText.getText().toString();
int port = 8000;
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(hostAddress, port);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
Toast.makeText(getApplicationContext(), "Unknown host: " + hostAddress, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Unable to get streams from server", Toast.LENGTH_SHORT).show();
}
String input = messageEditText.getText().toString();
try {
out.println(input);
results.pi = in.readLine();
handler.post(new Runnable() {
#Override
public void run() {
piResultTextView.setText(results.pi);
}
});
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Unable to read input stream from server", Toast.LENGTH_SHORT).show();
}
try {
out.close();
in.close();
echoSocket.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error closing streams", Toast.LENGTH_SHORT).show();
}
}
});
thread.start();
}
}

Java Socket connection "crashes" after reconnect

I want to create a socket connection between my server and my client. My client should reconnect, after the connection timed out. For this, I try every 3 seconds to connect with my socket. And this works. But then, when I want to start reading, but then I get the error "socket closed". What could be the mistake?
Here is the error log (It's on pastebin, it doesn't formate here right):
http://pastebin.com/2t8zAvcx
But I try to get the IP from the socket before I start reading and this works...
Here is my reconnect method:
public void reconnect(){
new Thread(new Runnable() {
#Override
public void run() {
try{
try{
socket.close();
socket = null;
}catch (IOException e) {
}
socket = new Socket(BackgroundService.HOSTNAME, BackgroundService.PORT);
connected = true;
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeUTF(username);
output.flush();
}catch(UnknownHostException e){
connected = false;
}catch(IOException e){
connected = false;
}
}
}).start();
}
Here I wait for an input:
public Notification waitForNotification(){
try{
if(!(connected))return null;
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String input;
Log.e("Before Input", socket.getInetAddress()+"");
while((input = inputStream.readUTF()) != null){
Notification notification = new Notification(input);
return notification;
}
return null;
} catch (IOException e) {
e.printStackTrace();
Log.e("Lost Connection3", "ERROR");
connected = false;
return null;
}
}
And here I manage it:
#Override
protected void onHandleIntent(Intent intent) {
Log.e("HII", "HII");
username = getDataFromStorage.loginData(getApplicationContext())[0];
new Thread(new Runnable() {
#Override
public void run() {
ConnectionService connectionService = new ConnectionService(username);
while (true) {
if(!(connectionService.getConnectionState())){
try {
Thread.sleep(3000);
connectionService.reconnect();
continue;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Notification notification = connectionService.waitForNotification();
if(notification == null){
continue;
}
processStartNotification(notification);
}
}
}).start();
}
And here I start the connection:
public ConnectionService(String username){
this.username = username;
try{
socket = new Socket(BackgroundService.HOSTNAME, BackgroundService.PORT);
connected = true;
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeUTF(username);
output.flush();
}catch(UnknownHostException e){
connected = false;
}catch(IOException e){
connected = false;
}
}

Android Socket error with no message

I create a simple network-testing application.
But It doesn't work and always throw error at Socket().
I tried to check error message by using getMessage(), there was no message printed.
What should I do?
public static void main(String args[]){
ServerSocket serverSocket = null;
Socket socket = null;
try{
serverSocket = new ServerSocket(14100);
socket = serverSocket.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] arr = new byte[100];
in.read(arr);
System.out.println(new String(arr));
String str = "Hello Client";
out.write(str.getBytes());
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
try{
socket.close();
}
catch(Exception ignored){}
try{
serverSocket.close();
}
catch(Exception ignored){}
}
}
Below is android code for Client.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView tv = (TextView) findViewById(R.id.id_tv);
Socket socket = null;
try{
socket = new Socket("192.168.0.52",14100);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
String str = "Hello Server";
out.write(str.getBytes());
byte arr[] = new byte[100];
in.read(arr);
tv.setText(new String(arr));
}
catch(UnknownHostException uhe){
}
catch(IOException ioe){
}
catch(Exception e){
}
finally{
try{
socket.close();
}
catch(Exception ignored){
}
}
}
Of course, I added permisson for android in AndroidManifest.xml
Move the socket code out of UI main thread onCreate() to an AsyncTask or a regular thread.
For a quick test, wrap all your socket code into a thread like so:
new Thread() {
public void run() {
// Move your socket code here for a quick test
}
}.start();

Outputting Every Other Time

I have made an application that opens up a socket via a thread and updates based on what is typed. Here is the code:
Server.java:
public class Server {
public static void main(String[] args) throws IOException {
int portNumber = 2392;
boolean listening = true;
System.out.println("Server: Running...");
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
System.out.println("Server: Connected to Client!");
while (listening) {
new ServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection( '" + e.getMessage() + "' );");
} finally {
System.out.println("Server: Disconnecting...");
}
}
}
Server Thread.java:
public class ServerThread extends Thread {
private Socket socket = null;
Scanner reader = new Scanner(System.in);
public ServerThread(Socket socket) {
super("ServerThread");
this.socket = socket;
}
public void run() {
System.out.println("Ruasd");
try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
String outputLine = "";
while (!outputLine.equals("Disconnect")) {
outputLine = reader.nextLine();
out.println(outputLine);
}
socket.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java:
public class MainActivity extends Activity {
private Socket socket;
private TextView status;
private BufferedReader in;
private Handler mHandler;
private static final int SERVERPORT = 2392;
private static final String SERVER_IP = "...ip#...";
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_vitals);
status = (TextView) findViewById(R.id.text_status);
new Thread(new CommunicationThread()).start();
}
#Override
protected void onStop() {
super.onStop();
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class CommunicationThread implements Runnable {
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
final String fromServer = in.readLine();
System.out.println("Server: " + fromServer);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
status.setText(fromServer);
}
});
if (fromServer.equals("Disconnect."))
break;
}
} catch (IOException e) {
e.printStackTrace();
}
It works perfectly the first time and outputs to the status TextView correctly. However, when I restart the application, it outputs every other word. For instance, If I type "Hey" "Hi "You" "How", I will see "Hi" and "How" in the TextView the second time I start the application.
What's really odd to me is that when I do System.out.println("Server: " + fromServer) it is outputting all values. Any suggestions are greatly appreciated.
Ok I think I found the problem (got it working for me, that is). In your CommunicationThread, you didn't have a while loop. Also, you need to iterate the server's input until it is null. See below:
class CommunicationThread implements Runnable {
#Override
public void run() {
try {
// keep the connection alive.
while (true) {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String fromServer;
// get server messages until there are none
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
status.setText(fromServer);
}
});
if (fromServer.equals("Disconnect."))
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Please get back to me on how it works out :)

Connection refused: when connecting a java client to an Android server(Emulator)

I've tried out almost everything.
Forwarding from ADB Shell using adb forward TCP:12345 TCP:12345
Using 10.0.2.2 (without forwarding) to listen to my host machine
Setting the INTERNET permission in the manifest
Setting the thread policy to permit all functions
I'm running my client java program in 12345 port and I have a ServerSocket in the Android program that listens over the same port. But when I run my client (after running the server program on the emulator) and enter the String that I want to transfer, I get the exception saying 'Connection Refused'.
java.net.ConnectException: Connection refused: connect
Here's my server program:
public class MainActivity extends Activity {
TextView tv;
ServerSocket ss = null;
String mClientMsg = "";
Thread myCommsThread = null;
public static final int SERVERPORT = 12345;
protected static final int MSG_ID = 0x1337;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = new TextView(this);
tv.setText("Nothing from client yet");
setContentView(tv);
this.myCommsThread = new Thread(new CommsThread());
this.myCommsThread.start();
}
#Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ID:
// TextView tv = (TextView) findViewById(R.id.TextView01);
tv.setText(mClientMsg);
setContentView(tv);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
Socket s = null;
try {
ss = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
Message m = new Message();
m.what = MSG_ID;
try {
if (s == null)
s = ss.accept();
BufferedReader input = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String st = null;
st = input.readLine();
mClientMsg = st;
myUpdateHandler.sendMessage(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
My Java client program is as follows:
public class TCPSender {
public static void main(String args[]) {
try {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter the file name:");
#SuppressWarnings("deprecation")
String f = dis.readLine();
File f1 = new File(f);
FileReader fr = new FileReader(f1);
Socket s = new Socket("127.0.0.1", 12345);
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(f);
int c = 0;
while ((c = fr.read()) != -1)
pw.println(c);
System.out.println("File content are sent....");
fr.close();
s.close();
} catch (Exception e) {
System.out.println("" + e);
}
}
}

Categories