为什么客户端一直卡在客户端的read方法中?

时间:2014-03-16 14:11:38

标签: java networking

我创建了2个Java应用程序来构建聊天室,当尝试通过网络读取消息时,客户端应用程序冻结。

它永远不会说'离开readText'但它确实说'在readText'在客户端应用程序上。

服务器应用程序:

Server.java:

import java.io.*;
import java.net.*;
import java.util.*;


public class Server
{

    public static void main(String[] args) throws IOException
    {
        final int PORT                  = 2525;
        Socket sock                     = null;
        ServerSocket serverSocket           = null;


        UserHandler userHandler             = null;

        try
        {
            serverSocket = new ServerSocket( PORT );
            System.out.println( "Service started..." );
        }
        catch( IOException exc )
        {
            System.out.println( "Failed to start service..." );
            System.exit( 0 );
        }

        do
        {
            sock = hSSocket.accept();
            System.out.println( "Incoming connection..." );
            userHandler = new UserHandler( sock );
            userHandler.start();
        }while( true );

    }

}

UserHandler.java:

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;

public class UserHandler extends Thread
{
    private Socket  sock                    = null;
    private Scanner in              = null;

    private String user                 = "";
    private boolean loggedIn                = false;

    public UserHandler( Socket sock ) throws IOException
    {
        super("UserHandler");
        this.sock = sock;

        in = new Scanner( this.sock.getInputStream() );
    }

    public void run()
    {
        String inText;

        System.out.printf( "Running...\n" );

        do
        {
            inText = in.nextLine();
            System.out.println( msg );

            try
            {
                processInText( inText );
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }while( !msg.equals("Disconnect") );

        try
        {
            sock.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void processInText( String inText )
    {
        String password = "";

        code = msg.substring( 0, 4 );

        switch( inText )
        {
        case "User":

            if( loggedIn )
            {
                System.out.println( "Already logged in" );
                sendText( "allw" );
                return;
            }

            if( inText.length() > 5 )
            {
                name = inText.substring( 5 );
                System.out.println( "Asking for password" );
                sendText( "Pass" );
                return;
            }

            sendText( "allw" );

            break;

        case "Pass":

            System.out.printf( "password received %s\n", inText.substring(5) );

                    if( user.equals( "user" ) && inText.substring( 5 ).equals( "pass" ) )
                       loggedIn = true;

                    break;

        default:
            System.out.println( "Unknown" );

        }
    }


    private void sendText( String outText )
    {
        PrintWriter out = null;
        System.out.println( "Inside sendText" );
        try
        {
            out = new PrintWriter( sock.getOutputStream() );

        } catch (IOException e)
        {
            e.printStackTrace();
        }

        System.out.println( "Sending message..." );
        System.out.printf( "%s\n", outText);
        out.println( outText );
        System.out.println( "Message sent..." );
    }





}

客户申请:

Client.java:

import java.io.*;
import java.net.*;
import java.util.*;


public class Client extends Thread
{
    private static final int PORT           = 2525;

    private Socket sock                 = null;
    private InetAddress host                = null;
    private Scanner in              = null;
    private PrintWriter out         = null;

    public static void main(String[] args)
    {
        Client client = new Client();
        client.setup();
        client.start();
    }

    public void sendText( String outText )
    {
        out.println( outText );
    }

    public String readText()
    {
        String inText = "";
        System.out.println( "Inside readText" );

        if( in != null )
            inText = in.nextLine();

        System.out.println( "leaving readText" );
        return inText;
    }

    public void setup()
    {
        try
        {
            host = InetAddress.getLocalHost();
            sock = new Socket( host, PORT );
        }
        catch( IOException exc )
        {

        }

        try
        {
            in = new Scanner( sock.getInputStream() );
            out = new PrintWriter( sock.getOutputStream(), true );

        } catch (Exception e)
        {

        }

    }


    private void sortText( String inText )
    {
        try
        {

            System.out.println( "Handling comms..." );

            switch( inText.substring( 0, 3 ) )
            {
                    case "User":
                System.out.println( "Sending user" );
                sendText("User user" );
                break;

            case "Pass":
                System.out.println( "Sending pass" );
                sendText("Pass pass" );
                break;

            case "Disc":
                System.out.println( "User " + msg.substring(3) + " disconnected." );
                break;

            case "allw":
                System.out.println( "logged in" );
                break;

            default:

                System.out.println( "Who knows what the server is saying" );
            }
        }
        catch( Exception e)
        {

        }
        // TODO Auto-generated method stub
    }

    @Override
    public void run()
    {
        String inText = "";

        do
        {
            System.out.println( "sending user" );
            sendText( "User user" );
            System.out.println( "User sent" );
            readText();
            System.out.println( "sending pass" );
            sendMessage( "Pass pass" );
            System.out.println( "Pass sent" );

            inText = readMessage();
            sortText( inText );

        }while( !inText.equals( "Disconnect" ) );



    }

}

0 个答案:

没有答案