Reputation: 1927
I'm having issues with my serverThreadProc() method. It throws a BindException whenever I run it. Thinking there might be an issue in the first two lines of the try block. Not sure what I'm doing wrong though.
private void serverThreadProc() {
while (!this.stopRequested) {
Socket sessionSocket = null;
try {
serverSocket = new ServerSocket(serverPort);
sessionSocket = this.serverSocket.accept();
InetAddress sourceAddress = sessionSocket.getInetAddress();
Log.i(LOG_TAG, "Accepted new incoming connection ... "
+ sourceAddress.toString());
if (clientMap.containsKey(sourceAddress)) {
ClientProcessor legacyProcessor = clientMap
.get(sourceAddress);
legacyProcessor.shutdown();
}
ClientProcessor processor = new ClientProcessor(sessionSocket);
processor.start();
clientMap.put(sourceAddress, processor);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int start() {
try {
this.serverSocket = new ServerSocket(0);
this.serverPort = this.serverSocket.getLocalPort();
Log.i(LOG_TAG, "Server " + serverName + " started at "
+ this.serverSocket.getLocalSocketAddress());
} catch (IOException ex) {
Log.i(LOG_TAG, ex.toString());
}
this.serverThread.start();
return this.serverPort;
}
public void stop() {
try {
stopRequested = true;
if (this.serverSocket) {
this.serverSocket.close();
}
} catch (IOException ex) {
Log.i(LOG_TAG, ex.toString());
}
Log.i(LOG_TAG, "The server " + this.serverName + " has been stopped.");
}
Here is the stack trace produced by the code:
01-11 05:51:53.472: I/ConnectActivity(2151): IP Address:0
01-11 05:51:53.492: I/ServiceServer(2151): Server Whooznear started at /0.0.0.0:35434
01-11 05:51:53.522: I/ConnectActivity(2151): Started at 35434
01-11 05:51:53.542: W/System.err(2151): java.net.BindException: Address already in use
01-11 05:51:53.542: W/System.err(2151): at org.apache.harmony.luni.platform.OSNetworkSystem.bind(Native Method)
01-11 05:51:53.542: W/System.err(2151): at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:275)
01-11 05:51:53.571: W/System.err(2151): at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:165)
01-11 05:51:53.571: W/System.err(2151): at java.net.ServerSocket.<init>(ServerSocket.java:123)
01-11 05:51:53.592: W/System.err(2151): at java.net.ServerSocket.<init>(ServerSocket.java:74)
01-11 05:51:53.592: W/System.err(2151): at com.whooznear.android.ServiceServer.serverThreadProc(ServiceServer.java:63)
01-11 05:51:53.592: W/System.err(2151): at com.whooznear.android.ServiceServer.access$0(ServiceServer.java:58)
01-11 05:51:53.611: W/System.err(2151): at com.whooznear.android.ServiceServer$1.run(ServiceServer.java:53)
Upvotes: 3
Views: 6917
Reputation: 5998
You create ServerSocket
in the procedure start()
, then you create new ServerSocket
on the same port in the procedure serverThreadProc()
. You have to close old one before you create new server socket. Or use another port.
Upvotes: 1
Reputation: 2161
Why are u using while
in the method serverThreadProc()
Make the program simple.I think your code is a bit messy.
You may refer to basic socket examples
This may help you to simplify ur code for the task
Upvotes: 0