I'm currently working on an android chat application. First, I have created the server and then, the android client. But for some reason, the client cannot connect to the server. Nevertheless, I have specified the same port for both client and server.
Could you help me, please ?
Here is the code of my server :
public class ChatServer {
private static int port = 8080;
public static void main (String[] args) throws IOException {
ServerSocket server = new ServerSocket(port); /* start listening on the port */
System.out.println( "Listening on "+ server );
Socket client = null;
while(true) {
try {
client = server.accept();
System.out.println( "Connection from " + client );
/* start a new thread to handle this client */
Thread t = new Thread(new ClientConnect(client));
t.start();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
server.close();
}
}
}
}
and here is the client :
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText nickname = (EditText)findViewById(R.id.nicknameField);
EditText password = (EditText)findViewById(R.id.passwordField);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
new Downloader(this).execute(8080);
}
and the Asynctask class
public class Downloader extends AsyncTask<Integer, Void, Void> {
private WeakReference<MainActivity> activity;
private int port;
public Downloader(MainActivity act){
super();
activity = new WeakReference<MainActivity>(act);
}
protected Void doInBackground(Integer... params) {
port = params[0];
try {
Socket clientSocket = new Socket("localhost", port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
When I used the debugger on eclipse, it goes through the line
Socket clientSocket = new Socket("localhost", port);
but after that it goes to the IOException.
Thanks for your help !