Java 使用套接字的客户机/服务器编程
最近,我接到一个任务,我必须构建一个计费服务的分布式应用程序,其中包括一个客户端和一个服务器。我已经在NetBeans IDE 7.2.1上完成了完整的程序。客户端和服务器的代码如下所示 服务器端:Java 使用套接字的客户机/服务器编程,java,sockets,Java,Sockets,最近,我接到一个任务,我必须构建一个计费服务的分布式应用程序,其中包括一个客户端和一个服务器。我已经在NetBeans IDE 7.2.1上完成了完整的程序。客户端和服务器的代码如下所示 服务器端: import java.io.*; import java.net.*; import java.util.*; import java.text.*; import java.math.BigDecimal; public class OBS_Server_11398 implements Ru
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.math.BigDecimal;
public class OBS_Server_11398 implements Runnable {
Socket OBS_Socket = null;
public Hashtable < String, BigDecimal > OBS_Billing_Table;
public Hashtable < String, String > Password_Table;
public OBS_Server_11398(Socket OBS_Socket, Hashtable < String, BigDecimal > OBS_Billing_Table,
Hashtable < String, String > Password_Table) {
this.OBS_Socket = OBS_Socket;
this.OBS_Billing_Table = OBS_Billing_Table;
this.Password_Table = Password_Table;
}
public void run() {
System.out.println("Connected to Client...");
// connection between server and client
PrintWriter pwd = null;
boolean chFlag = true;
try {
int ans = 0;
Scanner sc = new Scanner(OBS_Socket.getInputStream());
pwd = new PrintWriter(OBS_Socket.getOutputStream(), true);
String OBS_Password = sc.nextLine();
System.out.println("Account Password :" + OBS_Password);
// The Account is obtained from the table
boolean Password_Exists = Password_Table.containsKey(OBS_Password);
if (Password_Exists) {
pwd.println("Successful");
} else {
pwd.println("Failed");
}
do {
BigDecimal amount = new BigDecimal(0);
String passwd = null;
String OBS_Account = Password_Table.get(OBS_Password);
BigDecimal balance = (BigDecimal) OBS_Billing_Table.get(OBS_Account);
Scanner sc1 = new Scanner(OBS_Socket.getInputStream());
Date now;
now = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
String Current_Date = df.format(now);
String chSplit = sc1.nextLine();
int Option = 0;
if (chSplit.indexOf("-") != -1) {
String[] choiceObs = chSplit.split("-");
Option = Integer.parseInt(choiceObs[0]);
System.out.println("Enter your Option: " + Option);
System.out.println(choiceObs[1]);
if (Option == 4 || Option == 5) {
passwd = choiceObs[1];
} else {
amount = new BigDecimal(choiceObs[1]);
}
}
PrintWriter pw1 = new PrintWriter(OBS_Socket.getOutputStream(), true);
switch (Option) {
case 1:
System.out.println("\n * * * * VIEW BILL * * * * ");
// of OBS_Account
pw1.println("Your Current bill as of " + Current_Date + " is: OMR " + balance.setScale(3, BigDecimal.ROUND_HALF_UP));
break;
case 2:
System.out.println("\n * * * * PAY BILL * * * * ");
pw1.println(balance.setScale(3, BigDecimal.ROUND_HALF_UP));
OBS_Billing_Table.put(OBS_Account, new BigDecimal(sc1.nextLine()));
break;
case 3:
System.out.println("\n * * * * CHECK BALANCE * * * * ");
pw1.println("Your Balance Amount as of " + Current_Date + " is: OMR " + balance.setScale(3, BigDecimal.ROUND_HALF_UP));
break;
case 4:
System.out.println("** Change Password **");
Password_Table.put(passwd, OBS_Account);
Password_Table.remove(OBS_Password);
OBS_Password = passwd;
pw1.println("Your OBS_Account password is successfully changed!");
break;
case 5:
chFlag = false;
pw1.println("Exit Successfully..");
break;
default:
// choice Flag = true;
System.out.println("****************************************");
System.out.println("** Wrong Choice! Please try again! **");
System.out.println("****************************************");
}
} while (chFlag);
} catch (Exception e) {
// e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
System.out.println("Waiting for Client to Connect......");
ServerSocket ss = new ServerSocket(5555);
Socket OBS_Socket = null;
Hashtable < String, String > Password_Table = new Hashtable < String, String > ();
Hashtable < String, BigDecimal > OBS_Billing_Table = new Hashtable < String, BigDecimal > ();
Password_Table.put("1234", "AA1234");
Password_Table.put("5678", "BB5678");
OBS_Billing_Table.put("AA1234", new BigDecimal(5000));
OBS_Billing_Table.put("BB5678", new BigDecimal(2500));
while (true) {
OBS_Socket = ss.accept();
OBS_Server_11398 ast = new OBS_Server_11398(OBS_Socket, OBS_Billing_Table,
Password_Table);
Thread t = new Thread(ast);
t.start();
}
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
导入java.text.*;
导入java.math.BigDecimal;
公共类OBS_服务器_11398实现可运行{
套接字OBS_Socket=null;
公共哈希表OBS\u Billing\u表;
公共哈希表<字符串,字符串>密码\u表;
公共OBS_服务器_11398(套接字OBS_套接字,哈希表<字符串,BigDecimal>OBS_计费表,
HashtablePassword\u表){
this.OBS_Socket=OBS_Socket;
this.OBS\u Billing\u Table=OBS\u Billing\u Table;
this.Password\u Table=密码\u Table;
}
公开募捐{
System.out.println(“连接到客户端…”);
//服务器和客户端之间的连接
PrintWriter pwd=null;
布尔chFlag=真;
试一试{
int ans=0;
Scanner sc=新扫描仪(OBS_Socket.getInputStream());
pwd=新的PrintWriter(OBS_Socket.getOutputStream(),true);
字符串OBS_Password=sc.nextLine();
System.out.println(“帐户密码:“+OBS_密码”);
//账目是从表中获得的
布尔密码_Exists=密码_Table.containsKey(OBS_密码);
如果(密码_存在){
pwd.println(“成功”);
}否则{
pwd.println(“失败”);
}
做{
BigDecimal金额=新的BigDecimal(0);
字符串passwd=null;
字符串OBS\u Account=Password\u Table.get(OBS\u Password);
BigDecimal余额=(BigDecimal)OBS\U账单表.get(OBS\U账户);
Scanner sc1=新扫描仪(OBS_Socket.getInputStream());
日期现在;
现在=新日期();
DateFormat df=DateFormat.getDateInstance(DateFormat.LONG);
字符串Current_Date=df.format(现在);
字符串chSplit=sc1.nextLine();
int选项=0;
if(chSplit.indexOf(“-”)=-1){
字符串[]choiceObs=chSplit.split(“-”);
Option=Integer.parseInt(choiceObs[0]);
System.out.println(“输入您的选项:“+选项”);
System.out.println(choiceObs[1]);
如果(选项==4 | |选项==5){
passwd=choiceObs[1];
}否则{
金额=新的BigDecimal(choiceObs[1]);
}
}
PrintWriter pw1=新的PrintWriter(OBS_Socket.getOutputStream(),true);
开关(选件){
案例1:
System.out.println(“\n****查看账单****”);
//OBS_账户的管理
pw1.println(“您截至“+当前日期+”的当前账单是:OMR”+余额.setScale(3,BigDecimal.ROUND_-HALF_UP));
打破
案例2:
System.out.println(“\n****支付账单****”);
pw1.println(天平.设置刻度(3,大十进制.四舍五入半向上));
OBS_Billing_Table.put(OBS_账户,新的BigDecimal(sc1.nextLine());
打破
案例3:
System.out.println(“\n****检查余额****”);
pw1.println(“您截至“+当前日期+”的余额金额为:OMR”+Balance.setScale(3,BigDecimal.ROUND_-HALF_UP));
打破
案例4:
System.out.println(“**更改密码**”);
密码表put(密码、OBS账户);
密码表。删除(OBS密码);
OBS_Password=passwd;
println(“您的OBS_帐户密码已成功更改!”);
打破
案例5:
chFlag=false;
pw1.println(“成功退出”);
打破
违约:
//选择标志=真;
System.out.println(“**********************************************************”);
System.out.println(“**选择错误!请重试!**”);
System.out.println(“**********************************************************”);
}
}while(chFlag);
}捕获(例外e){
//e.printStackTrace();
}
}
公共静态void main(字符串args[])引发异常{
System.out.println(“等待客户端连接…”);
ServerSocket ss=新的ServerSocket(5555);
套接字OBS_Socket=null;
HashtablePassword_Table=新的Hashtable();
HashtableOBS_Billing_Table=新的Hashtable();
密码_Table.put(“1234”、“AA1234”);
密码_Table.put(“5678”、“BB5678”);
OBS_Billing_Table.put(“AA1234”,新的BigDecimal(5000));
OBS_Billing_Table.put(“BB5678”,新的大十进制(2500));
while(true){
OBS_Socket=ss.accept();
OBS_服务器_11398 ast=新的OBS_服务器_11398(OBS_插槽、OBS_计费表、,
密码(U表);
螺纹t=新螺纹(ast);
t、 start();
}
}
}
客户端:
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;
public class OBS_Client_11398 {
public static void main(String args[]) throws Exception {
Socket OBS_Socket;
boolean acFlag = false;
boolean pwdFlag = false;
String strpasswd = null;
int passwd = 0;
Scanner scn = new Scanner(System.in);
PrintWriter pwd = null;
Scanner sc = null;
String res = null;
do {
acFlag = false;
do {
pwdFlag = false;
try {
System.out.println(" * * * * OMANTEL BILLING SERVICE * * * * ");
System.out.println("\nPlease enter your OBS Account Number : ");
strpasswd = scn.nextLine();
passwd = Integer.parseInt(strpasswd);
if (strpasswd.length() != 4) {
// System.out.println(strpin.length());
pwdFlag = true;
}
} catch (Exception e) {
// e.printStackTrace();
System.out.println("Error! Please Enter a Password with 4 Digits Only..");
pwdFlag = true;
}
} while (pwdFlag);
OBS_Socket = new Socket("localhost", 5555);
pwd = new PrintWriter(OBS_Socket.getOutputStream(), true);
pwd.println(strpasswd);
sc = new Scanner(OBS_Socket.getInputStream());
res = sc.next();
if (res.equals("Failed")) {
System.out.println("ERROR! You have Entered an Invalid Account Number");
acFlag = true;
}
} while (acFlag);
if (res.equals("Successful")) {
boolean chFlag = false;
do {
chFlag = false;
System.out.println("\n * - - - - - - - - - - - - - - - - - *");
System.out.println(" * *");
System.out.println(" * WELCOME TO OMANTEL BILLING SERVICE *");
System.out.println(" * Programmed by: Sania Macci - 11398 *");
System.out.println(" * *");
System.out.println(" * Choose an Option from the list below *");
System.out.println(" * --> 1 <-- View Bill *");
System.out.println(" * --> 2 <-- Pay Bill *");
System.out.println(" * --> 3 <-- Check Balance *");
System.out.println(" * --> 4 <-- Change Password *");
System.out.println(" * --> 5 <-- Exit *");
System.out.println(" * *");
System.out.println(" * - - - - - - - - - - - - - - - - - *");
System.out.print("\nSelected Option : ");
Scanner sc1 = new Scanner(System.in);
int ch = 0;
try {
Scanner scCH = new Scanner(OBS_Socket.getInputStream());
PrintWriter pw1 = new PrintWriter(OBS_Socket.getOutputStream(),
true);
ch = sc1.nextInt();
System.out.println(ch);
if (ch == 1) {
scCH = new Scanner(OBS_Socket.getInputStream());
pw1.println(ch + "-" + "0");
System.out.println("");
System.out.println(" * * * * VIEW BILL * * * * ");
System.out.println("This bill will be Updated after every Transaction");
System.out.println("");
System.out.print(scCH.nextLine());
} else if (ch == 2) {
scCH = new Scanner(OBS_Socket.getInputStream());
pw1.println(ch + "-" + "2");
System.out.println("");
System.out.println(" * * * * PAY BILL * * * * ");
BigDecimal totalBill = new BigDecimal(scCH.nextLine());
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Amount to Pay: ");
BigDecimal amount = new BigDecimal(scanner.nextLine());
totalBill = totalBill.subtract(amount, new MathContext(5));
pw1.println(totalBill);
} else if (ch == 3) {
pw1.println(ch + "-" + "3");
System.out.println("");
System.out.println(" * * * * CHECK BALANCE * * * * ");
scCH = new Scanner(OBS_Socket.getInputStream());
System.out.print(scCH.nextLine());
} else if (ch == 4) {
scCH = new Scanner(OBS_Socket.getInputStream());
System.out.print("Please Enter a New Password: ");
int pinNum = sc1.nextInt();
pw1.println(ch + "-" + pinNum);
System.out.print(scCH.nextLine());
} else if (ch == 5) {
pw1.println(ch + "-" + "0");
scCH = new Scanner(OBS_Socket.getInputStream());
System.out.print(scCH.nextLine());
System.exit(-1);
} else {
System.out.print("WRONG CHOICE!! Please try again...\n");
chFlag = true;
}
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
if (readString.equals("")) {
chFlag = true;
}
} catch (java.util.InputMismatchException e) {
System.out.println("Wrong Choice! Please try again\n");
chFlag = true;
} catch (Exception e) { // e.printStackTrace();
}
} while (chFlag);
}
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
导入java.math.*;
公共类OBS_客户机_11398{
公共静态void main(字符串args[])引发异常{