Android socket not created - android

I'm developing an Android app that is going to send camera stream to a Node.js web server by Socket. When the app has to create the socket the app doesn't execute the code inside the try statement but even though launchs any catch exception.
MyThread.java
protected Void doInBackground(Void... unused) {
OutputStream os = null;
try {Log.d("MyCameraApp", "HERE1");
mSocket = new Socket(ip, port);
if (mSocket != null) {Log.d("MyCameraApp", "SOCKET CONNECTED");
try {
os = mSocket.getOutputStream();
while (true) {
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(4);
dos.writeUTF("####");
dos.writeInt(mFrameBuffer.size());
dos.writeUTF("-##-");
dos.flush();
dos.write(mFrameBuffer.toByteArray());
dos.flush();
Thread.sleep(1000 / 15);
}
} catch (Exception e) {
e.printStackTrace();
try {
if (os != null)
os.close();
} catch (Exception e2) {
e.printStackTrace();
}
}
}
else {
Log.d("MyCameraApp", "SOCKET NULL");
}
}
catch(UnknownHostException e) {
Log.d("MyCameraApp", "CATCH SOCKET");
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
CameraActivity.java
try {
mThread = new MyThread(getApplicationContext(), SERVERIP, SERVERPORT);
mThread.execute();Log.d("MyCameraApp", "WELL DONE");
}
catch(Exception e) {
e.printStackTrace();
Log.d("MyCameraApp", "CATCH");
}
The CameraActivity create a mThread object and the app only displays the log 'HERE1' inside the try, before the socket is created and then displays the log 'WELL DONE'.
I have a basic Node.js server that only is listening in the correspondent port, nothing else.
What is wrong?
Thanks in advance!

The Android device was connected by wifi and the pc was connected by Ethernet... differents subnets.

Related

Android - Bluetooth connection InputStream close crashes my app

I have an Android application where I connect to a bluetooth device. When I shut the device off, the application tries to disconnect. So I try to close the inputstream the outputstream and the socket. Even though I have a try catch on each close try, the app crashes when it tries to close the inputstream. Any ideas? Code example:
private void resetConnection() {
if (mmInputStream != null) {
try {
mmInputStream.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE InputStream", e);
}
mmInputStream = null;
}
if (mmOutputStream != null) {
try {
mmOutputStream.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE OutputStream", e);
}
mmOutputStream = null;
}
if (mmSocket != null) {
try {
mmSocket.close();
} catch (Exception e) {
Log.e(LOG_TAG, "CANNOT CLOSE mmSocket", e);
}
mmSocket = null;
}
}
Any suggestions? Thanks

how to send data from Group owner to client with wifi p2p

I tested many ways and finally asked this question. as many of articles mentioned in wifi-direct all clients know group owner's IP and can use this ip to send a message and group owner will save clients ip address. but I can't send a message from group owner to client like that client sent first time. I faced with this error's:
first:
failed to connect to /192.168.49.24 (port 8988) after 5000ms: isConnected failed:
EHOSTUNREACH (No route to host).
after change code:
first error + bind failed: EADDRINUSE (Address already in use).
My AsyncTask to retrieve :
#Override
protected String doInBackground(Void... params) {
ServerSocket serverSocket = null;
Socket client = null;
DataInputStream inputstream = null;
try {
serverSocket = new ServerSocket(8988);
client = serverSocket.accept();
inputstream = new DataInputStream(client.getInputStream());
String str = inputstream.readUTF();
String IP = client.getInetAddress().toString();
serverSocket.close();
return IP+"+"+str;
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
return null;
}finally{
if(inputstream != null){
try{
inputstream.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(client != null){
try{
client.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
if(serverSocket != null){
try{
serverSocket.close();
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
}
}
}
}
and my IntentService to send messages:
#Override
protected void onHandleIntent(Intent intent) {
Context context = getApplicationContext();
if (intent.getAction().equals(ACTION_SEND_IP)) {
String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
Log.e("DAVUD","Host:"+ host);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
Log.e("DAVUD","Port:"+ port);
DataOutputStream stream = null;
try {
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
stream = new DataOutputStream(socket.getOutputStream());
String str = intent.getStringExtra("message");
stream.writeUTF(str);
} catch (IOException e) {
Log.e(WiFiDirectActivity.TAG, e.getMessage());
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
and some other codes I tested... There is another question asked same this but not answered(android-wifi-direct-how-to-send-data-from-group-owner-to-the-clients) this project based on wifiDirectDemo Simple. Please help I really need it.
After one year I sew my question again. the problem was not about wifi or connection. it's about string parsing. where a line in doInBackground is:
return IP+"+"+str
and in onPostExecute I parsed and get ip from returned string; but parse code was not correct. so returns:
192.168.49.24
instead of:
192.168.49.241
where two of them is valid ips I am not thought parse logic had problem. I changed code and used String[] instead of String.

Android socket connect freezing

I am trying to implement a simple socket that sends and receives strings from a server.
The following code is freezing the application, not sure if I have done something obviously wrong?
public String internetRoutesRetrieve(String userName) {
String command = null;
String response = null;
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("Hidden IP", HiddenPort);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
command = "SEARCH <" + userName + ">";
dataOutputStream.writeUTF(command);
response = dataInputStream.readUTF();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response;
}
Thanks
Edit: It seems the program is freezing when I am trying to save the response from the server
see AsyncTask for proper client server communication on Android application.
you'd usualy get android.os.NetworkOnMainThreadException if you don't but I'd give it a try.

Android Printing via Bluetooth SPP

I need to print plain text in my android application. As far as I can tell I need to create an socket with SPP UDID to my device.
The sample codes I've seen, people just get an OutputStream from the Socket and the push the string with the text they want to print.
Is is that simple? Just push text through OutputStream?
Try this for print text:
try {
Socket clientSocket = null;
DataOutputStream outToServer;
try {
clientSocket = new Socket("192.168.101.64", 9100);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeUTF("Some string to print");
}
outToServer.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}

exception while trying to receive a file from server

i run some code to connect to a server, send it an Object and then receive another Object.
while running this on my Android enulator (v2.2) i get - java.net.SocketException: Socket is closed
the connection to the server is successful and i'm able to send the object but when i'm trying to do socket.getInputStream() it throws the exception
this is my connector class:
public class ConnectionToServer {
UserProblemRequest sentProblem;
Problem responseProblem;
Socket socket;
public ConnectionToServer(){
sentProblem = null;
responseProblem = null;
socket = null;
}
public void connect(){
try {
Log.d(TAG, "Connecting...");
socket = new Socket(Utils.SERVER_IP, Utils.SERVER_PORT);
Log.d(TAG, "SUCCESS: Connected!");
} catch (UnknownHostException e) {
Log.d(TAG, "ERROR: Falied to connect! (UnknownHostException)");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "ERROR: Falied to connect! (IOException)");
e.printStackTrace();
}
}
public void setProblemFromByteArray(byte[] data, boolean isFile){
sentProblem = new UserProblemRequest();
sentProblem.fileBArray = data.clone();
if (isFile){
sentProblem.requestType = Utils.requestType_IMAGE;
}
else {
sentProblem.requestType = Utils.RequestType_STRING;
}
}
public void sendProblem(){
ObjectOutputStream os;
try {
os = new ObjectOutputStream(socket.getOutputStream());
Log.d(TAG, "Sending file to server...");
os.writeObject(sentProblem);
os.flush();
os.close();
} catch (IOException e) {
Log.d(TAG, "ERROR: Falied to send file to server!");
e.printStackTrace();
}
Log.d(TAG, "SUCCESS: File sent to server!");
}
public void closeConnection(){
try {
socket.close();
} catch (IOException e) {
Log.d(TAG, "failed to close socket");
e.printStackTrace();
}
}
public Problem reciveResponseFromServer(){
ObjectInputStream ois;
try {
ois = new ObjectInputStream(socket.getInputStream());
responseProblem = (Problem) ois.readObject();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return responseProblem;
}
}
and i use this code to run it:
ConnectionToServer serverConnection = new ConnectionToServer();
serverConnection.connect();
serverConnection.setProblemFromByteArray(temp_data, true);
serverConnection.sendProblem();
Problem responseProblem = serverConnection.reciveResponseFromServer();
serverConnection.closeConnection();
any ideas?
Does closing the ObjectOutputStream() in sendProblem() wind up closing the underlying socket so that when you getInputStream() in reciveResponseFromServer() the socket is already closed?

Categories

Resources