2013-03-05 31 views
0

我编写了一个程序客户端 - 服务器,具有多线程的发送 - 接收文件。程序运行,客户端发送和服务器接收。文件被创建,但空的新文件被创建 为什么?请帮我具有多线程的客户端服务器

类客户:

import java.io.*; 
    import java.net.Socket; 


public class Client extends Thread { 

Socket socket = null; 
Socket socket1 = null; 
    public void sendFile() throws IOException { 

String host = "127.0.0.1"; 
String host1 = "127.0.0.2"; 

socket = new Socket(host, 1024); 
socket1 = new Socket(host1, 1025); 

File file = new File("/home/reza/Desktop/link help"); 
File file1 = new File("/home/reza/Desktop/hi"); 
long length = file.length(); 
long length1 = file1.length(); 
final byte[] bytes = new byte[(int) length]; 
final byte[] bytes1 = new byte[(int) length1]; 

FileInputStream fis = new FileInputStream(file); 
FileInputStream fis1 = new FileInputStream(file1); 

@SuppressWarnings("resource") 
final BufferedInputStream bis = new BufferedInputStream(fis); 
final BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); 
@SuppressWarnings("resource") 
final BufferedInputStream bis1 = new BufferedInputStream(fis1); 
final BufferedOutputStream out1 = new BufferedOutputStream(socket1.getOutputStream()); 


Thread t = new Thread(new Runnable() { 



    public void run() 
{ 


while(socket.isConnected()) 
    { 

Wait2(); 


    try { 
    System.out.println("ok"); 
    int count; 

    while ((count = bis.read(bytes)) > 0) { 
     out.write(bytes, 0, count); 

    } 
} catch (IOException e) { 

e.printStackTrace(); 
} 

    } 
    } 
    }); 

    Thread t1 = new Thread(new Runnable() { 
    public void run() { 
while(socket1.isConnected()) 
    { 

    Wait2(); 

     try { 
      System.out.println("ok1"); 
      int count1; 
      while ((count1 = bis1.read(bytes1)) > 0) { 
       out1.write(bytes1, 0, count1); 

      } 


    } catch (IOException e) { 

    e.printStackTrace(); 
    } 

    } 

} 
}); 
t1.start(); 
t.start(); 




socket.close(); 
socket1.close(); 
} 

public void Wait2() 

{ 


try { 

Thread.sleep(3000); 

} catch (InterruptedException x) { 

System.out.println("Interrupted!"); 

} 

} 
} 

级服务器:

import java.io.*; 
    import java.net.*; 
public class Server { 

public Server() 
{ 
Thread t = new Thread(new Client()); 
t.start(); 
Thread t1 = new Thread(new Client()); 
t1.start();  
} 



    //@SuppressWarnings("null") 
    public void recivefile() throws IOException { 
    ServerSocket serverSocket = null; 
ServerSocket serverSocket1 = null; 


try { 

    serverSocket = new ServerSocket(1024); 


} catch (IOException ex) { 
    System.out.println("Can't setup server on this port number. "); 
} 
try { 
    serverSocket1 = new ServerSocket(1025); 

    } catch (IOException ex) { 
     System.out.println("Can't setup server on this port number1. "); 
    } 


Socket socket = null; 
Socket socket1 = null; 

InputStream is = null; 
InputStream is1 = null; 

FileOutputStream fos = null; 
FileOutputStream fos1 = null; 

BufferedOutputStream bos = null; 
BufferedOutputStream bos1 = null; 

int bufferSize = 0; 
int bufferSize1 = 0; 


try { 
    socket = serverSocket.accept(); 
    socket1 = serverSocket1.accept(); 




} catch (IOException ex) { 
    System.out.println("Can't accept client connection. "); 
} 

try { 
    is = socket.getInputStream(); 
    is1 = socket1.getInputStream(); 


    bufferSize = socket.getReceiveBufferSize(); 
    bufferSize1 = socket1.getReceiveBufferSize(); 
    //bufferSize2 = socket2.getReceiveBufferSize(); 
    System.out.println("Buffer size: " + bufferSize); 
    System.out.println("file recieved"); 
    System.out.println("Buffer size1: " + bufferSize1); 
    System.out.println("file recieved"); 

    System.out.println("file recieved"); 
} catch (IOException ex) { 
    System.out.println("Can't get socket input stream. "); 
} 


try { 
    fos = new FileOutputStream("/home/reza/Desktop/reza"); 
    bos = new BufferedOutputStream(fos); 
    fos1 = new FileOutputStream("/home/reza/Desktop/ali"); 
    bos1 = new BufferedOutputStream(fos1); 




} catch (FileNotFoundException ex) { 
    System.out.println("File not found. "); 
} 

byte[] bytes = new byte[bufferSize]; 

int count; 

while ((count = is.read(bytes)) > 0) { 
    bos.write(bytes, 0, count); 

} 
byte[] bytes1 = new byte[bufferSize1]; 

int count1; 
while ((count1 = is1.read(bytes1)) > 0) { 
     bos1.write(bytes1, 0, count1); 
    } 


bos.flush(); 
bos.close(); 
bos1.flush(); 
bos1.close(); 
is.close(); 
is1.close(); 
socket.close(); 
serverSocket.close(); 
socket1.close(); 
serverSocket1.close(); 


} 

public static void main(String[] args) throws IOException 
{ 
    System.out.println("server is run, please send file"); 



    Server s = new Server(); 
    s.recivefile(); 


} 
} 

客户端测试类:

import java.io.IOException; 


public class clientTest extends Thread { 

public static void main(String[] args) throws IOException, InterruptedException 
{ 
    Client client = new Client(); 
    client.sendFile(); 
    } 
    } 
+1

为了更好地帮助您,我建议您设置格式化代码,使其更易于阅读 – 2013-03-05 12:13:25

+0

首先,我会建议这不是多线程服务器。在向前移动之前,您有一个等待两个连接的单个线程,而不是两个可独立接收数据的线程。如果这是HW,你可能会失去很多分数。 – 2013-03-05 12:23:10

+0

确切地说,你是对的,我已经失去了 – reza 2013-03-05 12:58:46

回答

0

我相信这个代码在服务器是你的问题:

while ((count = is.read(bytes)) > 0) { 
    bos.write(bytes, 0, count); 
} 
byte[] bytes1 = new byte[bufferSize1]; 

int count1; 
while ((count1 = is1.read(bytes1)) > 0) { 
    bos1.write(bytes1, 0, count1); 
} 
bos.flush(); 
bos.close(); 
bos1.flush(); 
bos1.close(); 
is.close(); 
is1.close(); 
socket.close(); 
serverSocket.close(); 
socket1.close(); 
serverSocket1.close(); 

因此,服务器已连接到客户端,然后它立即检查是否有任何字节要读取,如果不是,它会停止读取并关闭连接。如果发生这种情况的速度比客户端可以提供任何字节快,繁荣,则不会收到任何数据。而且它会比客户端发送数据的速度更快,因为客户端正在连接,然后开始线程发送数据。

只要客户端维持连接处于活动状态,服务器就应该在每个连接上进行读取。服务器需要等待接收数据。

请注意,在您的代码中,客户端正在等待服务器关闭连接。但是,服务器应该如何知道所有数据何时发送?客户端必须关闭连接,否则客户端必须向服务器发送EOF类型标记,指示数据已结束,并且关闭连接是安全的。

相关问题