Added new Project: MudClient
- TODO: Read my ReadME - TODO: Change it into a GUI - Currently it is a console application, Telnet functionality needs to be added as well as \n\r | \r\n correction. Simply create a GUI for it and use the Networking/Client.cs file (see how it is used in the Program.cs).
This commit is contained in:
parent
3110b74b58
commit
7ad027e9d7
5 changed files with 265 additions and 1 deletions
96
MudClient/Networking/Client.cs
Normal file
96
MudClient/Networking/Client.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace MudClient.Networking
|
||||
{
|
||||
class Client
|
||||
{
|
||||
public Client()
|
||||
{
|
||||
}
|
||||
~Client()
|
||||
{
|
||||
}
|
||||
public Boolean Initialize(string i, int p)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (i.Length <= 0)
|
||||
return false;
|
||||
ip = i;
|
||||
if (p <= 0)
|
||||
return false;
|
||||
port = p;
|
||||
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public Boolean Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
sock.Connect(ip, port);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public Boolean Send(string data,Boolean newLine)
|
||||
{
|
||||
try
|
||||
{
|
||||
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
||||
if(newLine)
|
||||
data += "\n";
|
||||
sock.Send(encoding.GetBytes(data));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public Boolean Receive(out string rs,int size)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] data = new byte[size];
|
||||
sock.Receive(data);
|
||||
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
|
||||
rs = enc.GetString(data.ToArray());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
rs = "";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public Boolean End()
|
||||
{
|
||||
try
|
||||
{
|
||||
sock.Close();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private string ip;
|
||||
private int port;
|
||||
|
||||
private Socket sock;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue