Hi guys i'm trying to make an app that connect to my arduino device using a datagramsocket. When i launch the app it should write in my edittext ip address of my arduino but it doesn't appear. I think the problem is that the UdcClientServer class is launched only one time at startup but it should be executed in realtime.
Here the main activity code:
public class MainActivity extends Activity {
private EditText textIpScheda=null;
private EditText textUdpPort=null;
private UdpClientServer cu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIpScheda = (EditText) findViewById(R.id.textIpScheda);
textUdpPort = (EditText) findViewById(R.id.textUdpPort);
try {
cu = new UdpClientServer(this);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public EditText getTextIpScheda(){
return textIpScheda;
}
public EditText getTextUdpPort() {
return textUdpPort;
}
}
Here the UdpClientServer class:
public class UdpClientServer {
public static String sReceive;
private static DatagramSocket dSocket;
private Socket socket;
private String stringa;
int receiveBufferSize = 1024;
int portUdp = 0;
final String PINGACMD = "AT*PINGA001";
InetAddress ipScheda;
byte[] receiveData = new byte[receiveBufferSize];
private MainActivity gui;
public UdpClientServer(MainActivity gui) throws SocketException, IOException {
this.gui = gui;
portUdp = 5200;
dSocket = new DatagramSocket(portUdp);
}
public void run(){
while (true) {
Arrays.fill(receiveData, (byte) 0);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
dSocket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
ipScheda = receivePacket.getAddress();
int port = receivePacket.getPort();
gui.getTextUdpPort().setText("" + port);
gui.getTextIpScheda().setText(ipScheda.getHostAddress());
sReceive = new String(receivePacket.getData());
this.sendCommand(PINGACMD);
}
}
public void sendCommand(String outSentence){
byte[] sendData = outSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipScheda, portUdp);
try {
dSocket.send(sendPacket);
Thread.sleep(100);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
}
}
}
thanks everybody
I want to create an app in which server and client communicate each other. here the code...
Server Side:
public class MainActivity extends ActionBarActivity {
private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
Socket clientSocket;
private TextView text;
EditText edit;
Button b;
public static final int SERVERPORT = 6000;
String TimeStamp;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("inside onCreate");
text = (TextView) findViewById(R.id.textView1);
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
TimeStamp = new java.util.Date().toString();
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try
{
EditText et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
text.setText(text.getText().toString()+"Me: "+ str +" On "+TimeStamp+ "\n");
/*
BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());
OutputStreamWriter osw= new OutputStreamWriter(bos,"US-ASCII");
osw.write(str+" "+TimeStamp);
osw.flush();
*/
if(clientSocket==null)
Toast.makeText(MainActivity.this, "null socket", Toast.LENGTH_SHORT).show();
else
{
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream())),
true);
out.println(str);
out.flush();
et.setText("");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
#Override
protected void onStop()
{
super.onStop();
try
{
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class ServerThread implements Runnable
{
public void run()
{
Socket socket = null;
try
{
serverSocket = new ServerSocket(SERVERPORT);
serverSocket.setReuseAddress(true);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted())
{
try
{
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable
{
//private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket aclientSocket)
{
//this.aclientSocket = clientSocket;
clientSocket=aclientSocket;
try {
//this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable
{
private String msg;
public updateUIThread(String str)
{
this.msg = str;
}
#Override
public void run()
{
text.setText(text.getText().toString()+"Client: "+ msg +" On "+TimeStamp+ "\n");
}
}
}
Messages send from client correctly receives to server, Now messages from Server writes to socket but cant read at client.
here the client side:
public class MainActivity extends ActionBarActivity {
private Socket socket;
Handler updateConversationHandler;
private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";
TextView text;
Button c;
String TimeStamp;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
TimeStamp = new java.util.Date().toString();
c=(Button)findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try
{
EditText et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
text.setText(text.getText().toString()+"Me: "+ str +"On "+TimeStamp+ "\n");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
out.flush();
et.setText("");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
class ClientThread implements Runnable
{
#Override
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class CommunicationThread implements Runnable
{
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
String read = input.readLine();
Log.d("msg","readString"+read);
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable
{
private String msg;
public updateUIThread(String str)
{
this.msg = str;
System.out.println("message in updateThread "+msg);
}
#Override
public void run()
{
System.out.println("message "+msg);
text.setText(text.getText().toString()+"Server: "+ msg +"On "+TimeStamp+ "\n");
}
}
}
The logcat shows:
D/gralloc_goldfish(1777): Emulator without GPU emulation detected.
W/System.err(1777): java.lang.NullPointerException
W/System.err(1777): at com.example.socketservereg.MainActivity$1.onClick(MainActivity.java:67)
W/System.err(1777): at android.view.View.performClick(View.java:4240)
W/System.err(1777): at android.view.View$PerformClick.run(View.java:17721)
W/System.err(1777): at android.os.Handler.handleCallback(Handler.java:730)
W/System.err(1777): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(1777): at android.os.Looper.loop(Looper.java:137)
W/System.err(1777): at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err(1777): at java.lang.reflect.Method.invokeNative(Native Method)
06-17
W/System.err(1777): at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err(1777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err(1777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
I am a beginner and any help will be appreciated...
In the server you use
socket.getOutputStream()
but it should be
clientSocket.getOutputStream()
I wish to build a simple peer-peer app using Sockets. A accepts a number and sends it to B. Then B returns the square of the number back to A.
A sends to B on port 6000 and B sends back to A on port 8000. So both the machines act as clients when they must send, and as servers when they must receive.
But isn't it true that a server should be started BEFORE a client ? So, whats the logical solution to making this app ?
Here is the Server's code
package com.javacodegeeks.android.androidsocketserver;
.....
public class Server extends Activity {
private ServerSocket serverSocket;
private Socket clientSock;
Handler updateConversationHandler;
Thread serverThread = null;
Thread clientThread = null;
private TextView text;
public static final int SERVERPORT = 5000;
public static final int CLIENTPORT = 8000;
private static final String CLIENT_IP = "10.0.2.2";
protected IRemote mService;
private boolean bound = false;
Intent it;
private boolean test = false;
private ServiceConnection mServiceConnection = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mService = null;
Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
Log.d("IRemote", "Binding - Service disconnected");
}
#Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// TODO Auto-generated method stub
mService = IRemote.Stub.asInterface((IBinder) service);
Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
Log.d("IRemote", "Binding is done - Service connected");
}
};
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text2);
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
it = new Intent();
it.setAction("com.example.mynewclient.RemoteService");
//it= new Intent(this,IRemote.class);
//TEMP
bound = getApplicationContext().bindService(it, mServiceConnection, Context.BIND_AUTO_CREATE);
Log.d("SERVER", bound ? "Binding is done - Service connected" : "Binding Failed");
this.clientThread = new Thread(new ClientThread());
this.clientThread.start();
}
#Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(View view) {
try {
// EditText et = findViewById(R.id.text);
String str = text.getText().toString();//et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(clientSock.getOutputStream())),
true);
out.println(345+"\n");
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
} catch (UnknownHostException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception 1", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception 2", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception 3", Toast.LENGTH_LONG).show();
}
}
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
BufferedWriter writer;
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
//writer = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
//writer.write("Server Echos to client");
//writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable {
private String msg;
public updateUIThread(String str) {
this.msg = str;
}
#Override
public void run(){
String message;
BufferedReader input;
if(mService == null)
{
Log.d("TAG 0", "Inside mservice==null");
Intent it = new Intent();
it.setAction("com.example.mynewclient.RemoteService");
//it.setClassName( "com.example.myclient","com.example.myclient.IRemoteService" );
//binding to remote service
// bindService(it, mServiceConnection, Service.BIND_AUTO_CREATE);
}
int f = Integer.parseInt(msg);
try{
//Toast.makeText(getApplicationContext(), "Result -> Add ->"+mIRemoteService.getSum(3,4), Toast.LENGTH_SHORT).show();
message = Integer.toString(mService.getSum(f,f));
text.setText(text.getText().toString()+"The sum is: "+ message + "\n");
}
catch(Exception e)
{
//setContentView(R.layout.activity_main);
System.out.println(e.toString());
Log.e("EXCEPTION 3", e.toString());
f = f+f;
message = "Not Successful";
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
//TEMP Commented
/*#Override
public void run() {
text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}*/
}
class ClientThread implements Runnable {
#Override
public void run() {
BufferedReader input;
try {
Log.d("SERVER", "Client - thread 1");
InetAddress serverAddr = InetAddress.getByName(CLIENT_IP);
clientSock = new Socket(serverAddr, SERVERPORT);
//input = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
/////////////////////
Log.d("SERVER", "Client - thread 2");
//TEMP Commented
//input = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
input = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
Log.d("SERVER", "Client - thread 3");
} catch (UnknownHostException e1) {
e1.printStackTrace();
Toast.makeText(getApplicationContext(), "exception 1", Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
e1.printStackTrace();
Toast.makeText(getApplicationContext(), "exception 2", Toast.LENGTH_SHORT).show();
}
}
}
}
Here's the client:
package com.javacodegeeks.android.androidsocketclient;
....
public class Client extends Activity {
private Socket thisSocket;
private ServerSocket serverSocket;
private TextView text;
private static final int SERVERPORT = 6000;
private static final String SERVER_IP = "10.0.2.2";
private static final int CLIENTPORT = 8000;
Handler updateConversationHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
new Thread(new ServerThread()).start();
}
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(thisSocket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
Log.e("SERVER ERROR",e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("SERVER ERROR",e.toString());
} catch (Exception e) {
e.printStackTrace();
Log.e("SERVER ERROR",e.toString());
}
}
class ClientThread implements Runnable {
#Override
public void run() {
BufferedReader input;
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
thisSocket = new Socket(serverAddr, SERVERPORT);
input = new BufferedReader(new InputStreamReader(thisSocket.getInputStream()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//TEMP COMMENTED
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(CLIENTPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
Log.d("CLIENT TAG", "Listening to server");
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
Log.d("TAG Client 1", "IOException");
}
catch (Exception e)
{
Log.d("TAG Client 2", e.toString());
}
}
Toast.makeText(getApplicationContext(), "Client interrupted", Toast.LENGTH_LONG).show();
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
Log.d("TAG Client 4", "Input from server"+input.toString());
} catch (IOException e) {
Log.d("TAG Client 5", "Input from server");
e.printStackTrace();
}
catch (Exception e)
{
Log.d("TAG Client 6", "Input from server");
}
}
public void run() {
BufferedWriter writer;
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
//writer = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
//writer.write("Server Echos to client");
//writer.close();
Log.d("TAG Client 7", "Input from server");
} catch (IOException e) {
e.printStackTrace();
Log.d("TAG Client 8", e.toString());
}
}
}
}
class updateUIThread implements Runnable {
private String msg;
public updateUIThread(String str) {
this.msg = str;
}
#Override
public void run() {
//text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
}
The distinction between client and server is not made by who is sending or receiving data. In many cases, both server and client do both. The point of peer-to-peer is that there is no server/client relationship.
That being said, both your applications should run and start listening before you send anything, unless it's always A that sends first, in which case you have a client/server relationship.
Hope that helps.
Edit: Should probably mention this: You usually use multiple threads for networking, to keep listening on at least one port while the rest of the program executes.
There should be a long-connection Server for each client(like A,B,C,D.....),A send msg to Server and forward to D.
I use these codes for communicating between devices. If i close or kill the client app, the server app gets thousands of useless data. The textView in the server side is full of this: Client says: null. If i close the client twice, the server stops with StackOverFlowError. How can i make the code to dont send this null values when the app stops? Or can i filter the server side to do nothing when getting this data?
Client:
public class Client extends Activity {
private Socket socket;
private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new ClientThread()).start();
}
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable {
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
Server:
public class Server extends Activity {
private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
private TextView text;
public static final int SERVERPORT = 1599;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text2);
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
#Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable {
private String msg;
public updateUIThread(String str) {
this.msg = str;
}
#Override
public void run() {
text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}
}
}
I think you should use a delimiter character, to tell the Server your Client it's about to die. Add that character of code in onPause method of your Android Activity/Fragment.
Then in your Server, just get the String or byte and compare it against your Delimiter String/Character and stop the Server listening for the Connection.
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
The problem is here. You aren't checking the result of readLine() for null. If it returns null, the peer has closed the connection and you should do likewise, and exit this loop. You can probably get rid of the isInterrupted() test as well.
Also, if you get any IOException other that a read timeout when reading from a socket, the connection is dead and you must close the socket and exit your loop.
I read a thread on the same topic : connecting 2 Emulator Instances
Server Side Code listening on port 6000:
public class NewServerActivity extends Activity {
ServerSocket ss = null;
String mClientMsg = "";
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
public static final int SERVERPORT = 6000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.TextView01);
tv.setText("Nothing from client yet");
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);
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();
}
}
}
}
}
Client side code :
public class NewClientActivity extends Activity {
private Button bt;
private TextView tv;
private Socket socket;
private String serverIpAddress = "10.0.2.2";
// AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
// PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
// PORT 6000
private static final int REDIRECTED_SERVERPORT = 5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.myButton);
tv = (TextView) findViewById(R.id.myTextView);
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
Log.d("Client", "Client sent message");
} catch (UnknownHostException e) {
tv.setText("Error1");
e.printStackTrace();
} catch (IOException e) {
tv.setText("Error2");
e.printStackTrace();
} catch (Exception e) {
tv.setText("Error3");
e.printStackTrace();
}
}
});
}
}
I applied server code on one emulator and client code on another emulator.
Redirected the ports :
telnet localhost 5554
redir add tcp:5000:6000
I ran the server on emulator 5554 and the client on 5556.
My client side is working fine but unfortunately, on starting the server app the emulator shows a message Unfortunately APPLICATION was stopped. And eclipse shows a java.lang.NullPointerException at "s = ss.accept();" line.
Is it somehow related to the configuration of my AVD.
Post your code and the stack trace.
From the one line you posted it sounds like 'ss' is null and you're trying to call accept() on a null.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mOnClick(View v) {
switch (v.getId()) {
case R.id.myButton:
new Thread(new Runnable() {
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
Log.d("Client", "Client sent message");
out.close();
socket.close();
} catch (UnknownHostException e) {
//tv.setText("Error1");
e.printStackTrace();
} catch (IOException e) {
//tv.setText("Error2");
e.printStackTrace();
} catch (Exception e) {
//tv.setText("Error3");
e.printStackTrace();
}
}
}).start();
break;
}
}
need xml change
android:onClick="mOnClick"
<Button
android:id="#+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mOnClick"
android:text="start" />
it's because try to run connect() method in main Thread