具有多线程的客户端服务器

时间:2013-03-05 12:08:47

标签: java multithreading client-server

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

班级客户:

  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 个答案:

答案 0 :(得分: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类型标记,指示数据结束并且可以安全地关闭连接。