客户端到服务器消息传递-Java

客户端到服务器消息传递-Java,java,client,Java,Client,我想要一些关于我相对简单的客户端到服务器消息传递程序的输入 我认为客户机部分还可以,但是服务器部分有点坏了 该程序实际上可以正常工作(客户端可以向服务器发送消息) 我在输入用户名+密码时遇到问题(将其设置为默认密码+用户名,而不是存储多个用户名+密码)(当前使用用户名:1,密码:2) 我想让服务器在连接时接收客户端Ip,如果有人有空闲时间和java经验,我将不胜感激 客户: import java.lang.*; import java.io.*; import java.net.*; impo

我想要一些关于我相对简单的客户端到服务器消息传递程序的输入

我认为客户机部分还可以,但是服务器部分有点坏了

该程序实际上可以正常工作(客户端可以向服务器发送消息)

我在输入用户名+密码时遇到问题(将其设置为默认密码+用户名,而不是存储多个用户名+密码)(当前使用用户名:1,密码:2)

我想让服务器在连接时接收客户端Ip,如果有人有空闲时间和java经验,我将不胜感激

客户:

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class LongClient {
    public static void main(String args[]) {
        try {
            Socket skt = new Socket("localhost", 1234);
            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            Scanner kbReader = new Scanner(System.in);
            PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
            out.flush();

            String message, servermessage;
            InetAddress clientip = InetAddress.getLocalHost();
            System.out.println("Preparing to chat...");
            out.println("Client IP Address: " + clientip); // sends message to client giving ip
            out.println("Client hostname: " + clientip.getHostAddress()); // send message to client giving computer name

            do {
                if (in.ready()) {
                    servermessage = in.readLine();
                    System.out.println("server>: " + servermessage);
                }

                message = kbReader.nextLine();
                out.println(message);

                out.println("Done");

                //message="bye";
                //out.println("bye");
                Thread.currentThread().sleep(300);
            } while (!message.equals("bye"));

            out.close();
            in.close();
        } catch(Exception e) {
            System.out.print(e);
        }
    } 
}
服务器:

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

class LongServer {
    public static void main(String args[]) throws IOException {
        String data = "Welcome to My Server"; //welcome message
        String data1 = "enter username"; //welcome message
        String message; 

        int idstats1 = 0; //verifyed username
        int idstats2 = 0; //verifyed password 
        int verify1 = 0;
        try {
            //Detecting the localhost's ip address
            InetAddress localaddr = InetAddress.getLocalHost();
            System.out.println ("Local IP Address : " + localaddr );
            System.out.println ("Local hostname : " + localaddr.getHostAddress());

            //Creating a server socket for connection
            ServerSocket srvr = new ServerSocket(1234);
            System.out.println("Waiting for connection on "+localaddr);

            //Accept incoming connection
            Socket skt = srvr.accept();
            System.out.print("Server has connected!\n");

            //get Input and Output streams
            PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
            out.flush();
            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            System.out.print("Sending string: '" + data + "'\n");
            out.println(data); //sends welcome message
            System.out.print("Sending string: '" + data1 + "'\n");
            out.println(data1); //sends welcome message

            message = in.readLine(); //reads the line typed in
            while(verify1 == 0) {
                if (idstats1 == 0) {
                    //if the client is not verifyed, it asks for a password
                    out.println("enter username");
                }
                if (idstats2 == 0) {
                    //if the client is not verifyed, it asks for a password
                    out.println("enter password");
                }

                if (message.equals("1")) { 
                    //is the input is password, then the client is verifyed
                    out.println("Username accepted");
                    idstats1 = 1;    
                }
                if (message.equals("2")) { 
                    //is the input is password, then the client is verifyed
                    out.println("Password Accepted");
                    idstats2 = 1;    
                }
                if (idstats1 == 1 && idstats2 == 1) {
                    verify1 = 1;
                }                           
            }

            if (verify1 == 1) {
                //if client is verifyed, it asks them to type a message and prints it to the console
                out.println("enter a message");
                System.out.println("client>"+message);
            }
            if (message.equals("bye")) { 
                //if the client enters "bye" then the server closes
                out.println("Server closing");
                System.out.println("server>Server closing");
            }
            while(!message.equals("bye")); {
                out.close();
                skt.close();
                srvr.close();
            }
        } catch(BindException e) {
            e.printStackTrace();
            System.out.print("A server is already running on the same port.");        
        } catch(SocketException e) {
            e.printStackTrace();
            System.out.print("Client has disconnected rudely.");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.print(e);    
        }
    }
}

谢谢,如果您有任何帮助,我们将不胜感激。

您的代码有一些奇怪的地方-
while(!message.equals(“拜拜”);
服务器中的{
将无法编译:(你能修复它吗?看起来你忘记了
做{
。如果你问我,似乎你需要更高级别的流。你应该查看
ObjectOutputStream
。这将允许你发送instaces而不仅仅是原语。这样,你可以创建不同的类(对于不同类型),其中包含信息。您可以创建该类的新实例,在其中存储数据,然后将其发送过来。当您收到对象时(在.readObject()中),您可以使用
instanceof
在哪个类类型之间切换。这样您就可以拥有LoginPacket.java和MessagePacket。java@AlexeyMalev这是为了看它,我想如果我现在就把它拿出来,以后再担心它会很好,代码行在客户端工作,但在服务器端不工作,对我来说,这表明t发送信息有问题,你怎么看?