• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
wenjunxiang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic