Reputation: 1462
i am implementing chat application where server is java based and client in android. my server code is written in java language using socket programming.when i connect my android phone(internet is on) with laptop and start both server and client then it working fine. in my client app i have to enter ip address of server machine which is like this 192.168... when client send massage to server then server return response to client. it is fine.
but when i run client from other android phone which is not in my home network(suppose my friend is in his home and try to connect with java server(in my home) over internet) .then server did not show connection establish. i also try putting my public ip address from google into client app when start but still no response.
server code..
public class SimpleChatServer {
static int port_num =4444;
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket clientSocket = null;
try {
serverSocket = new ServerSocket(port_num);
System.out.println("Server started. Listening to the port 4444. Waitng for the client.");
clientSocket = serverSocket.accept();
System.out.println("Client connected on port 4444.");
port_num++;
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
e.printStackTrace();
return;
}
please any buddy can tell me what should i do ? how to connect client to server ? and what is ip address from ipconfig and what is my ip public address from google ?
this is my android client code.
public class SimpleClientServerChatActivity extends Activity {
private EditText textField,ipaddrs;
private Button button, start;
private TextView textView;
private Socket client;
private PrintWriter printwriter;
private BufferedReader bufferedReader;
//Following is the IP address of the chat server. You can change this IP address according to your configuration.
// I have localhost IP address for Android emulator.
private String CHAT_SERVER_IP = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_client_server_chat);
textField = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView1);
ipaddrs = (EditText) findViewById(R.id.ipaddrs);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
###############// there client enter server public ip address via mobile app
CHAT_SERVER_IP = String.valueOf(ipaddrs.getText());
ChatOperator chatOperator = new ChatOperator();
chatOperator.execute();
}
});
}
/**
* This AsyncTask create the connection with the server and initialize the
* chat senders and receivers.
*/
private class ChatOperator extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
client = new Socket(CHAT_SERVER_IP, 4444); // Creating the server socket.
if (client != null) {
printwriter = new PrintWriter(client.getOutputStream(), true);
InputStreamReader inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
} else {
System.out.println("Server has not bean started on port 4444.");
}
} catch (UnknownHostException e) {
System.out.println("Faild to connect server " + CHAT_SERVER_IP);
e.printStackTrace();
} catch (IOException e) {
System.out.println("Faild to connect server " + CHAT_SERVER_IP);
e.printStackTrace();
}
return null;
}
/**
* Following method is executed at the end of doInBackground method.
*/
@Override
protected void onPostExecute(Void result) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Sender messageSender = new Sender(); // Initialize chat sender AsyncTask.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
messageSender.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
messageSender.execute();
}
}
});
Receiver receiver = new Receiver(); // Initialize chat receiver AsyncTask.
receiver.execute();
}
}
/**
* This AsyncTask continuously reads the input buffer and show the chat
* message if a message is availble.
*/
private class Receiver extends AsyncTask<Void, Void, Void> {
private String message;
@Override
protected Void doInBackground(Void... params) {
while (true) {
try {
if (bufferedReader.ready()) {
message = bufferedReader.readLine();
publishProgress(null);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
@Override
protected void onProgressUpdate(Void... values) {
textView.append("Server: " + message + "\n");
}
}
/**
* This AsyncTask sends the chat message through the output stream.
*/
private class Sender extends AsyncTask<Void, Void, Void> {
private String message;
@Override
protected Void doInBackground(Void... params) {
message = textField.getText().toString();
printwriter.write(message + "\n");
printwriter.flush();
return null;
}
@Override
protected void onPostExecute(Void result) {
textField.setText(""); // Clear the chat box
textView.append("Client: " + message + "\n");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_simple_client_server_chat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
there is a problem when i enter server public ip address mobile application became crash. why this is happening. if i put there local ip address it is not going to crash.
Upvotes: 2
Views: 3813
Reputation: 11
You are using your mobile phone to provide internet to your server(which is your laptop in your case), so here your mobile is acting as a router, and your mobile has provided local ip addresses to devices that connects to it. So what is happening is that when you enter your laptop's (server) ip address in client application for starting connection and your computer doesn't have any ip address in public(it has an ip address which is assigned by your mobile but it's not publically available and it's a local ip address which generally starts with 192.168........) so client app is unable to detect your server and servers which are connected to routers same is applied in their case.
So solution to this is, you enter your mobile's (which is providing internet to your server) ip address in your client application, and with any port forwarder application you do port forwarding to your server's(which is your laptop) ip address(and it's local ip address assigned by your phone/router to your laptop) and port(which you are using for communication with client), and there are many port forwader applications available on Google play store you can use anyone of them. So steps to start connection with client and server in your case are:
1: install and start any port forwader application in your phone from your mobiles store. 2: unable port forwarding (for every request which comes to your mobile's ip address) to your server's ip address (which is assigned by your mobile to your server). 3: Than enter your mobile's ip address in your client application.
And it will get connected to your server...
Upvotes: 2
Reputation: 3904
You have to run your jar file on server with public IP. Address on your computer is your local IP. What's more, sometimes WiFi access point can block port 4444 for example in public place (McDonald's, Edurom, KFC, etc.). You have to remember this.
So, configure your own sever with public IP, start your server with
java -jar server.jar
and then test it.
For example it is list of available ports in Edurom (University WiFi). Like I sad, port 4444 is blocked here.
Upvotes: 3