本文整理汇总了C#中CustomNetworking.StringSocket类的典型用法代码示例。如果您正苦于以下问题:C# StringSocket类的具体用法?C# StringSocket怎么用?C# StringSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringSocket类属于CustomNetworking命名空间,在下文中一共展示了StringSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Connect
/// <summary>
/// Connect to the server at the given hostname and port and with the given name of username and
/// password.
/// </summary>
public bool Connect(String hostname, int port, String password)
{
try
{
TcpClient client;
//Case 1: Using IP
IPAddress address;
if (IPAddress.TryParse(hostname, out address))
{
client = new TcpClient();
client.Connect(address, port);
}
else
{
//Case 2: Using DNS
client = new TcpClient(hostname, port);
}
socket = new StringSocket(client.Client, UTF8Encoding.Default);
//PASSWORD[esc]password\n
char esc = (char)27;
socket.BeginSend("PASSWORD" + esc + password + "\n", (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
return true;
}
catch
{
return false;
}
}
示例2: Connect
public void Connect(string hostname, int port, string clientPassword)
{
// initial connection to the server
if (clientSSocket == null)
{
password = clientPassword;
try
{
TcpClient client = new TcpClient(hostname, port);
clientSSocket = new StringSocket(client.Client, UTF8Encoding.Default);
clientSSocket.BeginSend("PASSWORD" + "\\e" + password + "\n", (ex, p) => { }, null);
clientSSocket.BeginReceive(LineReceived, null);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
// not sure if this part is necessary?
else
{
//clientSSocket.Close(); we don't want to close the socket unless the client is disconnecting
TcpClient client = new TcpClient(hostname, port);
clientSSocket = new StringSocket(client.Client, UTF8Encoding.Default);
clientSSocket.BeginSend("PASSWORD"+ "\\e" + password + "\n", (ex, p) => { }, null);
clientSSocket.BeginReceive(LineReceived, null);
}
}
示例3: TestBasicWord
public void TestBasicWord()
{
TcpListener server = null;
TcpClient client1 = null;
TcpClient client2 = null;
server = new TcpListener(IPAddress.Any, 4042);
server.Start();
client1 = new TcpClient("localhost", 4042);
client2 = new TcpClient("localhost", 4042);
Socket serverSocket = server.AcceptSocket();
Socket client1Socket = client1.Client;
Socket client2Socket = client2.Client;
StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
StringSocket player1Socket = new StringSocket(client1Socket, new UTF8Encoding());
StringSocket player2Socket = new StringSocket(client2Socket, new UTF8Encoding());
BoggleServer.BoggleServer.Player player1 = new BoggleServer.BoggleServer.Player("ryan", player1Socket);
BoggleServer.BoggleServer.Player player2 = new BoggleServer.BoggleServer.Player("ryan2", player2Socket);
BoggleServer.BoggleServer.Match match = new BoggleServer.BoggleServer.Match(
player1, player2, 60, new HashSet<string>(System.IO.File.ReadAllLines(@"..\..\..\Resources\Dictionary.txt")), "horstoaeaggdpple");
match.ReceivedPlayer1Data("word horse", null, player1Socket);
Assert.AreEqual(2, player1.Score);
Assert.IsTrue(player1.UniqueLegalWords.Contains("HORSE"));
}
示例4: User
public User(string _name, StringSocket _socket)
{
Name = _name;
socket = _socket;
Terminated = false;
socket.BeginReceive(LineReceivedFromUser, socket);
}
示例5: Connect
/// <summary>
/// Connect to the server at the given hostname and port and with the given name.
/// </summary>
public void Connect(string hostname, int port)
{
// Connect or throw exception
_client = new TcpClient(hostname, port);
// Create a StringSocket for communicating with the server.
_stringSocket = new StringSocket(_client.Client, UTF8Encoding.Default);
}
示例6: Connect
//make sure the client isn't already connected
//then connect
public void Connect(int port, string hostName)
{
if (ss == null)
{
TcpClient client = new TcpClient(hostName, port);
ss = new StringSocket(client.Client, UTF8Encoding.Default);
IsConnected = true;
}
}
示例7: Connect
/// <summary>
/// connects on the given port and ip
/// </summary>
/// <param name="port"></param>
/// <param name="ip"></param>
public void Connect(int port, string ip)
{
if (socket == null)
{
TcpClient client = new TcpClient(ip, port);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
state = State.name;
socket.BeginReceive(LineReceived, null);
}
}
示例8: ConnectionReceived
/// <summary>
/// This callback method is called when a connection has been received.
/// </summary>
private void ConnectionReceived(IAsyncResult ar)
{
Socket socket = server.EndAcceptSocket(ar);
StringSocket ss = new StringSocket(socket, UTF8Encoding.Default);
// Set the StringSocket to listen for a name from the client.
ss.BeginReceive(GetReceived, ss);
// Set the server to listen for another connection.
server.BeginAcceptSocket(ConnectionReceived, null);
}
示例9: Connect
/// <summary>
/// Connect to the server at the given hostname and port and with the give name.
/// </summary>
public void Connect(string hostname, String name)
{
if (socket == null)
{
playerName = name;
TcpClient client = new TcpClient(hostname, 2000);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
socket.BeginSend("PLAY " + name + "\n", (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
}
}
示例10: Connect
/// <summary>
/// Connect to the server at the given hostname and port and with the give name.
/// </summary>
public void Connect(string hostname, int port, String name)
{
if (socket == null||!validName )
{
if (name.StartsWith("PLAY ") && name.Substring(5).Trim().Length > 0)
validName = true;
TcpClient client = new TcpClient(hostname, port);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
socket.BeginSend(name + "\n", (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
}
}
示例11: Connect
/// <summary>
/// Connect to the server at the given hostname and port, with the given name.
/// </summary>
public void Connect(String hostname, int port, String name)
{
if (socket == null)
{
TcpClient client = new TcpClient(hostname, port);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
this.name = name;
this.hostname = hostname;
socket.BeginSend("PLAY " + name + "\n", (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
}
}
示例12: Main
static void Main(string[] args)
{
//new server
TcpListener server = new TcpListener(IPAddress.Any, 4010);
server.Start();
//blocks until user connects
Socket serverSocket = server.AcceptSocket();
Console.WriteLine("hey");
// Wrap the two ends of the connection into StringSockets
StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
sendSocket.BeginSend("hi", (e, o) => { }, null);
Console.ReadLine();
}
示例13: Connect
/// <summary>
/// Connect to the server at the given hostname and port and with the give name.
/// </summary>
public void Connect(string IPAddress, String name)
{
if (socket == null)
{
try
{
TcpClient client = new TcpClient(IPAddress, 2000);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
socket.BeginSend("PLAY " + name + "\n", (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
}
catch (Exception e)
{
//no such host event
noSuchHostEvent(e.Message);
}
}
}
示例14: Connect
/// <summary>
/// Takes a string representing the IP Address of the server, an int with the port number and the players name.
/// It then connects to the server and sends the play message with the players name.
/// </summary>
public void Connect(String password)
{
if (socket == null || !socket.Connected())
{
// Tries to connect to the server and then send the play message
try
{
TcpClient client = new TcpClient(IP, port);
socket = new StringSocket(client.Client, UTF8Encoding.Default);
socket.BeginSend(password, (e, p) => { }, null);
socket.BeginReceive(LineReceived, null);
}
// If it is unable to connect it catches the exception and throws it on
catch (SocketException e)
{
throw e;
}
}
}
示例15: runGame
public void runGame()
{
server = new BoggleServer.BoggleServer(2000, 10, "..\\..\\..\\dictionary", "AAAABBBBCCCCDDDD");
player1 = new TcpClient("localhost", 2000);
player2 = new TcpClient("localhost", 2000);
Socket p1socket = player1.Client;
Socket p2socket = player2.Client;
p1 = new StringSocket(p1socket, new UTF8Encoding());
p2 = new StringSocket(p2socket, new UTF8Encoding());
p1.BeginSend("PLAY player1\n", (e, o) => { }, null);
p2.BeginSend("PLAY player2\n", (e, o) => { }, null);
mre = new ManualResetEvent(false);
receivedWords = new List<string>();
for (int i = 0; i < 20; i++)
{
p1.BeginReceive(callback, p1);
}
mre.WaitOne(1000);
Assert.IsTrue(receivedWords.Contains("START AAAABBBBCCCCDDDD 10 PLAYER2"));
}