C # Tank war online code

Source: Internet
Author: User
Tags getstream

 

Simple C # Tank war online code

 

 

After writing a standalone edition http://blog.csdn.net/xiaoxiao108/archive/2010/12/18/6084473.aspx game

Write an online version for fun.

Development Tool vs2008

 

 

The online version is easy to implement.

1. One server and multiple clients

2. The server opens a port listener. When a client program is connected to the server, the server assigns a number to the client as its tank number.

3. When new tanks are created and tanks are moved, the client sends data to the server, and the server sends the data to all clients to synchronize online games.

 

 

Specific implementation code

1. enable service code on the server

Public void start ()

{

// Enable the UDP thread

Thread t = new thread (udpthread );

T. isbackground = true;

T. Start ();

// Enable the TCP Service

Tcplistener TL = new tcplistener (tcp_port );

TL. Start ();

While (true)

{

Tcpclient Tc = tl. accepttcpclient ();

Stream NS = tc. getstream ();

Binaryreader BR = new binaryreader (NS );

Int udpport = Br. readint32 (); // Br. Close (); cannot close br

Binarywriter BW = new binarywriter (NS );

Bw. Write (ID ++ );

Ipendpoint rep = (ipendpoint) TC. Client. remoteendpoint;

Client c = new client (REP. Address. tostring (), udpport );

Clients. Add (C );

Console. writeline ("a client TCP connect! ADDR-"+ rep. Address. tostring () +": "+ rep. Port );

}

}

2. UDP data receiving and forwarding code on the server

Private void udpthread ()

{

Console. writeline ("UDP thread started at Port:" + udp_port );

Byte [] Buf = new byte [1, 1024];

Udpclient UC = new udpclient (udp_port); // different from Java, if this sentence is placed outside while, the second tank cannot be connected.

Ipendpoint ipep = new ipendpoint (IPaddress. Any, 0 );

While (true)

{

Buf = UC. Receive (ref ipep );

Console. writeline ("a UDP packet published ed! From "+ ipep. Address +": "+ ipep. Port );

// Forward the received data to each client

For (INT I = 0; I <clients. Count; I ++)

{

Client c = clients [I];

Udpclient _ UC = new udpclient ();

_ UC. Connect (C. IP, C. udpport );

_ UC. Send (BUF, Buf. Length );

}

}

}

3. Client connection code

Public void connect (string IP, int port)

{

This. IP = IP;

Tcpclient client = new tcpclient ();

Client. Connect (IP, Port );

Stream NS = client. getstream ();

Binarywriter BW = new binarywriter (NS );

Bw. Write (udpport );

Binaryreader BR = new binaryreader (NS); // BW. Close (); BW cannot be closed.

// Retrieve the tank number assigned by the server from the server

Int id = Br. readint32 ();

TC. mytank. ID = ID;

// Set the number to an even number to a bad guy

If (ID % 2 = 0)

TC. mytank. Good = false;

Else

TC. mytank. Good = true;

// The following debugging code is displayed in the output window.

Debug. writeline ("connected to server! And server give me a id: "+ id );

BR. Close ();

NS. Close ();

Client. Close ();

Tanknewmsg MSG = new tanknewmsg (TC. mytank );

Send (MSG );

// Enable the receiving thread

Thread t = new thread (udprecvthread );

T. isbackground = true;

T. Start ();

}

4. Add the message code to the tank to send the code

 

Public void send (udpclient UC, string IP, int udpport)

{

UC. Connect (IP, udpport );

// Use | in the program to separate the sent content

String STR = msgtype + "|" + tank. ID + "|" + tank. X + "|" + tank. Y + "|" + (INT) tank. dir + "|" + tank. good;

UC. Send (encoding. utf32.getbytes (STR), encoding. utf32.getbytes (STR). Length );

}

5. Add the message parsing code to the tank

Public void parse (byte [] B)

{

String STR = encoding. utf32.getstring (B );

String [] STRs = Str. Split ('| ');

Int id = convert. toint32 (STRs [1]);

// If your tank does not process the data packet

If (ID = tc. mytank. ID)

{

Return;

}

Int x = convert. toint32 (STRs [2]);

Int y = convert. toint32 (STRs [3]);

Direction dir = (Direction) convert. toint32 (STRs [4]);

Bool good = convert. toboolean (STRs [5]);

Boolean exist = false;

For (INT I = 0; I <TC. Tanks. Count; I ++)

{

Tank T = tc. Tanks [I];

If (T. ID = ID)

{

Exist = true;

Break;

}

}

// Create a tank if it does not exist

If (! Exist)

{

Tanknewmsg MSG = new tanknewmsg (TC. mytank );

TC. nc. Send (MSG );

// Tank T = new tank (X, Y, good, Tc); // in Java, tank T = new tank (X, Y, good, Dir, TC)

Tank T = new tank (X, Y, good, Dir, Tc); // The old tank may send packets to the new tank.

T. ID = ID;

TC. Tanks. Add (t );

}

}

6. Tank mobile message

Public void send (udpclient UC, string IP, int udpport)

{

UC. Connect (IP, udpport );

// Use | in the program to separate the sent content

String STR = msgtype + "|" + ID + "|" + x + "|" + Y + "|" + convert. toint32 (DIR );

UC. Send (encoding. utf32.getbytes (STR), encoding. utf32.getbytes (STR). Length );

}

 

Public void parse (byte [] B)

{

String STR = encoding. utf32.getstring (B );

String [] STRs = Str. Split ('| ');

Int id = convert. toint32 (STRs [1]);

// If your tank does not process the data packet

If (ID = tc. mytank. ID)

{

Return;

}

Int x = convert. toint32 (STRs [2]);

Int y = convert. toint32 (STRs [3]);

Direction dir = (Direction) convert. toint32 (STRs [4]);

For (INT I = 0; I <TC. Tanks. Count; I ++)

{

Tank T = tc. Tanks [I];

If (T. ID = ID)

{

T. dir = dir;

T. X = X;

T. Y = y;

Break;

}

}

}

7. Bullet Message Processing code

Public void send (udpclient UC, string IP, int udpport)

{

UC. Connect (IP, udpport );

// Use | in the program to separate the sent content

String STR = msgtype + "|" + M. tankid + "|" + M. X + "|" + M. Y + "|" + (INT) M. dir + "|" + M. good;

UC. Send (encoding. utf32.getbytes (STR), encoding. utf32.getbytes (STR). Length );

}

 

Public void parse (byte [] B)

{

String STR = encoding. utf32.getstring (B );

String [] STRs = Str. Split ('| ');

Int tankid = convert. toint32 (STRs [1]);

If (tankid = tc. mytank. ID)

{

Return;

}

Int x = convert. toint32 (STRs [2]);

Int y = convert. toint32 (STRs [3]);

Direction dir = (Direction) convert. toint32 (STRs [4]);

Bool good = convert. toboolean (STRs [5]);

Missile M = new missile (tankid, X, Y, good, Dir, Tc );

TC. Missiles. Add (m );

}

 

 

 

 

Run the server before running multiple clients during standalone testing.

Modify NC. Connect ("127.0.0.1", 8888) in multi-host online games. The IP address in the game can be played in the LAN.

Private void form1_load (Object sender, eventargs E)

{

Mytank = new tank (50, 20, true, this); // put it in front of this cannot use // the Y axis is 30 less than Java

NC = new netclient (this );

NC. Connect ("127.0.0.1", 8888 );

// NC. Connect ("192.168.1.168", 8888 );

// NC. Connect ("10.10.10.1", 8888 );

}

 

If you find anything unreasonable, you need to improve the place, mail contact 328452421@qq.com (qq perennial not online, mail contact) Zhu Xiao (Taishan College ). Exchange with each other. Thank you.

 

 

Http://download.csdn.net/source/2986606

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.