我有一个银行帐户计划,用户可以在其中添加,取款和查看帐户详细信息。我试图在客户端和服务器之间来回发送这些数据。我正在建立连接,但我无法来回发送数据。我为使用nosuchelementexception
对象获取服务器值的每一行获得Scanner
:
Scanner sScan = new Scanner(s.getInputStream());
它是来回传递数据的正确方法吗?
以下是客户:
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
{
String user_name=null,type,temp;
type = null;
int balance=0,tmp=0,temp1=0;
int withdraw=0,currentBal=0;
int randNumber = 0;
randNumber = (int)((Math.random() * 9000)+1000);
create_account user = new create_account("user",0,"savings");
Scanner in = new Scanner(System.in);
Scanner strng=new Scanner(System.in);
try{
//new socket for communicatio
Socket s = new Socket("127.0.0.1", 3000);
//scanner for accepting data from serve
Scanner sScan = new Scanner(s.getInputStream());
//new printstream for passing values to server
PrintStream p = new PrintStream(s.getOutputStream());
System.out.println("Connected");
int userChoice;
boolean quit = false;
System.out.println("Welcome to UNCC Banking, please select from the menu");
do {
//present menu
System.out.println("1. Deposit money");
System.out.println("2. Withdraw money");
System.out.println("3. Check balance");
System.out.println("4. Display Account Details");
System.out.println("0. to quit: \n");
System.out.print("Enter Your Choice : ");
//send userChoice to Server?
userChoice = in.nextInt();
//p.println(userChoice);
//get number from server?
// temp1 = sScan.nextInt();
switch (userChoice) {
case 1: // deposit
//p.println(tmp);
//temp1 = sScan.nextInt();
System.out.print("Enter Amount Of Money : ");
//balance=in.nextInt();
p.println(balance);
temp1 = sScan.nextInt();
user.Acc_Balance=balance;
System.out.println("\t Successfully Deposited.");
break;
case 2: // withdraw money
if(user.Acc_Balance==0)
System.out.print("Your Account is Empty.");
else{
System.out.print("Enter Amout Of Money : ");
//withdraw=in.nextInt();
p.println(withdraw);
temp1=sScan.nextInt();
if(withdraw>user.Acc_Balance){
System.out.print("Please enter Valid Amout of Money : ");
//withdraw=in.nextInt();
p.println(withdraw);
temp1=sScan.nextInt();
}
else
currentBal= user.withdraw(withdraw);
System.out.println("Your Current Balance : "+currentBal);
p.println(currentBal);
}
break;
case 4: // check balance
System.out.println("Your Current Balance : "+user.Acc_Balance);
p.println(user.Acc_Balance);
break;
case 5: // display account info
user.display_details();
break;
case 0:
quit = true;
break;
default:
System.out.println("Invalid Choice.");
break;
}
System.out.println("\n");
} while (!quit);
System.out.println("Thank you for visiting UNCC Banking");
}
catch(Exception e){e.printStackTrace();}
}
}
}
以下是服务器:
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class BankAccount {
String name,acc_type;
int Acc_Balance;
BankAccount(){
}
BankAccount(String n,int b,String a_t) {
try{
ServerSocket s1 = new ServerSocket(3000);
Socket s2 = s1.accept();
Scanner sc=new Scanner(s2.getInputStream());
PrintStream p = new PrintStream(s2.getOutputStream());
System.out.println("Connected");
name=n;
p.println(name);
name = sc.nextLine();
Acc_Balance=b;
p.println(Acc_Balance);
Acc_Balance = sc.nextInt();
acc_type=a_t;
p.println(name);
name = sc.nextLine();
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
}
} // end class
class create_account extends BankAccount {
create_account (String n,int b,String a_t){
name=n;
// name = sc.nextLine();
// p.println(name);
Acc_Balance=b;
// Acc_Balance=sc.nextInt();
// p.println(Acc_Balance);
acc_type=a_t;
}
create_account(){
super();
}
void insert(String n,String a_t){
name=n;
acc_type=a_t;
Acc_Balance=0;
}
void display_details(){
/*
try{
ServerSocket s1 = new ServerSocket(1342);
Socket s2 = s1.accept();
Scanner sc=new Scanner(s2.getInputStream());
PrintStream p = new PrintStream(s2.getOutputStream());
*/
System.out.println("Depositor Name :" +name);
System.out.println("Account Balance : "+Acc_Balance);
// p.println(Acc_Balance);
System.out.println("Account Type : "+acc_type);
// p.println(acc_type);
}
//deposit money
void deposit(int money){
Acc_Balance=money;
}
//withdraw money
int withdraw(int withd){
Acc_Balance=Acc_Balance-withd;
return Acc_Balance;
}
}
答案 0 :(得分:0)
首先,当您运行服务器时,它会等待客户端连接Socket s2 = s1.accept()
。
首先,您尝试从客户name = sc.nextLine()
获取名称。
正如我从客户端代码中看到的那样,您永远不会将名称传递给服务器。尝试重构您的代码以具有相同的编写和阅读顺序。这意味着,如果您在服务器端等待名称,那么您在客户端需要做的第一件事就是p.println(name)
。