I have a simple server running on my pc and a simple client on my htc desire running android 2.2.
The server and client both use the same port. I hard code the servers ip into the clients code. When I try to connect to the running server via the client on android the client throws this exception:
IOException ...: java.net.SocketException: The operation timed out.
Here are parts of the client and server codes:
client code
InetAddress addr;
Socket socket = null;
byte [] ipAddress = new byte[] {(byte)82,(byte)168,(byte)175,(byte)141};
try{
addr = InetAddress.getByAddress(ipAddress);
socket = new Socket(addr, 1234);
System.out.println("socket = "+socket);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
out.println("hello");
socket.close();
} catch (UnknownHostException e) {
System.out.println("UnknownHostException ...: "+e);
} catch (IOException e) {
System.out.println("IOException ...: "+e);
}
,
server code
ServerSocket ss = new ServerSocket(PORT);
System.out.println("Started:"+ss);
try{
//block until a connection occures
Socket socket = ss.accept();
try{
System.out.println("Conncetion accepted:"+socket);
InputStream IS = socket.getInputStream();
InputStreamReader ISR = new InputStreamReader(IS);
BufferedReader in = new BufferedReader(ISR);
OutputStream OS = socket.getOutputStream();
OutputStreamWriter OSR= new OutputStreamWriter(OS);
BufferedWriter BW = new BufferedWriter(OSR);
//output automatically flushed by PrintWriter
PrintWriter out = new PrintWriter(BW,true);
while(true){
String str = in.readLine();
System.out.println("Client: "+str);
out.println("hi");
}
}finally{
System.out.println("Closing...");
socket.close();
}
}finally{
ss.close();
}
This code is working using wifi but not through 3g.
Any help is welcome...and thank you in advance.