import java.io.*;
import java.net.*;class Client {
public static void main(String args[]) throws Exception{
Socket server = new Socket("127.0.0.1",4567);
System.out.println("Connected with Server..");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(server.getOutputStream());
out.println("ok?????");
out.flush();
int choice = new Client().display();
String ss;
System.out.println(choice);
while(choice!=2){
if(choice == 0){
System.out.println("输入用户名密码,用,分隔");
out.println(br.readLine());
out.flush();
System.out.println("what?fuck");
}
else if(choice == 1){
System.out.println("输入新用户名密码,用,分隔");
ss = br.readLine();
out.println(ss);
out.flush();
}
choice = new Client().display();
}
server.close();
}
public int display() throws Exception{
System.out.println("0.User Login");
System.out.println("1.Register new User");
System.out.println("3.quit");
//System.out.println((int)(System.in.read()));
return (int)(System.in.read())-48;
}
}
choice == 0 的时候 直接跳过out.println(br.readLine());
纠结 求解释
import java.net.*;class Client {
public static void main(String args[]) throws Exception{
Socket server = new Socket("127.0.0.1",4567);
System.out.println("Connected with Server..");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(server.getOutputStream());
out.println("ok?????");
out.flush();
int choice = new Client().display();
String ss;
System.out.println(choice);
while(choice!=2){
if(choice == 0){
System.out.println("输入用户名密码,用,分隔");
out.println(br.readLine());
out.flush();
System.out.println("what?fuck");
}
else if(choice == 1){
System.out.println("输入新用户名密码,用,分隔");
ss = br.readLine();
out.println(ss);
out.flush();
}
choice = new Client().display();
}
server.close();
}
public int display() throws Exception{
System.out.println("0.User Login");
System.out.println("1.Register new User");
System.out.println("3.quit");
//System.out.println((int)(System.in.read()));
return (int)(System.in.read())-48;
}
}
choice == 0 的时候 直接跳过out.println(br.readLine());
纠结 求解释
import java.net.*;public class Server {
public static void main(String args[]) throws Exception{
ServerSocket server = new ServerSocket(4567);
System.out.println("Server is ready...please Open the Client");
Socket client = server.accept();//侦听并接收到 Server的连接,accept产生一个阻塞,直到等待到一个连接,之后返回一个客户端的Socket实例
System.out.println("client is Open ..Server is working");
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String msg = br.readLine();
System.out.println("msg" + msg);
//if(msg.startsWith("0")){}
while(!msg.equals("quit")){
System.out.println("you got message from client: " + msg);
msg = br.readLine();
}
br.readLine();
System.out.println("client " + msg + "ed");
bw.flush();
server.close();
client.close();
}
}server接收到的是空的字符串
后加断点,单步调试看看
现在的问题就是client端 br.readLine() 不等待键盘输入,server端接收到的直接是空的字符串,
怎么回事呢?
System.out.println("0.User Login");
System.out.println("1.Register new User");
System.out.println("3.quit");
Scanner console=new Scanner(System.in);
int choice=console.nextInt();
return choice;
}
int str2 = br.read();
int str3 = br.read();
out.println(br.readLine());
out.flush();你debug看下,发现str2是13,str3为10,所以得到是空字符串就知道什么原因了。