How To Send Data Form Socket To Serversocket In Android?
I want to send a file from client to Server by using Socket Programming. I unable to transfer this file, client side is giving message OK, server get freeze at serverClient.accept
Solution 1:
Client
publicclassTCPServer {
//tcp port on local host portpublicstaticfinalintPORT=3100;
publicstaticvoidmain(String[] args)throws IOException {
ServerSocketserverSocket=null;
try {
//server socket, can also specify Host Address
serverSocket = newServerSocket(PORT);
//start listening on port
System.out.println("Listening for clients on port: " + PORT);
} catch (IOException e) {
System.err.println("Could not listen on port: " + PORT);
System.err.println(e.getMessage());
System.exit(-1);
}
//create new thread poolThreadPoolthreadPool=newThreadPool(2);
//call runnable method on thread pool
threadPool.runTask(startServer(serverSocket));
//join thread pool
threadPool.join();
//close server socket and destroy threadpool
serverSocket.close();
threadPool.destroy();
}
privatestatic Runnable startServer(final ServerSocket socket) {
returnnewRunnable() {
@Overridepublicvoidrun() {
//keep looping and looking for datawhile (true)
try {
//create new thread newTCPServerThread(socket.accept()).start();
} catch (IOException e) {
System.out.println("Client got disconnected!" + "\nListening for clients on port: " + PORT);
}
}
};
}
}
Server
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
publicclassTCPServerThreadextendsThread {
privateSocketsocket=null;
//constructorpublicTCPServerThread(Socket socket) {
this.socket = socket;
}
publicvoidrun() {
try {
//read data into buffered streamBufferedInputStreamstream=newBufferedInputStream(
socket.getInputStream());
//create music player object with buffered streamPlayerp=newPlayer(stream);
//start playing
p.play();
//close socket after done playing
socket.close();
} catch (IOException e) {
System.out.println("Client got disconnected!" + "\nListening for clients on port: " + TCPServer.PORT);
} catch (JavaLayerException e) {
System.out.println("Client got disconnected!" + "\nListening for clients on port: " + TCPServer.PORT);
}
}
}
Thread Pool
import java.util.LinkedList;
classThreadPoolextendsThreadGroup {
privateboolean isAlive;
private LinkedList<Runnable> taskQueue;
privateint threadID;
privatestaticint threadPoolID;
//constructorpublicThreadPool(int numThreads) {
super("ThreadPool-" + (threadPoolID++));
// Changes the daemon status of this thread group.
setDaemon(true);
isAlive = true;
taskQueue = newLinkedList<Runnable>();
for (inti=0; i < numThreads; i++) {
newPooledThread().start();
}
}
publicsynchronizedvoidrunTask(Runnable task) {
if (!isAlive) {
thrownewIllegalStateException();
}
if (task != null) {
taskQueue.add(task);
notify();
}
}
protectedsynchronized Runnable getTask()throws InterruptedException {
while (taskQueue.size() == 0) {
if (!isAlive) {
returnnull;
}
wait();
}
return (Runnable) taskQueue.removeFirst();
}
publicsynchronizedvoidclose() {
if (isAlive) {
isAlive = false;
taskQueue.clear();
interrupt();
}
}
publicvoidjoin() {
// notify all waiting threads that this ThreadPool is no// longer alivesynchronized (this) {
isAlive = false;
notifyAll();
}
// wait for all threads to finish
Thread[] threads = newThread[activeCount()];
intcount= enumerate(threads);
for (inti=0; i < count; i++) {
try {
threads[i].join();
} catch (InterruptedException ex) {
}
}
}
privateclassPooledThreadextendsThread {
publicPooledThread() {
super(ThreadPool.this, "PooledThread-" + (threadID++));
}
publicvoidrun() {
while (!isInterrupted()) {
// get a task to runRunnabletask=null;
try {
task = getTask();
} catch (InterruptedException ex) {
}
// if getTask() returned null or was interrupted,// close this thread by returning.if (task == null) {
return;
}
// run the task, and eat any exceptions it throwstry {
task.run();
} catch (Throwable t) {
uncaughtException(this, t);
}
}
}
}
}
Post a Comment for "How To Send Data Form Socket To Serversocket In Android?"