簡體   English   中英

Java套接字編程,服務器未發送消息

[英]Java socket programming,server is not sending messages

我正在構建一個簡單的聊天程序(非常簡單),它將具有一個服務器類和其他客戶端類。客戶端將發送用戶名和密碼,服務器將對其進行驗證並做出相應的響應。如果憑據正確,則將開始聊天。 下面是兩個類。 客戶類別

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(“您是有效用戶。開始對話”)之后,服務器可以在服務器端進行驗證。 *為什么客戶不打印此行

終於我能夠修復代碼了。 這是新文件客戶客戶端代碼服務器服務器代碼

雙方都在互相閱讀,因此存在僵局。 客戶端還將拋出它讀取的第一條消息,但是還沒有到此為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM