Created
January 18, 2013 16:49
-
-
Save anonymous/4565988 to your computer and use it in GitHub Desktop.
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.Windows.Forms; | |
using System.Net; | |
using System.Net.Sockets; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
namespace Client | |
{ | |
public partial class Form1 : Form | |
{ | |
private static string ip = "192.168.0.100"; | |
private static short port = 8000; | |
// end of config | |
private static TcpClient client = new TcpClient(); | |
private static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port); | |
public Form1() | |
{ | |
InitializeComponent(); | |
rtbInput.ReadOnly = true; | |
try | |
{ | |
client.Connect(endPoint); | |
} | |
catch | |
{ | |
rtbInput.Text += "Could not connect to the server"; | |
return; | |
} | |
} | |
private void btnSend_Click(object sender, System.EventArgs e){ | |
var stream = client.GetStream(); | |
string msg; | |
byte[] buffer; | |
// Send messages to the server | |
while (true) | |
{ | |
msg = tbOutput.Text; | |
rtbInput.AppendText(msg + System.Environment.NewLine); | |
if (!String.IsNullOrWhiteSpace(msg)) | |
{ | |
buffer = Encoding.ASCII.GetBytes(msg); | |
stream.Write(buffer, 0, buffer.Length); | |
stream.Flush(); | |
tbOutput.Clear(); | |
} | |
else break; | |
} | |
} | |
} | |
} |
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.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
namespace Server | |
{ | |
public partial class Form1 : Form | |
{ | |
// config | |
private static string ip = "192.168.0.100"; | |
private static short port = 8000; | |
// end of config | |
private static Thread thread; | |
private static TcpListener listener; | |
delegate void SetTextCallback(string text); | |
public Form1() | |
{ | |
InitializeComponent(); | |
rtbText.ReadOnly = true; | |
listener = new TcpListener(IPAddress.Parse(ip), port); | |
thread = new Thread(worker); | |
// start the listening thread | |
thread.Start(); | |
} | |
public void worker() | |
{ | |
// listen... | |
listener.Start(); | |
SetText("Waiting for clients"); | |
while (true) | |
{ | |
// wait for a client to connect | |
TcpClient client = listener.AcceptTcpClient(); | |
// client connected | |
int s = client.GetHashCode(); | |
SetText("Client " + s + " connected..."); | |
// launch a thread to handle it | |
ThreadPool.QueueUserWorkItem(tcpClient => | |
{ | |
var theClient = (TcpClient)tcpClient; | |
var stream = theClient.GetStream(); | |
var bufferSize = 4096; | |
var buffer = new byte[bufferSize]; | |
int count; | |
// read the client data | |
while (true) | |
{ | |
count = 0; | |
try | |
{ | |
count = stream.Read(buffer, 0, bufferSize); | |
} | |
catch (Exception ex) | |
{ | |
SetText("Error while reading client data: " + ex.Message); | |
break; | |
} | |
if (count > 0) | |
{ | |
int i = theClient.GetHashCode(); | |
string m = Encoding.ASCII.GetString(buffer, 0, count); | |
SetText(i + " says: " + m); | |
} | |
else break; | |
} | |
stream.Close(); | |
theClient.Close(); | |
}, client); | |
} | |
} | |
private void SetText(string text) | |
{ | |
if (this.rtbText.InvokeRequired) | |
{ | |
SetTextCallback d = new SetTextCallback(SetText); | |
this.Invoke(d, new object[] { text }); | |
} | |
else | |
{ | |
rtbText.AppendText(text + System.Environment.NewLine); | |
} | |
} | |
private void btnSend_Click(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment