简体   繁体   中英

Java socket programming,server is not sending messages

I was building a simple chat program(very simple) which will be having one server class and other client class.Client will send username and password and server will verify them and will respond accordingly.If credentials are correct,chat will begin. Below are both classes. Client Class

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"));

 }*/
  }

server Class

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"));

    }

}

Now till now client is able to send username and password to server and server is able to validate that but after that on server side when running *out.println("You are a valid user.Starting converseation"); *Why client is not printing this line

Finally i was able to fix the code. Here are new files client Client code server Server code

Both sides are reading from the other, so there is a deadlock. The client will also throw the first message it reads away, but it hasn't got to that point yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM