socket服务器端

package server.model;

 

import java.awt.HeadlessException;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Date;

 

 

import server.ServerPanel;

 

public class ServerSocketUtil {

public static boolean isExit = false;

static ServerSocket server = null;

public static void startServer(int port) throws IOException {

server = new ServerSocket(port);

ServerSocketThread serverThread = new ServerSocketThread(server);

serverThread.start();

}

public static void closeServer() throws IOException {

if(ServerSocketThread.socket != null) ServerSocketThread.socket.close();

if(server == null) return;

server.close();

}

}

 

class ServerSocketThread extends Thread {

ServerSocket server = null;

static Socket socket = null;

 

public ServerSocketThread(ServerSocket server) {

this.server = server;

}

 

@Override

public void run() {

while(true) {

try {

socket = server.accept();

new Thread() {

public void run() {

try {

ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));

ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));

while(true) {

NetMsg msg = (NetMsg) ois.readObject();

System.out.println("hhhhhhhhhhhh"+msg.getContent());

switch(msg.getType()) {

case NetMsg.TYPE_FIND_GOODS: {

String goodsId = (String)msg.getContent();

Goods goods = GoodsMgr.findGoods(goodsId);

NetMsg tempMsg = new NetMsg(NetMsg.TYPE_FIND_GOODS_REPLY,goods);

oos.writeObject(tempMsg);

oos.flush();

break;

}

case NetMsg.TYPE_LOGIN: {

String tempStr = (String) msg.getContent();

String[] str = tempStr.split(",");

String cushierId = str[0];

String cushierPwd = str[1];

Cushier tempCushier = new Cushier(cushierId);

int k = CushierMgr.getCushierList().indexOf(tempCushier);

NetMsg tempMsg3 = null;

if(k == -1) {

//System.out.println("未找到用户!");

tempMsg3 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, "wrongid");

oos.writeObject(tempMsg3);

oos.flush();

continue;

}

Cushier cushier = CushierMgr.getCushierList().get(k);

if(cushier.getPassword().equals(cushierPwd)) {

NetMsg tempMsg2 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, cushier);

ServerPanel.count++;

ServerPanel.labCushierCount.setText(ServerPanel.count+"人在线");

oos.writeObject(tempMsg2);

oos.flush();

} else {

tempMsg3 = new NetMsg(NetMsg.TYPE_LOGIN_REPLY, "wrongpwd");

oos.writeObject(tempMsg3);

oos.flush();

}

break;

}

case NetMsg.TYPE_PAY:

double money = (Double)msg.getContent();

//System.out.println("money"+money);

MoneyMgr.add(money);

String date = MoneyMgr.sdf.format(new Date())+"";

if(MoneyMgr.getMoneyMap().size()>0 && MoneyMgr.getMoneyMap().get(MoneyMgr.sdf.format(new Date())) != null) {

ServerPanel.labDayTurnover.setText("¥"+(MoneyMgr.getMoneyMap().get(date))+"元");

} else {

ServerPanel.labDayTurnover.setText("¥ 0 元");

}

break;

}

}

} catch (HeadlessException e) {

e.printStackTrace();

} catch (IOException e) {

//e.printStackTrace();

//一个客户端退出系统时,让主界面在线的人数减一

if(ServerPanel.count>0) {

ServerPanel.count--;

ServerPanel.labCushierCount.setText(ServerPanel.count+"人在线");

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}.start();

} catch (IOException e) {

//e.printStackTrace();

//System.out.println("服务已停止!");

ServerPanel.labServerInfo.setText("未启动");

return;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值