I am very new to Android. Our lan is working in class C IP (192.168.1.x
series). I need to establish socket connection between android
emulator(Server) and java eclipse (eclipse-java-juno-SR1) which is acting
like a client. As I got a information from "http://developer.android.com"
website every Android emulator having 10.0.2.15 (Class A IP) by default.
But our LAN is working in Class C.
"PC-1" which is using for Android Emulator is having 192.168.1.50 IP, and
the another one "PC-2" which is using for Eclipse-Java is having
192.168.1.54. This is the setup which I have.
My work is, When I send a string command (through socket) from Android
emulator(running on PC-1 192.168.1.50) to Java program(running on PC-2
192.168.1.54) then that Java program should send continuous jpg photo to
Android emulator through socket.
And I have added Internet permission in manifest:
<uses-permission android:name="android.permission.INTERNET"/>
I tried lot but when I try to work with sockets installed apk application
is closing by this error: "Unfortunately, Activity name> is closing"
I have attached the project code, please find it.
Please help me out in this... Thanks in advance :)
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
// This is the program running on the android emulator
package com.example.networkdemo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public String s = "This data coming from client ";
EditText textOut;
TextView textIn;
Button buttonSend;
//=====================================================================
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOut = (EditText)findViewById(R.id.textout);
buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
//=========================================
buttonSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
try {
Socket clientSocket = new Socket("192.168.1.54",6666);
OutputStream outs = clientSocket.getOutputStream();
byte buf[] = s.getBytes();
outs.write(buf,0,buf.length);
outs.flush();
outs.close();
clientSocket.close();
}
catch (IOException e) {
System.exit(-1);
}
} // End of Onclick
});
} // End of onCreate
} // End of Activity
// Java program running on eclipse juno
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class CharServerSocket {
static boolean x=true;
public static void main(String[] args) throws Exception {
int listenPort = 6666;
String recmsg = null;
ServerSocket sersocket= new ServerSocket(listenPort);
try {
System.out.println("Waiting for data....");
Socket tempSocket = null;
while(x)
{
tempSocket = sersocket.accept();
InputStream inputstm = tempSocket.getInputStream();
recmsg = convertStreamToString(inputstm);
System.out.println(recmsg);
}
System.out.println("*** Data receiveing over ***");
tempSocket.close();
sersocket.close();
}
catch (IOException e) {
System.out.println("No client found");
System.exit(-1);
}
}
// Function to Convert InputStream to String
public static String convertStreamToString(InputStream is) throws
Exception {
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "");
}
is.close();
x=false;
return sb.toString();
}
}