File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes socket and thread,puzzel me days,who can help me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket and thread,puzzel me days,who can help me?" Watch "socket and thread,puzzel me days,who can help me?" New topic
Author

socket and thread,puzzel me days,who can help me?

wenjunxiang
Greenhorn

Joined: Jul 08, 2002
Posts: 4
this is my code to accept client upload directorys or files!
windows ok,
under linux: when i start again address is already use ,how can i do?
It like that i can't kill thread's threa,how can i do
public class Frame1 extends JFrame{
...
PooledRemoteFileServer server;
//start or stop listening thread

void jButton1_actionPerformed(ActionEvent e) {
if(server== null) {
server = new PooledRemoteFileServer(9000, 5);
server.start();
jButton1.setEnabled(false);
jButton2.setEnabled(false);
jButton1.setText("Stop");
jTextField1.setText("Started");
jButton1.setEnabled(true);
}
else {
jButton1.setEnabled(false);
server.stopit();
server.clean();
try
{
server.serversocket.close();
server.serversocket=null;

server=null;

}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
jButton1.setText("Start");
jTextField1.setText("Stopped");
jButton2.setEnabled(true);
jButton1.setEnabled(true);

}
}
//listen thread
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import java.lang.*;
public class PooledRemoteFileServer extends Thread {
protected int maxConnections;
protected int listenPort;
protected ServerSocket serversocket=null;
private Vector clientSocketVect=new Vector();
private Vector handlethread = new Vector();
private boolean blinker=true;
public PooledRemoteFileServer(int aListenPort, int maxConnections) {
listenPort = aListenPort;
this.maxConnections = maxConnections;

}
public void acceptConnections(){
try {
serversocket = new ServerSocket(listenPort, maxConnections);
Socket incomingConnection = null;
while(true) {
if(blinker==false)
{
System.out.println("thread exit");
return ;
}
System.out.println("listening");
incomingConnection = serversocket.accept();
clientSocketVect.add(incomingConnection);
handleConnection(incomingConnection);
}
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
public void stopit()
{
blinker=false;

}
public void clean()
{

try {
Socket psock = null;
PooledConnectionHandler temphandler=null;
for(int j=0;j<maxConnections;j++)
{
temphandler=(PooledConnectionHandler)handlethread.get(j);
temphandler.stopit();
temphandler=null;
}
handlethread.clear();
System.out.println("son thread clean ok");
for (int i=0;i<clientSocketVect.size();i++) {
psock = (Socket)clientSocketVect.get(i);
psock.close();
psock = null;
}
clientSocketVect.clear();
// serversocket.close();
// serversocket = null;
System.out.println("socket clean ok");
}
catch(Exception excpt) {
System.err.println("Failed : "+ excpt);
}

}
public void setUpHandlers() {
for (int i = 0; i < maxConnections; i++) {
PooledConnectionHandler currentHandler = new PooledConnectionHandler();
handlethread.add(currentHandler);
new Thread(currentHandler, "Handler " + i).start();
}
}
protected void handleConnection(Socket connectionToHandle) {
PooledConnectionHandler.processRequest(connectionToHandle);
}
public void run() {
this.setUpHandlers();
this.acceptConnections();
}
}
//handel connection
public class PooledConnectionHandler extends Thread {
public boolean blinker=true;
public PooledConnectionHandler() {
}
//put connection in pool
public static void processRequest(Socket requestToHandle) {
synchronized (pool) {
pool.add(pool.size(), requestToHandle);
pool.notifyAll();
}
}
public void stopit()
{
blinker=false;
}
public void run() {

while (true) {
if(blinker==false)
{
System.out.println("son exit");
return ;
}
synchronized (pool) {
System.out.println("son run");
while (pool.isEmpty()) {
try {
pool.wait();
}
catch (InterruptedException e) {

return;
}
}
connection = (Socket) pool.remove(0);
ClientStart(connection);
}
handleConnection();//handel connection
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: socket and thread,puzzel me days,who can help me?