Communicator Class
For communicator class i have used a TCPListner class in .net framework. This class has two methods. Read() and sendData(). Read will call in a separate thread which will be called from main thread in main class of the game.
For communicator class i have used a TCPListner class in .net framework. This class has two methods. Read() and sendData(). Read will call in a separate thread which will be called from main thread in main class of the game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.IO; | |
using System.Threading; | |
namespace TileMap | |
{ | |
class Communicator | |
{ | |
#region "Variables" | |
private NetworkStream clientStream; //Stream - outgoing | |
private TcpClient client; //To talk back to the client | |
private BinaryWriter writer; //To write to the clients | |
private NetworkStream serverStream; //Stream - incoming | |
private TcpListener listener; //To listen to the clinets | |
public string reply = ""; //The message to be written | |
private static Communicator comm = new Communicator(); | |
#endregion | |
public void Prepare() | |
{ | |
try | |
{ | |
//Creating listening Socket | |
IPAddress address = IPAddress.Parse(Constants.CLIENT_IP); | |
this.listener = new TcpListener(address, Constants.CLIENT_PORT); | |
//Starts listening | |
this.listener.Start(); | |
//Establish connection upon client request | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Communication (RECEIVING) Failed! \n " + e.Message); | |
} | |
} | |
public string Read() | |
{ | |
Socket connection = null; | |
//connection is connected socket | |
connection = listener.AcceptSocket(); | |
if (connection.Connected) | |
{ | |
//To read from socket create NetworkStream object associated with socket | |
this.serverStream = new NetworkStream(connection); | |
SocketAddress sockAdd = connection.RemoteEndPoint.Serialize(); | |
string s = connection.RemoteEndPoint.ToString(); | |
List inputStr = new List(); | |
int asw = 0; | |
while (asw != -1) | |
{ | |
asw = this.serverStream.ReadByte(); | |
inputStr.Add((Byte)asw); | |
} | |
reply = Encoding.UTF8.GetString(inputStr.ToArray()); | |
//Console.WriteLine(reply); | |
this.serverStream.Close(); | |
return reply; | |
} | |
else | |
{ | |
return "nothing"; | |
} | |
} | |
public void SendData(String message) | |
{ | |
//Opening the connection | |
this.client = new TcpClient(); | |
try | |
{ | |
this.client.Connect(Constants.SERVER_IP, Constants.SERVER_PORT); | |
if (this.client.Connected) | |
{ | |
//To write to the socket | |
this.clientStream = client.GetStream(); | |
//Create objects for writing across stream | |
this.writer = new BinaryWriter(clientStream); | |
Byte[] tempStr = Encoding.ASCII.GetBytes(message); | |
//writing to the port | |
this.writer.Write(tempStr); | |
this.writer.Close(); | |
this.clientStream.Close(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
finally | |
{ | |
this.client.Close(); | |
} | |
} | |
} | |
} |
No comments:
Post a Comment