使用java

时间:2015-04-22 12:17:06

标签: java sockets client-server

我最近发现了一个很好的代码来检测连接到PC的外部驱动器,所以 我尝试用简单的客户端服务器程序进行实验。 我希望服务器在连接外部驱动器时向客户端发送通知。

这是java中用于检测驱动器的原始代码。

import java.io.*;
public class FindDrive
{
public static void main(String[] args)
{
String[] letters = new String[]{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
File[] drives = new File[letters.length];
boolean[] isDrive = new boolean[letters.length];

// init the file objects and the initial drive state
for ( int i = 0; i < letters.length; ++i )
    {
    drives[i] = new File(letters[i]+":/");

    isDrive[i] = drives[i].canRead();
    }

 System.out.println("FindDrive: waiting for devices...");

 // loop indefinitely
 while(true)
    {
    // check each drive 
    for ( int i = 0; i < letters.length; ++i )
        {
        boolean pluggedIn = drives[i].canRead();

        // if the state has changed output a message
        if ( pluggedIn != isDrive[i] )
            {
            if ( pluggedIn )
                System.out.println("Drive "+letters[i]+" has been plugged in");
            else
                System.out.println("Drive "+letters[i]+" has been unplugged");

            isDrive[i] = pluggedIn;
            }
        }

    // wait before looping
    try { Thread.sleep(100); }
    catch (InterruptedException e) { /* do nothing */ }

    }
}
}

服务器代码:

import java.io.*;
import java.net.*;    
public class Pserver
{
private static Socket socket;
public static void main(String[] args)
{
    try
    {
    int port = 25000;
    ServerSocket serverSocket = new ServerSocket(port);
    while(true)
    {
        //Reading the message from the client
        socket = serverSocket.accept();
        System.out.println("Client has connected!");

        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        String returnPassMessage="The device has plugged in.You can use this device";
        String returnFailMessage="The device has been unplugged";

        //detect drive code

        String[] letters = new String[]{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J","L"};
        File[] drives = new File[letters.length];
        boolean[] isDrive = new boolean[letters.length];

    // init the file objects and the initial drive state
    for ( int i = 0; i < letters.length; ++i )
        {
        drives[i] = new File(letters[i]+":/");

        isDrive[i] = drives[i].canRead();
        }

    System.out.println("FindDrive: waiting for devices...");

     // loop indefinitely
    while(true)
        {
        // check each drive 
        for ( int i = 0; i < letters.length; ++i )
            {
            boolean pluggedIn = drives[i].canRead();

            // if the state has changed output a message
            if ( pluggedIn != isDrive[i] )
                {
                if ( pluggedIn )
                {
                    System.out.println("Drive "+letters[i]+" has been plugged in");
                    bw.write(returnPassMessage);
                }
                else
                 {   System.out.println("Drive "+letters[i]+" has been unplugged");
                    bw.write(returnFailMessage);

                isDrive[i] = pluggedIn;
                }
                }
            }

            // wait before looping
        try { Thread.sleep(2000); }
        catch (InterruptedException e) { /* do nothing */ }
        }
    }
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

客户代码:

import java.io.*;
import java.net.*;
import java.util.*;
public class Pclient
{
private static Socket socket;
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
while(true)
{
    try
    {
        String host = "localhost";
        int port = 25000;
        InetAddress address = InetAddress.getByName(host);
        socket = new Socket(address, port);
        System.out.println("You're now connected to the Server"); 

        //Get the return message from the server
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);


        while(true)
       {
       String message = br.readLine();
        System.out.println("Message received from the server : " +message);

        //to wait before loop
        try { Thread.sleep(2000); }
        catch (InterruptedException e) { /* do nothing */ }
        }


    }

    catch (ConnectException e) 
    { //When the connection is refused upon connecting to the server
       //promt the user here
    System.out.println("Connection refused");
    break;
    }
    catch (IOException exception)
    {
        //when connection drops, server closed, loses connection
     //promt the user here
    System.out.println("Disconnected from server");
    break; //to quit the infinite loop
    }
}
}
}

独立检测驱动器代码完美运行。但无论何时它在服务器上运行,“驱动器已插入”消息重复,最重要的是没有消息发送到客户端。请帮忙!

0 个答案:

没有答案