概述
服务器IP:172.0.0.1
端口号:4331
客户端模版:
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientItem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Socket clientSocket=null;
DataInputStream inData=null;
DataOutputStream outData=null;
Thread thread;
Read read=null;
try {clientSocket=new Socket();
read =new Read();
thread =new Thread(read);
System.out.print("输入服务器的IP:");
String IP= scanner.nextLine();
System.out.print("输入端口号:");
int port=scanner.nextInt();
String enter=scanner.nextLine();
if(clientSocket.isConnected()) {}
else {
InetAddress address=InetAddress.getByName(IP);
InetSocketAddress socketAddress=new InetSocketAddress(address,port);
clientSocket.connect(socketAddress);
InputStream in=clientSocket.getInputStream();
OutputStream out=clientSocket.getOutputStream();
inData=new DataInputStream(in);
outData =new DataOutputStream(out);
read.setDataInputStream(inData);
read.setDataOutputStream(outData);
thread.start();
}
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
}
}
class Read implements Runnable{
Scanner scanner =new Scanner(System.in);
DataInputStream in;
DataOutputStream out;
public
void setDataInputStream(DataInputStream in){
this.in=in;
}
public void setDataOutputStream(DataOutputStream out) {
this.out=out;
}
public void run() {
System.out.println("输入账单:");
String content =scanner.nextLine();
try {
out.writeUTF("账单"+content);
String str=in.readUTF();
System.out.println(str);
str=in.readUTF();
System.out.println(str);
str=in.readUTF();
System.out.println(str);
}
catch(Exception e) {}
}
}
服务器模版:
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerItem {
public static void main(String args[]) {
ServerSocket server=null;
ServerThread thread;
Socket you=null;
while(true) {
try {server=new ServerSocket(4331);
}
catch(IOException e1) {
System.out.println("正在监听");
}
try {System.out.println("正在等待客户");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch(IOException e) {
System.out.println(""+e);
}
if(you!=null) {
new ServerThread(you).start();
}
}
}
}
class ServerThread extends Thread{
Socket socket;
DataInputStream in=null;
DataOutputStream out=null;
ServerThread(Socket t){
socket=t;
try {out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
}
catch(IOException e) {}
}
public void run() {
try {
String item=in.readUTF();
Scanner scanner=new Scanner(item);
scanner.useDelimiter("[^0123456789.]+");
if(item.startsWith("账单")) {
double sum=0;
while(scanner.hasNext()) {
try {double price =scanner.nextDouble();
sum=sum+price;
System.out.println(price);
}
catch(InputMismatchException exp) {
String t=scanner.next();
}
}
out.writeUTF("您的账单:");
out.writeUTF(item);
out.writeUTF("总额:"+sum+"元");
}
}
catch(Exception exp) {}
}
}
最后
以上就是默默指甲油为你收集整理的会结账的服务器的全部内容,希望文章能够帮你解决会结账的服务器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复