简体   繁体   English

服务器-客户端 Java

[英]Server-Client Java

I made a basic server and client program in Java and when you start the server than the client it connects the two if the user types in the password "root" and i saved that value as password, and I'm trying to make it so when the connection starts on the server side it outputs "The password is" than the value of password, but everytime i run it the program outputs "The password is null", why is it giving me this??我用 Java 制作了一个基本的服务器和客户端程序,当您启动服务器而不是客户端时,如果用户输入密码“root”,并且我将该值保存为密码,则它会连接两者,我正在尝试这样做当连接在服务器端启动时,它输出“密码是”而不是密码的值,但是每次我运行它时,程序都会输出“密码为空”,为什么要给我这个?? I changed the string to static and it still gave me null, please help and explain??我将字符串更改为静态,它仍然给我空值,请帮忙解释一下??

Server side code服务端代码

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public void lookForConnection(){
    Socket connection;
    clientMethod cM = new clientMethod();
    {
        try
        {
            System.out.print("Program running...");
            ServerSocket serverSocket = new ServerSocket(6789);
            while(true) 
            {
                connection = serverSocket.accept();
                System.out.println("Connected Succesfully!");
                System.out.println(connection.getInetAddress().getHostName());
                System.out.println("The password for this connection is " + cM.password);              
            }
        }
        catch(IOException ex)
        {
            System.out.println (ex.toString());

        }

    }
}

Client side code客户端代码

import java.io.EOFException;
import java.util.Scanner;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class clientMethod {
    public String serverIP;
    public String password;
    public Socket connection; 
    public void Client(String host)
    {
        serverIP = host;
    }
    public void startRunning() {
        try{
            Scanner console = new Scanner(System.in);
            System.out.print("Please provide the admin password: ");
            password = console.next();
            if(password.compareTo("root") == 0 ) {
                connectToServer();          
            //}
            }
        }
        catch(EOFException eofException){
            System.out.print("\n Client terminated the connection");
        }
        catch(IOException ioException) {
            ioException.printStackTrace();
        }
    }
    private void connectToServer() throws IOException {
        System.out.println("Attempting connection...");
        connection = new Socket(InetAddress.getByName(serverIP), 6789);
        System.out.println("Connected to " + connection.getInetAddress().getHostName());

    }
}

Probably because you never send your password to your Server, you only print the value of the variable password of your local variable cM that is never set so it is null for sure.可能是因为您从未将密码发送到您的服务器,您只打印了从未设置的本地变量cM的变量password的值,因此它肯定为null

Check this tutorial to see how to send data to a server but basically the idea is to write your data from your client using the OutputStream that you get from Socket#getOutputStream() , it will send data to the server and from the server you read it from Socket#getInputStream() .查看本教程以了解如何将数据发送到服务器,但基本上的想法是使用从Socket#getOutputStream()获得的OutputStream从客户端写入数据,它会将数据发送到服务器和您读取的服务器它来自Socket#getInputStream()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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