i try creating client-server app using socket.
i already succeed doing that with the AVD and run both server and client on my pc machine.
but when i try make it work in same Wifi network on my device, the app just crash.
yes, i’m using seperate thread for the connection
and already added the use of Internet to the manifest.
here is some code…
the client thread:
package com.mainlauncher;
import java.io.*;
import java.net.*;
public class ConnectionThread extends Thread {
private static final int SERVERPORT = 7777;
private static final String SERVERADDRESS = "My-PC";
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
@Override
public void run() {
super.run();
open();
close();
}
void open(){
try{
socket = new Socket(SERVERADDRESS,SERVERPORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
}
void close(){
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
if(socket!=null)
socket.close();
}
catch (IOException e) {}
socket=null;
}
}
the Server side main:
import java.io.IOException;
import java.net.*;
public class Main {
public static void main(String[] args) {
int port = 7777;
new Main().handleClients(port);
}
private void handleClients(int port) {
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(port);
System.out.println("Server is ready...");
for(int i=1; ; i++){
Socket socket = serverSocket.accept();
ServerThread thread = new ServerThread(i,socket);
System.out.println(i + " Connected");
thread.run();
}
}
catch (Exception e){
System.err.println(e.getMessage());
}
finally{
if(serverSocket != null){
try{ serverSocket.close(); }
catch(IOException x){}
}
}
}
}
and the ServerThread:
import java.io.*;
import java.net.*;
public class ServerThread extends Thread {
private int serverIndex;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
public ServerThread (int serverIndex, Socket socket){
this.serverIndex = serverIndex;
this.socket = socket;
}
@Override
public void run() {
super.run();
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println(serverIndex + " Disconnected");
}
finally{
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {}
}
}
}
i tried looking for answers here \ google etc…
nothing helped.
there is no firewall or anything to block the incoming connection on my pc.
any ideas anyone?
thanks,
Lioz
This won’t immediately solve your problem, but you have a couple of bugs that are causing your program to throw away the evidence of the the real problem.
1) This simply squashed the exception, throwing away all evidence that it ever happened.
2) This is a bit better, but it only prints out the exception message … not the stack trace.
The other problem with 2) is that it catches ALL exceptions … not just IOExceptions. That includes unchecked exceptions like
NullPointerException,SecurityExceptionand so on.Exceptions should be diagnosed like this:
or logged.
Finally, the way that you are handling requests is wrong:
The mistake is in the last statement. What this actually does is to simply call the
threadobject’srun()method … in the current thread. To run therun()method in a different thread you need to call thestartmethod. Like this: