package com.yc.chat1;
import java.io.DataInputStream;
public class Client2 implements Runnable{
protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
private Text text_3;
private String ip;
private Integer port;
private Socket sk;
private DataInputStream dis;
private DataOutputStream dos;
private boolean connected=false;
private String name;
private String tname;
private String msg;
private Text text_4;
private boolean duizhan;//对站是否开始
private Table table;
private Display display;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Client2 window = new Client2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(672, 366);
shell.setText("SWT Application");
Label label = new Label(shell, SWT.NONE);
label.setText("服务器地址:");
label.setBounds(20, 10, 61, 17);
text = new Text(shell, SWT.BORDER);
text.setText("127.0.0.1");
text.setBounds(95, 10, 79, 23);
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setBounds(199, 10, 24, 17);
lblNewLabel.setText("端口:");
text_1 = new Text(shell, SWT.BORDER);
text_1.setText("6668");
text_1.setBounds(238, 7, 73, 23);
Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setBounds(320, 10, 45, 17);
lblNewLabel_1.setText("用户名");
text_2 = new Text(shell, SWT.BORDER);
text_2.setBounds(371, 7, 73, 23);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setBounds(467, 5, 80, 27);
btnNewButton.setText("登录");
Label label_1 = new Label(shell, SWT.NONE);
label_1.setText("对战名称");
label_1.setBounds(20, 58, 61, 17);
text_3 = new Text(shell, SWT.BORDER);
text_3.setBounds(81, 58, 73, 23);
Label label_2 = new Label(shell, SWT.NONE);
label_2.setBounds(181, 58, 52, 17);
label_2.setText("您出什么:");
text_4 = new Text(shell, SWT.BORDER);
text_4.setBounds(238, 55, 73, 23);
Button button = new Button(shell, SWT.NONE);
button.setText("发送");
button.setBounds(344, 53, 80, 27);
Button button_1 = new Button(shell, SWT.NONE);
//选择对手
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
name=text_2.getText().trim();
tname=text_3.getText().trim();
if(tname==null||tname.equals("")){
MessageDialog.openWarning(shell, "警告提示", "你还没有选择对手");
}else{
try {
dos.writeUTF("login "+name+" "+tname);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
button_1.setBounds(467, 53, 80, 27);
button_1.setText("选择对手");
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(20, 104, 85, 150);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
tblclmnNewColumn.setWidth(80);
tblclmnNewColumn.setText("在线玩家");
Button btnNewButton_1 = new Button(shell, SWT.NONE);
btnNewButton_1.setBounds(566, 5, 80, 27);
btnNewButton_1.setText("退出");
//登录
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ip=text.getText().trim();
port=Integer.parseInt(text_1.getText().trim());
name=text_2.getText().trim();
tname=text_3.getText().trim();
/**
if(tname==null||tname.equals("")){
MessageDialog.openWarning(shell, "警告提示", "你还没有选择对手");
}*/
connect();
Thread t=new Thread(Client2.this);
t.start();
}
});
//发送
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println(msg);
if(msg.equals("对战开始")||msg.equals("对方接受挑战")){
//System.out.println(text_4.getText().trim());
if(text_4.getText().trim()!=null&&!text_4.getText().trim().equals("")){
try {
dos.writeUTF("1 "+text_4.getText().trim());
} catch (IOException e1) {
e1.printStackTrace();
}
}else{
MessageDialog.openWarning(shell, "温馨提示", "您还没有出拳,不能发送");
}
}
}
});
//退出
btnNewButton_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
dos.writeUTF("end "+name);
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connected=false;
}
});
}
public void connect(){
try {
sk=new Socket(ip,port);
//System.out.println(ip);
dis=new DataInputStream(sk.getInputStream());
dos=new DataOutputStream(sk.getOutputStream());
//System.out.println(name+"连接服务器成功....");
connected=true;
//dos.writeUTF("login "+name+" "+tname);
dos.writeUTF("login1 "+name);
} catch (Exception e) {
connected=false;
MessageDialog.openError(shell, "错误提示", "服务器连接失败");
}
}
@Override
public void run() {
while(connected){
try {
msg=dis.readUTF();
//System.out.println(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(msg.equals("对战开始")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openInformation(shell, "对战开始", "准备应战");
msg="对战开始";
}
});
}else if(msg.equals("您选择的对手还没有上线,请重新选择对手")){
System.out.println("ee");
}else if(msg.endsWith("要邀请你应战")){
display.asyncExec(new Runnable() {
public void run() {
boolean t=MessageDialog.openQuestion(shell, "是否应战", msg);
if(t){
try {
dos.writeUTF("应战");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
//dos.writeUTF("应战");
}else if(msg.equals("对方接受挑战")){
}else if(msg.equals("平局")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "平局");
}
});
msg="对战开始";
}else if(msg.equals("你输了")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "你输了,再接再励");
boolean t3=MessageDialog.openQuestion(shell, "是否继续", "yes/or");
if(t3){
msg="对战开始";
}
}
});
msg="对战开始";
}else if(msg.equals("你赢了")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "恭喜你赢了");
boolean t3=MessageDialog.openQuestion(shell, "是否继续", "yes/or");
if(t3){
msg="对战开始";
}
}
});
msg="对战开始";
}else if(msg.equals("你选择的对手已经开玩游戏,请重新选择对手")){
System.out.println("你选择的对手已经开玩游戏,请重新选择对手");
}else if(msg.equals("你还没有选择对手,请重新登陆")){
System.out.println("你还没有选择对手,请重新登陆");
connected=false;
}else if(msg.startsWith("OnLineUser ")){
display.asyncExec(new Runnable() {
@Override
public void run() {
String n;
table.removeAll();
n=msg.substring(msg.indexOf(" ")+1);
//System.out.println(msg);
String[] users=n.split(",");
TableItem ti=null;
for(int i=0,len=users.length;i<len;i++){
ti=new TableItem(table, SWT.NONE);
ti.setText(new String[]{users[i]});
}
msg="对战开始";
}
});
}else if(msg.startsWith("end ")){
break;
}
}
}
}
package com.yc.chat1;
import java.io.DataInputStream;
public class Server1 {
protected Shell shell;
private Text text;
private String port;
private boolean isRun;
private ServerSocket se;
//存放登录的用户对象
private List<OnLineClient> clients=new ArrayList<OnLineClient>();
//存放用户的姓名
private List<String> userList=new ArrayList<String>();
//存放对战双方
private Map<String,String> v=new LinkedHashMap<String, String>();
//存在每一个用户出的剪刀石头布
private Map<String,String> v1=new LinkedHashMap<String, String>();
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Server1 window = new Server1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(473, 339);
//shell.setText("6668");
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setBounds(10, 10, 57, 17);
lblNewLabel.setText("端口号:");
text = new Text(shell, SWT.BORDER);
text.setText("6668");
text.setBounds(60, 10, 91, 23);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setBounds(177, 10, 80, 27);
btnNewButton.setText("启动服务器");
Button btnNewButton_1 = new Button(shell, SWT.NONE);
btnNewButton_1.setBounds(293, 10, 80, 27);
btnNewButton_1.setText("关闭服务器");
//启动服务器
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
port=text.getText().trim();
new Thread(){
public void run() {
startServer();
};
}.start();
}
});
}
public void startServer(){
try {
se=new ServerSocket(Integer.parseInt(port));
isRun=true;
} catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
MessageDialog.openError(shell, "失败提示", "服务器连接失败");
}
//System.out.println(isRun);
while(isRun){
//System.out.println("服务器启动成功");
Socket s;
try {
s=se.accept();
System.out.println(s.getInetAddress().getHostAddress()+"连接上来了");
new Thread(new OnLineClient(s)).start();
} catch (IOException e) {
MessageDialog.openError(shell, "错误提示", "服务器崩溃");
isRun=false;
e.printStackTrace();
}
}
}
public class OnLineClient implements Runnable{
private Socket sc;
private String name;
private String tname;
private DataInputStream dis;
private DataOutputStream dos;
private boolean connected=false;
private String t;
public OnLineClient(Socket sc) {
try {
this.sc=sc;
dis=new DataInputStream(sc.getInputStream());
dos=new DataOutputStream(sc.getOutputStream());
connected=true;
} catch (IOException e) {
//MessageDialog.openError(shell, "失败提示", name+"与服务器连接失败");
e.printStackTrace();
}
}
public String getName() {
return name;
}
public String getTame() {
return tname;
}
public void setName(String name) {
this.name = name;
}
public void setTname(String tname) {
this.tname = tname;
}
public void send(String msg){
try {
dos.writeUTF(msg);
} catch (IOException e) {
connected=false;
clients.remove(this);
MessageDialog.openError(shell, "下线提示",name+"已经下线");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(connected){
String info=dis.readUTF();
System.out.println(info);
if(info.startsWith("login ")){
//"login "+name+" "+tname
name=info.substring(info.indexOf(" ")+1);
String[] s=name.split(" ");
//本身名称
name=s[0];
//对手名称
tname=s[1];
//clients.add(this);
//userList.add(s[0]);
//查看你的对手是否在线
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
//v 存放对战信息 张三 李四 ,张三 张三,李四
Set<String> s1=v.keySet();
//System.out.println(s1.size());
if(s1.size()>0){
for(String ss:s1){
if(ss.equals(tname)){
this.send("你选择的对手已经开玩游戏,请重新选择对手");
break;
}else{
//张三,李四
v.put(tname, name);
v.put(name, tname);
cl.send(name+"要邀请你应战");
break;
}
}
break;
}else{
v.put(tname, name);
v.put(name, tname);
cl.send(name+"要邀请你应战");
break;
}
}
}
}else if(info.equals("应战")){
for(OnLineClient cl:clients){
if(v.get(name).equals(cl.getName())){
this.setTname(cl.getName());
//System.out.println("//"+tname);
//System.out.println("//"+cl.getTame());
cl.send("对战开始");
break;
}
}
this.send("对战开始");
}else if(info.startsWith("1 ")){
String e=info.substring(info.indexOf(" ")+1);
v1.put(name, e);
String thisQ=v1.get(name);
String duiQ=v1.get(tname);
System.out.println(thisQ);
System.out.println(duiQ);
if(thisQ!=null&&!thisQ.equals("")&&duiQ!=null&&!duiQ.equals("")){
//System.out.println(thisQ);
//System.out.println("拉拉");
//System.out.println(duiQ);
if(thisQ.equals("石头")&&duiQ.equals("石头")){
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(name);
v1.remove(tname);//v1.remove(name);
cl.send("平局");
break;
}
}
}else if(thisQ.equals("石头")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("石头")&&duiQ.equals("布")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("石头")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("平局");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("布")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("石头")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("布")){
v1.remove(name);
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("平局");
break;
}
}
}
}
}else if(info.startsWith("login1 ")){
name=info.substring(info.indexOf(" ")+1);
String[] s=name.split(" ");
//本身名称
name=s[0];
clients.add(this);
userList.add(s[0]);
StringBuffer sbf=new StringBuffer();
for(String str:userList){
sbf.append(str+",");
}
//System.out.println(sbf.toString());
for(OnLineClient ct:clients){
ct.send("OnLineUser "+sbf.toString());
}
}else if(info.startsWith("end ")){
final String outName=info.substring(info.indexOf(" ")+1);
//把当前用户从在线用户列表中删除
clients.remove(this);
//把当前用户从userList中删除
userList.remove(outName);
//广播其他在线用户,当前用户已下线
StringBuffer sbf=new StringBuffer();
for(String str:userList){
sbf.append(str+",");
}
System.out.println(sbf.toString());
for(OnLineClient ct:clients){
ct.send("OnLineUser "+sbf.toString());
}
//通知用户下线
this.send("end ");
connected=false;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
public class Client2 implements Runnable{
protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
private Text text_3;
private String ip;
private Integer port;
private Socket sk;
private DataInputStream dis;
private DataOutputStream dos;
private boolean connected=false;
private String name;
private String tname;
private String msg;
private Text text_4;
private boolean duizhan;//对站是否开始
private Table table;
private Display display;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Client2 window = new Client2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(672, 366);
shell.setText("SWT Application");
Label label = new Label(shell, SWT.NONE);
label.setText("服务器地址:");
label.setBounds(20, 10, 61, 17);
text = new Text(shell, SWT.BORDER);
text.setText("127.0.0.1");
text.setBounds(95, 10, 79, 23);
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setBounds(199, 10, 24, 17);
lblNewLabel.setText("端口:");
text_1 = new Text(shell, SWT.BORDER);
text_1.setText("6668");
text_1.setBounds(238, 7, 73, 23);
Label lblNewLabel_1 = new Label(shell, SWT.NONE);
lblNewLabel_1.setBounds(320, 10, 45, 17);
lblNewLabel_1.setText("用户名");
text_2 = new Text(shell, SWT.BORDER);
text_2.setBounds(371, 7, 73, 23);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setBounds(467, 5, 80, 27);
btnNewButton.setText("登录");
Label label_1 = new Label(shell, SWT.NONE);
label_1.setText("对战名称");
label_1.setBounds(20, 58, 61, 17);
text_3 = new Text(shell, SWT.BORDER);
text_3.setBounds(81, 58, 73, 23);
Label label_2 = new Label(shell, SWT.NONE);
label_2.setBounds(181, 58, 52, 17);
label_2.setText("您出什么:");
text_4 = new Text(shell, SWT.BORDER);
text_4.setBounds(238, 55, 73, 23);
Button button = new Button(shell, SWT.NONE);
button.setText("发送");
button.setBounds(344, 53, 80, 27);
Button button_1 = new Button(shell, SWT.NONE);
//选择对手
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
name=text_2.getText().trim();
tname=text_3.getText().trim();
if(tname==null||tname.equals("")){
MessageDialog.openWarning(shell, "警告提示", "你还没有选择对手");
}else{
try {
dos.writeUTF("login "+name+" "+tname);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
button_1.setBounds(467, 53, 80, 27);
button_1.setText("选择对手");
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(20, 104, 85, 150);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
tblclmnNewColumn.setWidth(80);
tblclmnNewColumn.setText("在线玩家");
Button btnNewButton_1 = new Button(shell, SWT.NONE);
btnNewButton_1.setBounds(566, 5, 80, 27);
btnNewButton_1.setText("退出");
//登录
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ip=text.getText().trim();
port=Integer.parseInt(text_1.getText().trim());
name=text_2.getText().trim();
tname=text_3.getText().trim();
/**
if(tname==null||tname.equals("")){
MessageDialog.openWarning(shell, "警告提示", "你还没有选择对手");
}*/
connect();
Thread t=new Thread(Client2.this);
t.start();
}
});
//发送
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println(msg);
if(msg.equals("对战开始")||msg.equals("对方接受挑战")){
//System.out.println(text_4.getText().trim());
if(text_4.getText().trim()!=null&&!text_4.getText().trim().equals("")){
try {
dos.writeUTF("1 "+text_4.getText().trim());
} catch (IOException e1) {
e1.printStackTrace();
}
}else{
MessageDialog.openWarning(shell, "温馨提示", "您还没有出拳,不能发送");
}
}
}
});
//退出
btnNewButton_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
dos.writeUTF("end "+name);
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connected=false;
}
});
}
public void connect(){
try {
sk=new Socket(ip,port);
//System.out.println(ip);
dis=new DataInputStream(sk.getInputStream());
dos=new DataOutputStream(sk.getOutputStream());
//System.out.println(name+"连接服务器成功....");
connected=true;
//dos.writeUTF("login "+name+" "+tname);
dos.writeUTF("login1 "+name);
} catch (Exception e) {
connected=false;
MessageDialog.openError(shell, "错误提示", "服务器连接失败");
}
}
@Override
public void run() {
while(connected){
try {
msg=dis.readUTF();
//System.out.println(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(msg.equals("对战开始")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openInformation(shell, "对战开始", "准备应战");
msg="对战开始";
}
});
}else if(msg.equals("您选择的对手还没有上线,请重新选择对手")){
System.out.println("ee");
}else if(msg.endsWith("要邀请你应战")){
display.asyncExec(new Runnable() {
public void run() {
boolean t=MessageDialog.openQuestion(shell, "是否应战", msg);
if(t){
try {
dos.writeUTF("应战");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
//dos.writeUTF("应战");
}else if(msg.equals("对方接受挑战")){
}else if(msg.equals("平局")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "平局");
}
});
msg="对战开始";
}else if(msg.equals("你输了")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "你输了,再接再励");
boolean t3=MessageDialog.openQuestion(shell, "是否继续", "yes/or");
if(t3){
msg="对战开始";
}
}
});
msg="对战开始";
}else if(msg.equals("你赢了")){
display.asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openConfirm(shell, "结果", "恭喜你赢了");
boolean t3=MessageDialog.openQuestion(shell, "是否继续", "yes/or");
if(t3){
msg="对战开始";
}
}
});
msg="对战开始";
}else if(msg.equals("你选择的对手已经开玩游戏,请重新选择对手")){
System.out.println("你选择的对手已经开玩游戏,请重新选择对手");
}else if(msg.equals("你还没有选择对手,请重新登陆")){
System.out.println("你还没有选择对手,请重新登陆");
connected=false;
}else if(msg.startsWith("OnLineUser ")){
display.asyncExec(new Runnable() {
@Override
public void run() {
String n;
table.removeAll();
n=msg.substring(msg.indexOf(" ")+1);
//System.out.println(msg);
String[] users=n.split(",");
TableItem ti=null;
for(int i=0,len=users.length;i<len;i++){
ti=new TableItem(table, SWT.NONE);
ti.setText(new String[]{users[i]});
}
msg="对战开始";
}
});
}else if(msg.startsWith("end ")){
break;
}
}
}
}
package com.yc.chat1;
import java.io.DataInputStream;
public class Server1 {
protected Shell shell;
private Text text;
private String port;
private boolean isRun;
private ServerSocket se;
//存放登录的用户对象
private List<OnLineClient> clients=new ArrayList<OnLineClient>();
//存放用户的姓名
private List<String> userList=new ArrayList<String>();
//存放对战双方
private Map<String,String> v=new LinkedHashMap<String, String>();
//存在每一个用户出的剪刀石头布
private Map<String,String> v1=new LinkedHashMap<String, String>();
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Server1 window = new Server1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(473, 339);
//shell.setText("6668");
Label lblNewLabel = new Label(shell, SWT.NONE);
lblNewLabel.setBounds(10, 10, 57, 17);
lblNewLabel.setText("端口号:");
text = new Text(shell, SWT.BORDER);
text.setText("6668");
text.setBounds(60, 10, 91, 23);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setBounds(177, 10, 80, 27);
btnNewButton.setText("启动服务器");
Button btnNewButton_1 = new Button(shell, SWT.NONE);
btnNewButton_1.setBounds(293, 10, 80, 27);
btnNewButton_1.setText("关闭服务器");
//启动服务器
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
port=text.getText().trim();
new Thread(){
public void run() {
startServer();
};
}.start();
}
});
}
public void startServer(){
try {
se=new ServerSocket(Integer.parseInt(port));
isRun=true;
} catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
MessageDialog.openError(shell, "失败提示", "服务器连接失败");
}
//System.out.println(isRun);
while(isRun){
//System.out.println("服务器启动成功");
Socket s;
try {
s=se.accept();
System.out.println(s.getInetAddress().getHostAddress()+"连接上来了");
new Thread(new OnLineClient(s)).start();
} catch (IOException e) {
MessageDialog.openError(shell, "错误提示", "服务器崩溃");
isRun=false;
e.printStackTrace();
}
}
}
public class OnLineClient implements Runnable{
private Socket sc;
private String name;
private String tname;
private DataInputStream dis;
private DataOutputStream dos;
private boolean connected=false;
private String t;
public OnLineClient(Socket sc) {
try {
this.sc=sc;
dis=new DataInputStream(sc.getInputStream());
dos=new DataOutputStream(sc.getOutputStream());
connected=true;
} catch (IOException e) {
//MessageDialog.openError(shell, "失败提示", name+"与服务器连接失败");
e.printStackTrace();
}
}
public String getName() {
return name;
}
public String getTame() {
return tname;
}
public void setName(String name) {
this.name = name;
}
public void setTname(String tname) {
this.tname = tname;
}
public void send(String msg){
try {
dos.writeUTF(msg);
} catch (IOException e) {
connected=false;
clients.remove(this);
MessageDialog.openError(shell, "下线提示",name+"已经下线");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(connected){
String info=dis.readUTF();
System.out.println(info);
if(info.startsWith("login ")){
//"login "+name+" "+tname
name=info.substring(info.indexOf(" ")+1);
String[] s=name.split(" ");
//本身名称
name=s[0];
//对手名称
tname=s[1];
//clients.add(this);
//userList.add(s[0]);
//查看你的对手是否在线
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
//v 存放对战信息 张三 李四 ,张三 张三,李四
Set<String> s1=v.keySet();
//System.out.println(s1.size());
if(s1.size()>0){
for(String ss:s1){
if(ss.equals(tname)){
this.send("你选择的对手已经开玩游戏,请重新选择对手");
break;
}else{
//张三,李四
v.put(tname, name);
v.put(name, tname);
cl.send(name+"要邀请你应战");
break;
}
}
break;
}else{
v.put(tname, name);
v.put(name, tname);
cl.send(name+"要邀请你应战");
break;
}
}
}
}else if(info.equals("应战")){
for(OnLineClient cl:clients){
if(v.get(name).equals(cl.getName())){
this.setTname(cl.getName());
//System.out.println("//"+tname);
//System.out.println("//"+cl.getTame());
cl.send("对战开始");
break;
}
}
this.send("对战开始");
}else if(info.startsWith("1 ")){
String e=info.substring(info.indexOf(" ")+1);
v1.put(name, e);
String thisQ=v1.get(name);
String duiQ=v1.get(tname);
System.out.println(thisQ);
System.out.println(duiQ);
if(thisQ!=null&&!thisQ.equals("")&&duiQ!=null&&!duiQ.equals("")){
//System.out.println(thisQ);
//System.out.println("拉拉");
//System.out.println(duiQ);
if(thisQ.equals("石头")&&duiQ.equals("石头")){
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(name);
v1.remove(tname);//v1.remove(name);
cl.send("平局");
break;
}
}
}else if(thisQ.equals("石头")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("石头")&&duiQ.equals("布")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("石头")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("平局");
break;
}
}
}else if(thisQ.equals("剪刀")&&duiQ.equals("布")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("石头")){
v1.remove(name);
this.send("你赢了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你输了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("剪刀")){
v1.remove(name);
this.send("你输了");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("你赢了");
break;
}
}
}else if(thisQ.equals("布")&&duiQ.equals("布")){
v1.remove(name);
this.send("平局");
for(OnLineClient cl:clients){
if(cl.getName().equals(tname)){
v1.remove(tname);
cl.send("平局");
break;
}
}
}
}
}else if(info.startsWith("login1 ")){
name=info.substring(info.indexOf(" ")+1);
String[] s=name.split(" ");
//本身名称
name=s[0];
clients.add(this);
userList.add(s[0]);
StringBuffer sbf=new StringBuffer();
for(String str:userList){
sbf.append(str+",");
}
//System.out.println(sbf.toString());
for(OnLineClient ct:clients){
ct.send("OnLineUser "+sbf.toString());
}
}else if(info.startsWith("end ")){
final String outName=info.substring(info.indexOf(" ")+1);
//把当前用户从在线用户列表中删除
clients.remove(this);
//把当前用户从userList中删除
userList.remove(outName);
//广播其他在线用户,当前用户已下线
StringBuffer sbf=new StringBuffer();
for(String str:userList){
sbf.append(str+",");
}
System.out.println(sbf.toString());
for(OnLineClient ct:clients){
ct.send("OnLineUser "+sbf.toString());
}
//通知用户下线
this.send("end ");
connected=false;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}