【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了ClientConnection.java相关资料,希望对各位网友有所帮助!
package cardserver;
import javax.microedition.io.SocketConnection;
/**
* <p>Title: CardServer</p>
*
* <p>Description: lizhenpeng</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: LP&P</p>
*
* @author lipeng
* @version 1.0
*/
import java.io.*;
import javax.microedition.io.*;
import java.util.*;
public class ClientConnection
implements Runnable
{
public SocketConnection socket;
boolean isRunning;
InputStream is;
public OutputStream os;
public ClientConnection Oppo;
DataHead recvDataHead=new DataHead();
DataHead sendDataHead=new DataHead();
byte[] recvBuffer;
byte[] sendBuffer;
byte[] recvHeadBuffer=new byte[4];
public String userName;
Card [] totalCard = new Card[55];
public ClientConnection(SocketConnection sock)
{
isRunning=true;
socket=sock;
try
{
is=socket.openInputStream();
os=socket.openOutputStream();
} catch(Exception e)
{
}
}
public void run()
{
while(isRunning)
{
try
{
is.read(recvHeadBuffer,0,4);
recvDataHead.FillData(recvHeadBuffer,0);
if(recvDataHead.size!=0)
{
// socket.get
recvBuffer=new byte[recvDataHead.size];
is.read(recvBuffer,0,recvDataHead.size);
//处理数据
ProcessData();
} else
{
//处理信息
ProcessData();
}
} catch(Exception e)
{
System.out.println(e);
isRunning = false;
break;
}
}
MainForm.inst.userList.removeElement(this);
}
public void ProcessData()
{
switch(recvDataHead.command)
{
//发送本次产生的唯一的ID
case NetProtocol.LOGIN:
{
try
{
userName=new String(this.recvBuffer);
int port;
int size=MainForm.inst.userList.size();
byte[] buffer=new byte[32*(size-1)];
int place=0;
for(int i=0;i<size;i++)
{
ClientConnection client=(ClientConnection)MainForm.inst.userList.
elementAt(i);
if(client!=this)
{
byte[] nameBuffer=client.userName.getBytes();
System.arraycopy(nameBuffer,0,buffer,place,nameBuffer.length);
place+=32;
}
}
this.sendDataHead.command=NetProtocol.HASLOGIN;
sendDataHead.size=(char)buffer.length;
this.sendBuffer=sendDataHead.getBytes();
os.write(sendBuffer,0,sendBuffer.length);
os.write(buffer,0,buffer.length);
os.flush();
} catch(Exception e)
{
System.out.println(e);
}
}
break;
case NetProtocol.REQUEST_STARTGAME:
{
String name=new String(this.recvBuffer);
int size=MainForm.inst.userList.size();
for(int i=0;i<size;i++)
{
ClientConnection client=(ClientConnection)MainForm.inst.userList.
elementAt(i);
if(client.userName.compareTo(name)==0)
{
this.Oppo = client;
client.Oppo = this;
mixCard();
try
{
byte[] buffer1=new byte[4*54/2];
byte[] buffer2=new byte[4*54/2];
int place=0;
for(i=1;i<28;i++)
{
System.arraycopy(totalCard[i].getBytes(),0,buffer1,place,4);
place+=4;
}
place=0;
for(i=28;i<54;i++)
{
System.arraycopy(totalCard[i].getBytes(),0,buffer2,place,4);
place+=4;
}
sendDataHead.command=NetProtocol.STARTGAME_FIRST;
sendDataHead.size=(char)(buffer1.length+buffer2.length);
sendBuffer=sendDataHead.getBytes();
os.write(sendBuffer,0,sendBuffer.length);
os.write(buffer1,0,buffer1.length);
os.write(buffer2,0,buffer2.length);
Oppo.sendDataHead.command = NetProtocol.STARTGAME_LAST;
Oppo.sendDataHead.size = (char)(buffer2.length+buffer1.length);
Oppo.sendBuffer = Oppo.sendDataHead.getBytes();
Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
Oppo.os.write(buffer2,0,buffer2.length);
Oppo.os.write(buffer1,0,buffer1.length);
//改变用户状态为游戏中
//通知开始
//发牌
//先后顺序
}
catch(Exception e)
{
System.out.print(e);
}
break;
}
}
}
break;
case NetProtocol.POST_CARD:
{
try
{
this.Oppo.sendDataHead.command=NetProtocol.OPPO_POSTCARD;
this.Oppo.sendDataHead.size=(char)recvBuffer.length;
this.Oppo.sendBuffer=Oppo.sendDataHead.getBytes();
Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
this.Oppo.os.write(this.recvBuffer,0,this.recvBuffer.length);
}
catch(Exception e)
{
System.out.print(e);
}
}
break;
case NetProtocol.REJECT_CARD:
{
try
{
this.Oppo.sendDataHead.command=NetProtocol.OPPO_REJECT;
this.Oppo.sendDataHead.size=0;
this.Oppo.sendBuffer=Oppo.sendDataHead.getBytes();
Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
}
catch(Exception e)
{
System.out.print(e);
}
}
break;
}
}
void mixCard()
{
initCard();
Random rand=new Random();
for(int i=1;i<totalCard.length;i++)
{
int order = Math.abs(rand.nextInt())%(totalCard.length-1)+1;
Card temp=totalCard[i];
totalCard[i]=totalCard[order];
totalCard[order]=temp;
}
}
/*
id 为1-54
kind 为1-5
1为红心
2为黑桃
3为方块
4位梅花
5为王
num为 1-13
*/
void initCard()
{
for(int i=0;i<totalCard.length;i++)
{
totalCard[i]=new Card();
totalCard[i].id = (byte)(i+1);
}
for(int i=1;i<14;i++)
{
totalCard[i].kind=1;
totalCard[i].num=(byte)i;
}
for(int i=14;i<27;i++)
{
totalCard[i].kind=2;
totalCard[i].num=(byte)(i-13);
}
for(int i=27;i<40;i++)
{
totalCard[i].kind=3;
totalCard[i].num=(byte)(i-26);
}
for(int i=40;i<53;i++)
{
totalCard[i].kind=4;
totalCard[i].num=(byte)(i-39);
}
totalCard[53].kind = 5;
totalCard[53].num = 0;
totalCard[54].kind = 5;
totalCard[54].num = 1;
}
}
上一篇:
ClientConfig.java
下一篇:
法律专业开题报告范文