2009-10-28 38 views
1

嗨,我已经创建了一个服务器类与threadpool如下所示,它使用workerRunnable类。我正在面对的这个代码的问题是,当我试图从两个客户端同时向这个服务器发送文件时,它给了我一个不规则的响应(从第一个线程运行到下一个客户端请求尽快运行来自第二个客户端的请求cums停止第一个请求并开始处理第二个请求,第二个响应发送给两个客户端套接字而不是发送它们各自的响应)...请问任何人可以告诉我我要去哪里错误?????无法处理多个客户端与线程池,因为它暂停第一个线程,当我得到第二个客户端请求

package com.tel.snmp; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.io.IOException; 
import java.util.concurrent.ArrayBlockingQueue; 
import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.ThreadPoolExecutor; 
import java.util.concurrent.TimeUnit; 

public class ThreadPooledServer implements Runnable{ 


    protected int   serverPort = 4444;//hk 
    protected ServerSocket serverSocket = null; 
    protected boolean  isStopped = false; 
    protected Thread  runningThread= null; 
    public BlockingQueue q = new ArrayBlockingQueue(20); 
    public static int clientconnection = 0; 


    ThreadPoolExecutor threadpool = new ThreadPoolExecutor(4,10,20,TimeUnit.SECONDS,q); 

    public ThreadPooledServer(int port){ 
     this.serverPort = port; // wrk2 
      } 

    public void run() 
    { 
     synchronized(this) 
     { 
      this.runningThread = Thread.currentThread(); 
     } 
     openServerSocket(); 
     while(! isStopped()){ 
      Socket clientSocket = null; 
      try 
      { 
       //System.out.println("the value of client connection BEFORE is"+clientconnection); 

       clientSocket = this.serverSocket.accept(); 
       clientconnection++; 
       System.out.println("the value of client connection is"+clientconnection); 
      } catch (IOException e) 
      { 
       if(isStopped()) 
       { 
        System.out.println("Server Stopped.") ; 
        return; 
       } 
       throw new RuntimeException(
        "Error accepting client connection", e); 
      } 
      this.threadpool.execute(new WorkerRunnable(clientSocket,"Thread pooled server")); 

     } 
     System.out.println("Server Stopped.") ; 
    } 


    private synchronized boolean isStopped() { 
     return this.isStopped; 
    } 

    public synchronized void stop(){ 
     this.isStopped = true; 
     try { 
      this.serverSocket.close(); 
     } catch (IOException e) { 
      throw new RuntimeException("Error closing server", e); 
     } 
    } 

    private void openServerSocket() { 
     try { 
      this.serverSocket = new ServerSocket(this.serverPort); //wrkr2 
        } 
     catch (IOException e) { 
      throw new RuntimeException("Cannot open port serverPort"+serverPort, e); 
     } 
    } 
} 
-----------------------------Worker Runnable class----------------------------- 
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package com.tel.snmp; 

/** 
* 
* @author harikrishnadv 
*/ 
import com.tel.common.ProtocolSelector; 
import java.io.*; 
import java.net.*; 


/*public class WorkerRunnable implements Runnable{ 

    protected Socket clientSocket = null; 
    protected String serverText = null; 

    public WorkerRunnable(Socket clientSocket, String serverText) { 
     this.clientSocket = clientSocket; 
     this.serverText = serverText; 
    } 

    public void run() { 
     try { 
      InputStream input = clientSocket.getInputStream(); 
      OutputStream output = clientSocket.getOutputStream(); 
      long time = System.currentTimeMillis(); 
      output.write(("HTTP/1.1 200 OK\n\nWorkerRunnable: " + 
        this.serverText + " - " + 
        time + 
        "").getBytes()); 
      output.close(); 
      input.close(); 
      System.out.println("Request processed: " + time); 
     } catch (IOException e) { 
      //report exception somewhere. 
      e.printStackTrace(); 
     } 
    } 
} 
*/ 
public class WorkerRunnable implements Runnable 
{ 
    FileInputStream fis; 
FileOutputStream fos; 
BufferedInputStream bis; 
BufferedOutputStream bos; 
     String filename="clientfile"; 
     String fname=null; 
//Socket soc; 
     int flag=0; 
int ch; 
     //static int count=0;// new 
    protected Socket clientSocket = null; 
    protected String serverText = null; 

    public WorkerRunnable(Socket clientSocket, String serverText) { 
     this.clientSocket = clientSocket; 
     this.serverText = serverText; 

    } 

    public synchronized void run() { 
    try { 
    receiveFile(); 
         /*try{ 
          this.wait(); 
         } 
         catch(InterruptedException i) 
         { 

         }*/ 
        if(flag==1) 
         { 
         System.out.println("**********************************************************************************************************************************"); 
         sendFile();       
         } 
         closeAll(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
} 

     /** Method to send the response file to the client */ 
public void sendFile() throws IOException { 
    // SENDING A FILE 
       //String sendfile=EMS.fileprocessname+EMS.clientcount+".xml"; 
       String sendfile=EM.fileprocessname;//+EM.clientcount; 
       System.out.println("filename that has been sending to client is"+sendfile); 
       bos = new BufferedOutputStream(clientSocket.getOutputStream()); 
       //fis = new FileInputStream("C://outputs.xml"); 
       fis = new FileInputStream(sendfile); 
    while ((ch = fis.read()) != -1) { 
    bos.write(ch); 
    bos.flush(); 
    } 
    bos.write(-1); 
    bos.flush(); 

    System.out.println("File Sent to :: " + clientSocket); 
    fis.close();    
} 

/** Method to receive input file from client */ 
     public void receiveFile() throws IOException { 
    // RECEIVING A FILE 
       fname="C://"+filename+ThreadPooledServer.clientconnection+".xml"; 
    bis = new BufferedInputStream(clientSocket.getInputStream()); 
       //fos = new FileOutputStream("C://client.xml"); 
       fos = new FileOutputStream(fname); 
    while ((ch = bis.read()) != 255) { 
    fos.write(ch); 
    fos.flush(); 
    } 
    System.out.println("File Received from :: " +clientSocket); 
    fos.close(); 

       if(flag==0){ 
        ProtocolSelector m=new ProtocolSelector();     
        //m.xmldecider("C://client.xml"); 

        m.xmldecider(fname); 
        flag=1;         
       } 
} 

public void closeAll() throws IOException { 
    bis.close(); 
    bos.close(); 
} 
} 

我就在感谢乌拉圭回合宝贵的答复的

回答

1

clientconnection字段是静态的,而是然后从你的WorkerRunnable receiveFile()方法来访问。在执行receiveFile()方法时,不能保证clientconnection的值仍然正确 - 另一个客户端可能已经出现并增加了它。

尝试更改您的WorkerRunnable构造函数以将clientconnection作为参数,例如

变化:

this.threadpool.execute(new WorkerRunnable(clientSocket, 
    "Thread pooled server")); 

到:

this.threadpool.execute(new WorkerRunnable(clientSocket, clientconnection, 
    "Thread pooled server")); 

一个clientconnection字段添加到您的WorkerRunnable然后改变这一行:

fname="C://"+filename+ThreadPooledServer.clientconnection+".xml"; 

到:

fname="C://"+filename+clientconnection+".xml"; 
+0

谢谢你.....你的权利,因为你已经说过,在提出请求的同时,它正在发生变化.....但我仍然面临着这个问题....可以在任何地方找到否则我会在代码中出错......并感谢你之前的回复 – user198020 2009-10-29 07:27:10

+0

客户端连接错误将解释为什么两个客户端都收到相同的响应。你能否更新你的问题并提供你现在看到的一些细节? – monorailkitty 2009-10-29 14:47:50