1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Brawl_ultra_guns
{
public class Client
{
static byte[] msg;
int port = 8000;
IPAddress ip;
IPEndPoint IpEnd;
Thread th_recieve;
static Socket ClientSocket;
private static int ID = 0;
static NiveauMulti multi = new NiveauMulti(null);
static int ID2;
public void Main()
{
int X = Convert.ToInt32(multi.player.Position.X) * 1000;
int Y = Convert.ToInt32(multi.player.Position.Y);
SendMsg(BitConverter.GetBytes((ID*1000000+X+Y)));
}
public void Start()
{
ip = IPAddress.Parse("192.94.16.19");
IpEnd = new IPEndPoint(ip, port);//initialisation du serveur
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
ClientSocket.Connect(IpEnd);
th_recieve = new Thread(Client.Recieve);//si il se connecte, lance le thread de detection
th_recieve.Start();
}
catch (SocketException)//s'il n'y a pas de serveur
{
}
}
public void SendMsg(byte[] msg)
{
int DtSent = ClientSocket.Send(msg, msg.Length, SocketFlags.Broadcast);
}
public static void Recieve()
{
while (true)
{
if (ClientSocket.Connected) //si on est connecte
{
if (ClientSocket.Available > 0) //si on recoit qque chose
{
while (ClientSocket.Available > 0) //tant quon recoit
{
msg = new Byte[ClientSocket.Available];
ClientSocket.Receive(msg, 0, ClientSocket.Available, SocketFlags.None);
int seq = BitConverter.ToInt32(msg, 0);
if (ID == 0)
{
ID = BitConverter.ToInt32(msg, 0);
}
else
{
int seq = BitConverter.ToInt32(msg, ClientSocket.Available);
ID2 = seq / 1000000;
if (ID != ID2 & ID2 != 0)
{
//seq = seq % 1000000;
multi.joueurAdverse.Position.X = seq/1000;
multi.joueurAdverse.Position.Y = seq % 1000;
}
// }
}
}
}
}
}
public void Draw(SpriteBatch sprite, Texture2D text, SpriteFont spritefont)
{
sprite.DrawString(spritefont, Convert.ToString(ID), new Vector2(600, 0), Color.Black);
sprite.DrawString(spritefont, Convert.ToString(ID2), new Vector2(600, 20), Color.Black);
}
public void Deconnection()
{
try
{
th_recieve.Suspend();
th_recieve.Interrupt();
ClientSocket.Disconnect(true);
}
catch { }
}
}
} |
Partager