I'm trying to connect my 'Galaxy S' to my Panasonic PLC(or even other PC itself) but I always get an SocketTimeoutException. My android version is 2.3.3:
IP-PLC: 192.168.2.99
IP-GalaxyS: I don't know how to check it
Mask of the network: 255.255.255.0
The PLC is connected directly with the wi-fi router, and I connect my cell phone to this network.
package com.example.communication;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import android.util.Log;
public class TryConnection implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("192.168.2.99");
Socket socket = new Socket(serverAddr, 9094);
Log.e("SUCESS", "SUCESS");
} catch(SocketTimeoutException e) {
Log.e("SOCKET TIMEOUT", "SOCKET TIMEOUT", e);
} catch (IOException e) {
Log.e("CONNECTION ", " ERROR", e);
}
}
and my MainActivity Class:
package com.example.communication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendTag(View view) {
TryConnection tryconnection = new TryConnection();
Thread thread = new Thread(tryconnection);
thread.start();
}
}
Do you have any idea for why is it happening ?
It happened with me also, restarted eclipse to resolve the issue.
Related
I have java code that sends strings via ip to a python script. The code works perfectly with the emulator but when I successfully install the app via usb to my phone it does not work. Here is the code:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
public String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn_python = findViewById(R.id.python);
final Button btn_movie = findViewById(R.id.movie);
final Button btn_hw = findViewById(R.id.homework);
btn_python.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
send py = new send();
message = "python";
Log.i("Button", "Button works");
System.out.println("whatever");
py.execute();
}
});
btn_movie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
send mov = new send();
message = "movie";
mov.execute();
}
});
btn_hw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
send hw = new send();
message = "homework";
hw.execute();
}
});
}
class send extends AsyncTask<Void,Void,Void>{
Socket s;
PrintWriter pw;
#Override
protected Void doInBackground(Void...params) {
System.out.println("whatevernumbertwo");
try {
System.out.println("whatevernumberthree");
s = new Socket("ip address", 7800);
Log.i("Socket", "connects to socket");
pw = new PrintWriter(s.getOutputStream());
Log.i("output stream", "Output stream works");
pw.write(message);
Log.i("write", "Write works");
pw.flush();
Log.i("flush", "Flush works");
pw.close();
s.close();
} catch (UnknownHostException e) {
System.out.println("Fail");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Fail");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
As I mentioned this works on the emulator but not on the actual device. The proper permissions have also been given. What am I doing wrong? Thanks in advance.
After much digging around, it turned out to be the server's firewall all along. That explains why (apparently) no exception was thrown, and why the code didn't seem to execute; it was executing, it was just getting stuck inside Socket() (during the connect).
Surely Socket() is, in fact, throwing an IOException; it probably just takes a while.
The code works on the emulator because, as it is operating on the same machine, it is behind the firewall.
package com.example.tristan.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private Socket client;
int serverPort = 8888;
String serverIP = "192.168.1.6";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fctClient();
}
public void fctClient() {
try {
Toast toast = Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT);
InetAddress addr = InetAddress.getByName(serverIP);
SocketAddress socketAddress = new InetSocketAddress(addr,serverPort);
//SocketAddress socketAddress = new InetSocketAddress(serverIP, serverPort);
client = new Socket();
toast.show();
client.connect(socketAddress);
toast.show();
toast.show();`enter code here`
client.close();
} catch (UnknownHostException e) {
Toast toast = Toast.makeText(getApplicationContext(), "KO1", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
} catch (IOException e) {
Toast toast = Toast.makeText(getApplicationContext(), "KO2", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
}
}
}
Hello,
I am trying to connect a client on Android on a Python server using a socket. The Python server is working well but the Android code fails to connect the server passing through a IOException.
Is anyone able to tell me my mistake? The code fails on line "client.connect(socketAddress);". This let me think that the wrong command is used to generate socketAddress.
I found the following link which met the same problem but his solution doesn't work for me.
(Java/Android) Client-side Socket throwing IOException
Thanking you in advance,
TL
You have a NetworkOnMainThreadExeption clearly visible in the logcat. All internet code should be executed in a thread or AsyncTask.
If you modify your code then remove all Toasts as they cannot be called in a thread or the doInBackground of an AsyncTask.
I am programming an android app to receive a packet that is being broadcast on the broadcast address of a network (This has been tested and the packet does get broadcast and gets received in the "UDP Sender/Receiver" application as well.) I cannot get my app to pick it up and tell me that it exists. The devices are on the same network and the code for the sending device is working and proprietary. Here is the basic DatagramSocket code for the app.
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
final TextView txt = (TextView)findViewById(R.id.txt1);
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
}
if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
}}
Any help is appreciated, thank you!!
Updated code:
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
WifiManager wifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
final TextView txt = (TextView) findViewById(R.id.txt1);
lock.acquire();
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
//while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
//}
//if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
//}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
lock.release();
}
}
In your answer you mention that the test packets you are sending are being broadcast. Have you tried to see if you can receive the packet if you send it directly to the IP address of your device instead of broadcasting it? There is a chance that your socket is working fine but just not receiving broadcast packets. By default, the Android Wi-Fi stack filters out multicast packets in order to conserve power. If you can receive a packet sent directly to your IP address then that means all you have to do is enable the receiving of multicast packets by acquiring a MulticastLock, which you can find more information on here: Android device not receiving multicast package
If you are still unable to receive a direct packet then there is probably another issue at play, but I would check this first.
i am new by android.I have started work with a simple chat app. I wrote a short code by UDP socket one for client and one for server.Now i have some issues at connectivity android real device which client app installed on it , and emulator device which server app run on it.
Client app is a simple code which must set IP for connection to emulator and also has a EditText for sending a message.(I at this point set 10.0.2.2 or 10.0.2.15. But my app could not send a message from my real device to emulator! What is goes wrong? is needed to more settings?)
Server app has only a textView for getting and showing recived message.
here is Client code:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ClienttextchatActivity extends Activity
{
private String udpMsg=null;
Handler hand=new Handler();
EditText edtSetIp=null , edtText=null;
Button btnSetIp=null , btnSend=null;
static String ip=null;
static final String LOG_TAG = "UdpStream";
static final int PORT = 8888;
static final int BUF_SIZE=4096;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtText=(EditText)findViewById(R.id.edtText);
edtSetIp=(EditText)findViewById(R.id.edtSetIp);
btnSetIp=(Button)findViewById(R.id.btnSetIp);
btnSend=(Button)findViewById(R.id.btnSend);
btnSetIp.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ip=edtSetIp.getText().toString();
}
});
btnSend.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Thread mythread=new Thread(new th1() );
mythread.start();
}
});
}
//#############################################
public class th1 implements Runnable
{
public void run()
{
udpMsg =edtText.getText().toString();
Log.d(LOG_TAG,udpMsg);
DatagramSocket ds = null;
try
{
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName(ip);
Log.d(LOG_TAG,"address server address created.");
DatagramPacket dp;
dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), serverAddr, PORT);
ds.send(dp);
Log.d(LOG_TAG,"packet send.");
}
catch (SocketException e)
{
e.printStackTrace();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (ds != null)
{
ds.close();
}
}
}//end run
}//end class th
}
Make sure emulator is started, then in ADB shell, type this command, it will show ip address of emulator.
adb shell
ifconfig etho
I am creating a socket connection in android eclipse for receiving data first i need to test the connection but the page is not opening on my app, i use a listactivity for my classes.
I want to receive data from my pc(hyperterminal), i have connected a hardware device to my computer which behaves like a router and i want to connect my android phone to the router via Wifi and receive data on my phone as i type on the hyperterminal.
package com.exa.bullseye;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;
import java.io.OutputStream;
public class WifiReceive extends Activity {
EditText ip;
EditText s_port;
Button connect;
public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.wifireceive);
ip = ((EditText)findViewById(R.id.ip));
s_port = ((EditText)findViewById(R.id.port));
connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
try {
String address = ip.getText().toString();
InetAddress addr = InetAddress.getByName(address);
int port = Integer.parseInt(s_port.getText().toString());
// Creates a stream socket and connects it to the specified port
// number at the specified IP address. Blocks until the connection succeeds.
Socket socket = new Socket(addr, port);
Toast.makeText(getApplicationContext(), "Socket Connected",
Toast.LENGTH_LONG).show();
socket.close();
}
catch (UnknownHostException e) {
Toast.makeText(getApplicationContext(), "Host not found",
Toast.LENGTH_LONG).show();
}
catch (IOException ioe) {
Toast.makeText(getApplicationContext(), "I/O Exception",
Toast.LENGTH_LONG).show();
}
}
});
}
}