Created
April 14, 2012 01:40
-
-
Save bschwind/2381434 to your computer and use it in GitHub Desktop.
TCP Client Example
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.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