本文整理汇总了C#中CustomNetworking.StringSocket.BeginReceive方法的典型用法代码示例。如果您正苦于以下问题:C# StringSocket.BeginReceive方法的具体用法?C# StringSocket.BeginReceive怎么用?C# StringSocket.BeginReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CustomNetworking.StringSocket
的用法示例。
在下文中一共展示了StringSocket.BeginReceive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
示例2: User
public User(string _name, StringSocket _socket)
{
Name = _name;
socket = _socket;
Terminated = false;
socket.BeginReceive(LineReceivedFromUser, socket);
}
示例3: 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);
}
}
示例4: 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);
}
示例5: 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);
}
}
示例6: 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);
}
}
示例7: 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);
}
}
}
示例8: 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"));
}
示例9: Receiver
/// <summary>
/// Receives COUNT strings and appends them to the received list.
/// </summary>
private void Receiver(StringSocket socket)
{
int count = 0; // Number of times callback has been called
// Receive COUNT strings
for (int i = 0; i < COUNT; i++)
{
// Receive one string. The callback appends to the list and updates count
socket.BeginReceive((s, e, p) => { lock (received) { received.Add(s); count++; } }, i);
}
// Wait until all of the callbacks have been called.
SpinWait.SpinUntil(() => count == COUNT);
}
示例10: run
public void run(int port)
{
TcpListener server = null;
TcpClient client = null;
try
{
// Create and start a server and client
server = new TcpListener(IPAddress.Any, port);
server.Start();
client = new TcpClient("localhost", port);
// Obtain the sockets from the two ends of the connection. We are using the blocking AcceptSocket()
// method here, which is OK for a test case.
Socket serverSocket = server.AcceptSocket();
Socket clientSocket = client.Client;
// Make sure we're connected.
Assert.IsTrue(serverSocket.Connected);
Assert.IsTrue(clientSocket.Connected);
// Wrap the two ends of the connection into StringSockets
StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
StringSocket receiveSocket = new StringSocket(clientSocket, new UTF8Encoding());
// Use two threads to blast strings into the socket
Thread t1 = new Thread(() => Blast1(sendSocket));
Thread t2 = new Thread(() => Blast2(sendSocket));
t1.IsBackground = true;
t2.IsBackground = true;
t1.Start();
t2.Start();
// Receive all those messages
for (int i = 0; i < 2 * COUNT; i++)
{
receiveSocket.BeginReceive(Receiver, i);
Assert.IsTrue(mre.WaitOne(TIMEOUT), "Didn't receive in timely fashion " + i);
mre.Reset();
}
// Make sure that the strings arrived in the proper order
int blast1Count = 0;
int blast2Count = 0;
foreach (String s in strings)
{
if (s.StartsWith("Blast1"))
{
Assert.AreEqual(blast1Count, Int32.Parse(s.Substring(7)), "Bad Blast1: " + s);
blast1Count++;
}
else if (s.StartsWith("Blast2"))
{
Assert.AreEqual(blast2Count, Int32.Parse(s.Substring(7)), "Bad Blast2: " + s);
blast2Count++;
}
else
{
Assert.Fail("Bad string: " + s);
}
}
}
finally
{
server.Stop();
client.Close();
}
}
示例11: ConnectionAccept
/// <summary>
/// Callback for accepting socket connections.
/// </summary>
/// <param name="ar"></param>
private void ConnectionAccept(IAsyncResult ar)
{
Socket socket = server.EndAcceptSocket(ar);
StringSocket StrungSocket = new StringSocket(socket, UTF8Encoding.Default);
StrungSocket.BeginReceive(AddPlayer, StrungSocket);
//Console.WriteLine("Connected");
//if an even number of clients
//send new pair a Start^&%&*^& message
server.BeginAcceptSocket(ConnectionAccept, null);
}
示例12: BeginReceieve
/// <summary>
/// Calls BeginReceive and adds strings in order they were received.
/// </summary>
/// <param name="receiveSocket"></param>
/// <param name="n"></param>
private void BeginReceieve(StringSocket receiveSocket, int n)
{
receiveSocket.BeginReceive(ReceiveCallback, null);
CompareList.Add(n.ToString());
}
示例13: run
public void run(int port)
{
// Create and start a server and client.
TcpListener server = null;
TcpClient client = null;
try
{
server = new TcpListener(IPAddress.Any, port);
server.Start();
client = new TcpClient("localhost", port);
// Obtain the sockets from the two ends of the connection. We are using the blocking AcceptSocket()
// method here, which is OK for a test case.
Socket serverSocket = server.AcceptSocket();
Socket clientSocket = client.Client;
// Wrap the two ends of the connection into StringSockets
StringSocket sendSocket = new StringSocket(serverSocket, new UTF8Encoding());
StringSocket receiveSocket = new StringSocket(clientSocket, new UTF8Encoding());
// This will coordinate communication between the threads of the test cases
mre1 = new ManualResetEvent(false);
mre2 = new ManualResetEvent(false);
// Make two receive requests
receiveSocket.BeginReceive(CompletedReceive1, 1);
receiveSocket.BeginReceive(CompletedReceive2, 2);
// Now send the data. Hope those receive requests didn't block!
String msg = "Hello world\nThis is a test\n";
foreach (char c in msg)
{
sendSocket.BeginSend(c.ToString(), (e, o) => { }, null);
}
// Make sure the lines were received properly.
Assert.AreEqual(true, mre1.WaitOne(timeout), "Timed out waiting 1");
Assert.AreEqual("Hello world", s1);
Assert.AreEqual(1, p1);
Assert.AreEqual(true, mre2.WaitOne(timeout), "Timed out waiting 2");
Assert.AreEqual("This is a test", s2);
Assert.AreEqual(2, p2);
}
finally
{
server.Stop();
client.Close();
}
}
示例14: ConnectionRequested
// GAME SERVER
/// <summary>
/// To establish connection through StringSocket (begin listening)
/// </summary>
/// <param name="result"></param>
public void ConnectionRequested(IAsyncResult result)
{
// We obtain the socket corresonding to the connection request. Notice that we
// are passing back the IAsyncResult object.
StringSocket ss = new StringSocket(listener.EndAcceptSocket(result), new UTF8Encoding());
ss.BeginReceive(RequestGameStart, ss);
// We ask the server to listen for another connection request. As before, this
// will happen on another thread.
listener.BeginAcceptSocket(ConnectionRequested, null);
}
示例15: BoggleGame
/// <summary>
/// Constructor for a boggle game. Initializes the member variables and starts the game
/// between two given sockets (player1 and player2).
/// </summary>
/// <param name="player1"></param>
/// <param name="player2"></param>
/// <param name="time"></param>
/// <param name="filepath"></param>
/// <param name="initBoard"></param>
public BoggleGame(StringSocket player1, StringSocket player2, int time, string filepath, string initBoard)
{
//Initialize member variables
this.player1 = player1;
player1Name = player1.Name;
this.player2 = player2;
player2Name = player2.Name;
p2Words = new HashSet<string>();
p2IllegalWords = new HashSet<string>();
p1Words = new HashSet<string>();
p1IllegalWords = new HashSet<string>();
commonWords = new HashSet<string>();
validWords = new HashSet<String>();
this.time = time;
this.filepath = filepath;
p1Score = 0;
p2Score = 0;
Console.WriteLine("Starting game between " + player1Name + " and " + player2Name);
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(filepath);
while ((line = file.ReadLine()) != null)
{
//For each line in the file, add it to the valid words list
validWords.Add(line);
}
//Close the file
file.Close();
//Create the board, if given an initialization, then initialize the board,
//otherwise create a random start
if (initBoard != null)
bb = new BoggleBoard(initBoard);
else
bb = new BoggleBoard();
//Send the START command to both players, showing the letters, time, and opponents name
player1.BeginSend("START " + bb.ToString() + " " + time.ToString() + " " + player2.Name +"\n", (o, e) => { }, null);
player2.BeginSend("START " + bb.ToString() + " " + time.ToString() + " " + player1.Name + "\n", (o, e) => { }, null);
//Create a timer to keep track of the time with a 1 second interval
System.Timers.Timer gameTimer = new System.Timers.Timer(1000);
//Add the event handler to the timer
gameTimer.Elapsed += new System.Timers.ElapsedEventHandler(gameTimer_Elapsed);
//If we've run out of time
if (time != 0)
{
//receive stuff from player 1
player1.BeginReceive(Player1Input, null);
//receive stuff fromp player 2
player2.BeginReceive(Player2Input, null);
}
}