Java套接字编程,服务器不发送消息

时间:2015-02-07 16:46:29

标签: java sockets

我正在构建一个简单的聊天程序(非常简单),它将有一个服务器类和其他客户端类。客户端将发送用户名和密码,服务器将验证它们并将相应地做出响应。如果凭据正确,聊天将开始。 以下是两个班级。   客户类

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

public class NewClient {

BufferedReader br, readKB, readC;
InputStreamReader isr;
String userName, password,msg;
Socket skt;
PrintWriter out;

public static void main(String[] args) throws Exception {
    NewClient client = new NewClient();
    client.credentials();

}

void credentials() throws Exception {
    isr = new InputStreamReader(System.in);
    br = new BufferedReader(isr);
    System.out.println("Enter Your username");
    userName = br.readLine();
    System.out.println(userName);
    System.out.println("Enter Your password");
    password = br.readLine();
    System.out.println(password);
    skt = new Socket("127.0.0.1", 1500);
    out = new PrintWriter(skt.getOutputStream(), true);
    readC = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    out.println(userName);
    out.println(password);
    msg=readC.readLine();
    System.out.println(msg);
    System.out.println("reached here type now");
     do{
            msg = readKB.readLine();
            out.println(msg);

            msg=readKB.readLine();
            System.out.println(msg);
        }
        while(!msg.equals("bye"));


    String str = readC.readLine();

    /*}
     else
     System.out.println("Invalid User");
     */
}

/*  void chitChat()throws Exception{
 readKB = new BufferedReader(new InputStreamReader(System.in));
 out = new PrintWriter(skt.getOutputStream());
 readC = new BufferedReader(new InputStreamReader(skt.getInputStream()));
 do {
 out.println(readKB.readLine());
 System.out.println(readC.readLine());
 }
 while(!readC.readLine().equals("bye"));

 }*/
  }

服务器类

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

public class NewServer {

    BufferedReader readC, readKB;
    PrintWriter out;
    ServerSocket sskt;
    Socket skt;
    boolean flag = false;
    String userName, password;

    public static void main(String[] args) throws Exception {
        NewServer server = new NewServer();
        if (server.verifyCredentials()) {
            server.chitChat();
        }

    }

    boolean verifyCredentials() throws Exception {
        sskt = new ServerSocket(1500);
        skt = sskt.accept();
        readC = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        userName = readC.readLine();
        password = readC.readLine();
        out = new PrintWriter(skt.getOutputStream());

        Scanner scr = new Scanner(new File("password.txt"));
        boolean flag = false;
        while (scr.hasNextLine()) {
            String line = scr.nextLine();
            String Fileusername = line.substring(0, line.indexOf(' '));
            String Filepassword = line.substring(line.indexOf(' ') + 1, line.length());
            if (userName.equals(Fileusername) && password.equals(Filepassword)) {
                System.out.println("Valid user");
                flag = true;
                break;
            }

        }
        if (!flag) {
            System.out.println("invalid user");
        }
        return flag;
    }

    void chitChat() throws Exception {
        String msg;
        out.println("You are a valid user.Starting converseation");
        System.out.println("reached here");

         do{
                msg = readC.readLine();
                System.out.println(msg);
                msg=readKB.readLine();
                out.println(msg);
            }
            while(!msg.equals("bye"));

    }

}

现在到目前为止客户端能够向服务器发送用户名和密码,服务器能够验证但是在服务器端运行之后 * out.println(“您是有效的用户。开始对话”); *为什么客户不打印此行

最后,我能够修复代码。这是新文件 客户Client code 服务器Server code

1 个答案:

答案 0 :(得分:2)

双方正在读另一方,因此存在僵局。客户端也会抛出它读出的第一条消息,但它尚未到达那一点。