📄 tanke.txt
字号:
// do nothing
}
// if another states, then multicast messages
else {
sndPacket = new DatagramPacket(inputMessage.getBytes(),
inputMessage.getBytes().length,UDPServer,
new Parameter().UDPServerPort);
UDPPort.send(sndPacket);
}
inputMessage = null;
}
}
}catch(IOException ioe){
if ( cSocket.isClosed() == false ){
finalize();
}
else{
ioe.printStackTrace();
}
}
}
/**
* end of thread, then disconnection with client
*/
public void finalize(){
if ( cSocket.isClosed() == false ){
try{
inputMessage = "bye" + "||" + userName;
sndPacket = new DatagramPacket(inputMessage.getBytes(),
inputMessage.getBytes().length,UDPServer,
new Parameter().UDPServerPort);
UDPPort.send(sndPacket);
startThread = false;
ServerControlFrame.outputInformation.append(userName+" disconnect it\n");
in.close();
cSocket.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
cServer = null;
}
public String getUserName(){
return userName;
}
}
package TankBattle;
import java.lang.*;
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: Tank Game</p>
* <p>Description: SDIA csc829</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Uni. Of Newcastle</p>
* @author David.Li
* @version 1.0
*/
public class SocketFileOperate {
protected String TCPServerAddress;
protected String UDPServerAddress;
protected int TCPServerPort;
protected int UDPServerPort;
private String hostName;
private int portNum;
private InetAddress localHost = null;
private Socket socketPort = null;
private File inputStream = new File("information.txt");
public SocketFileOperate ( ) {
}
public SocketFileOperate ( String hostName, int portNum ) {
this.hostName = hostName;
this.portNum = portNum;
}
/**
* get Server Address from information.txt
* @return server address string
* @throws IOException
*/
public String getTCPServerAddress () throws IOException{
this.TCPServerAddress = getServer("SEV");
return TCPServerAddress;
}
/**
* get UPD multicast address from information.txt
* @return UPD server address string
* @throws IOException
*/
public String getUDPServerAddress () throws IOException{
this.UDPServerAddress = getServer("BRO");
return UDPServerAddress;
}
/**
* get TCP port from information.txt
* @return TCP server port - int
* @throws IOException
*/
public int getTCPServerPort () throws IOException{
this.TCPServerPort = getPort("TCP");
return TCPServerPort;
}
/**
* get UDP port from information.txt
* @return UDP server port - int
* @throws IOException
*/
public int getUDPServerPort () throws IOException{
this.UDPServerPort = getPort("UDP");
return UDPServerPort;
}
/**
* Get Host name of local machine
* @return Local Host Name -> String
* @throws IOException
*/
public String getLocalHoseName() throws IOException{
String hostName = new String();
localHost = InetAddress.getLocalHost();
hostName = localHost.getHostName();
return hostName;
}
/**
* Get Ip address of local host machine
* @return local ip -> String
* @throws IOException
*/
public String getLocalIPAddress() throws IOException{
String localIpAddress = new String();
localHost = InetAddress.getLocalHost();
localIpAddress = localHost.getHostAddress();
return localIpAddress;
}
/**
* Get line number from a file named information.txt
* You can change this port as you want
* Test success!
* @return Line num of document -> int
* @throws IOException
*/
public int getLine ( ) throws IOException{
int fileLine = 0;
BufferedReader in = new BufferedReader(new FileReader(inputStream));
while ( in.readLine()!= null){
fileLine ++;
}
in.close();
return fileLine;
}
/**
* Get port num from a file named information.txt
* You can change this port as you want
* Test Success
* @param typePort -> String of type of port like TCP UDP
* @return get port num of information.txt -> int
* @throws IOException
*/
public int getPort ( String typePort ) throws IOException{
int lineNum;
String portString = null;
lineNum = getLine();
BufferedReader in = new BufferedReader(new FileReader(inputStream));
String portInf[] = new String[lineNum];
for (int i = 0 ; i < lineNum; i++){
portInf[i] = in.readLine();
StringTokenizer st = new StringTokenizer( portInf[i] , "|");
while( st.hasMoreElements() ){
if ( st.nextToken().equals(typePort) )
portString = st.nextToken();
}
}
in.close();
portNum = (new Integer(portString)).intValue();
return portNum;
}
/**
* get server ip address saved in the file
* @param typeServer -> server String
* @return server IP address in information file
* @throws IOException
*/
public String getServer ( String typeServer ) throws IOException{
int lineNum;
String serverString = null;
lineNum = getLine();
BufferedReader in = new BufferedReader(new FileReader(inputStream));
String serverInf[] = new String[lineNum];
for (int i = 0 ; i < lineNum; i++){
serverInf[i] = in.readLine();
StringTokenizer st = new StringTokenizer( serverInf[i] , "|");
while( st.hasMoreElements() ){
if ( st.nextToken().equals(typeServer) )
serverString = st.nextToken();
}
}
in.close();
return serverString;
}
}
package TankBattle;
import java.net.*;
import java.io.*;
/**
* <p>Title: Tank Game</p>
* <p>Description: SDIA csc829</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Uni. Of Newcastle</p>
* @author David.Li
* @version 1.0
*/
public class SocketUtil {
private Socket s;
/**
* constructor of classes, init a socket
* @param s - socket
*/
public SocketUtil(Socket s) {
this.s = s;
}
/**
* Get data from socket
* @return DataInputStream type
* @throws IOException
*/
public DataInputStream getDataStream() throws IOException {
return(new DataInputStream( new BufferedInputStream( s.getInputStream())));
}
/**
* Get unbuffered data from socket
* @return DataInputStream type
* @throws IOException
*/
public DataInputStream getUnbufferedDataStream() throws IOException {
return(new DataInputStream(s.getInputStream()));
}
/**
* output data to a socket
* @return PrintStream type
* @throws IOException
*/
public PrintStream getPrintStream() throws IOException {
return(new PrintStream(s.getOutputStream(),true));
}
}
package TankBattle;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* <p>Title: Tank Game</p>
* <p>Description: SDIA csc829</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Uni. Of Newcastle</p>
* @author unascribed
* @version 1.0
*/
public class TankBattleThread extends Thread{
// cActive is used for testing tank HP
private boolean cActive;
private String tankSignal;
private String receiveData;
private String tankName;
private Vector anotherTank;
private Thread cTankThread;
private Socket cSocket;
private NetworkClient nClient = new NetworkClient();
public TankBattleThread() {
}
public void start(){
this.cTankThread = new Thread(this);
cTankThread.start();
// start local client receive thread
new NetworkClientReceiveThread(new NetworkClient()).start();
// send client IP address + tank name
cSocket = nClient.initConnectServer();
cActive = true;
// prepare to pain map and tank!!! at here
}
public void run(){
// at some element, if key press, then repain tank , send new X,Y
while (cActive){
// test the receive Data state, if true, means new receive data
// receivedata fromat: Signal || TankName || HP || X || Y || D
if (nClient.getStateOfData()){
receiveData = nClient.getReceiveData();
StringTokenizer st = new StringTokenizer(receiveData,"||");
tankSignal = st.nextToken();
tankName = st.nextToken();
// receive another tank start
if(tankSignal.equals("start")){
for (int i = 0; i < anotherTank.size(); i++){
// if another is deid, then remove it from Vector
if (((AnotherTankThread)anotherTank.elementAt(i)).anotherActive == false )
anotherTank.remove(i);
}
// add a new element, mean is another tank start
// give receive tank name to another tank thread, anotherName
anotherTank.addElement(new AnotherTankThread(tankName));
// start another Tank Thread
((AnotherTankThread)anotherTank.lastElement()).start();
}
else if (tankSignal.equals("bye")){
// another gamer is quite
for (int i = 0; i < anotherTank.size(); i++){
if ((((AnotherTankThread)
anotherTank.elementAt(i)).anotherName).equals(tankName)){
// end that tank
((AnotherTankThread)anotherTank.elementAt(i)).finilize();
}
anotherTank.remove(i);
}
}else{
// find which tank move
for (int i = 0; i < anotherTank.size(); i++){
if ((((AnotherTankThread)
anotherTank.elementAt(i)).anotherName).equals(tankName)){
// decompress packet.
((AnotherTankThread)anotherTank.elementAt(i)).setAnotherHP(st.nextToken());
((AnotherTankThread)anotherTank.elementAt(i)).setAnotherX(st.nextToken());
((AnotherTankThread)anotherTank.elementAt(i)).setAnotherY(st.nextToken());
((AnotherTankThread)anotherTank.elementAt(i)).setAnotherD(st.nextToken());
}
}
}
}
}
// enquire client, do you want to start a new tank, tank is deid.
}
public void finilize(){
// close all sub Thread of local Client
for (int i = 0; i < anotherTank.size(); i++ ){
((AnotherTankThread)anotherTank.elementAt(i)).finilize();
}
this.cTankThread = null;
}
}
SEV|服务器IP地址
BRO|230.0.0.1
TCP|服务器TCP端口
UDP|2004
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -