Skip to content

Instantly share code, notes, and snippets.

@bschwind
Created April 14, 2012 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bschwind/2381434 to your computer and use it in GitHub Desktop.
Save bschwind/2381434 to your computer and use it in GitHub Desktop.
TCP Client Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphicsToolkit.Networking;
namespace TestClient
{
public class Program
{
static void Main(string[] args)
{
//Create a client and connect to a server running on our local machine
Client client = new Client();
client.OnDataReceived += new ClientHandlePacketData(client_OnDataReceived);
//Replace "localhost" with the ip address of your server
//Replace 16645 with the port number you use for your server
client.ConnectToServer("localhost", 16645);
//Loop and send whatever the user types to the server
ASCIIEncoding encoder = new ASCIIEncoding();
while (true)
{
string s = Console.ReadLine();
//If the user types exit, then exit
if ("exit".Equals(s))
{
break;
}
client.SendImmediate(encoder.GetBytes(s));
}
client.Disconnect();
Environment.Exit(0);
}
//This method is called when the client has received data from the server
static void client_OnDataReceived(byte[] data, int bytesRead)
{
ASCIIEncoding encoder = new ASCIIEncoding();
string message = encoder.GetString(data, 0, bytesRead);
Console.WriteLine("Received a message: " + message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment