So I downloaded an example program that shows socket communication here: http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/
You enter the IP + Port and connect to the server (or in my case its a device that can be plugged to the network).
Basically you can enter a text to the first textbox and when you click Tx it will send it to the server.
If you press Rx it will receive the response and paste it as string into the second textbox, heres a snippet of the receive function:
private void cmdReceiveData_Click(object sender, System.EventArgs e)
{
try
{
byte [] buffer = new byte[1024];
int iRx = m_socWorker.Receive (buffer);
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
txtDataRx.Text = szData;
}
catch(System.Net.Sockets.SocketException se)
{
MessageBox.Show (se.Message );
}
}
Now I'd like to have a function/Eventhandler that automatically detects whenever there's a response and automatically enters it to the second textbox without having to press Rx, just like the System.IO.Ports.SerialDataReceivedEventArgs handler.
Is there such a thing for sockets and is it possible to implement here?
If you try to answer or help me please not that i'm a rookie when it comes down to programming.
Thanks
Loading
Sam HobbsPosted Jul 13, 2011, 5:09 PM
Dan HoPosted Jul 13, 2011, 3:35 AM
Heres the fixed code:
using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
class TalkUser
{
static Form talk;
static N.Sockets.TcpClient TC;
public static int testint;
public static string rData;
[DllImport("kernel32.dll")]
private static extern void ExitProcess(int a);
public static void Main()
{
talk = new Form();
talk.Text = "TalkUser - The OFFICIAL TalkServ Client";
talk.Closing += new CancelEventHandler(talk_Closing);
talk.Controls.Add(new TextBox());
talk.Controls[0].Dock = DockStyle.Fill;
talk.Controls.Add(new TextBox());
talk.Controls[1].Dock = DockStyle.Bottom;
((TextBox)talk.Controls[0]).Multiline = true;
((TextBox)talk.Controls[1]).Multiline = true;
talk.WindowState = FormWindowState.Maximized;
talk.Show();
((TextBox)talk.Controls[1]).KeyDown += new KeyEventHandler(key_down);
TC = new N.Sockets.TcpClient();
TC.Connect("192.168.1.17", 8000);
Thread t = new Thread(new ThreadStart(run));
t.Start();
while (true)
{
Application.DoEvents();
if (testint == 1)
{
((TextBox)talk.Controls[0]).AppendText(rData + "\r\n");
((TextBox)talk.Controls[0]).SelectionStart = ((TextBox)talk.Controls[0]).Text.Length;
testint = 0;
}
}
}
private static void talk_Closing(object s, CancelEventArgs e)
{
e.Cancel = false;
Application.Exit();
ExitProcess(0);
}
private static void key_down(object s, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox TB = (TextBox)s;
if (TB.Lines.Length > 1)
{
StreamWriter SW = new StreamWriter(TC.GetStream());
SW.WriteLine(TB.Text);
SW.Flush();
TB.Text = "";
TB.Lines = null;
}
}
}
public static void run()
{
StreamReader SR = new StreamReader(TC.GetStream());
while (true)
{
Application.DoEvents();
TextBox TB = (TextBox)talk.Controls[0];
rData = SR.ReadLine();
testint = 1;
}
}
}
Dan HoPosted Jul 12, 2011, 10:17 AM
well I found a different program thats supposed to automatically react and append the text whenver I get a response. However I get an error saying InvalidOperationException and something with cross-thread error when I get a response. I marked the line where it appears.
I heard this can be fixed with invokes and delegates but I dont have any idea what this is supposed to be, can anyone modify this in order for it to work?
using System.IO;
using System.Net;
using System;
using System.Threading;
using N = System.Net;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
class TalkUser
{
static Form talk;
static N.Sockets.TcpClient TC;
[DllImport("kernel32.dll")]
private static extern void ExitProcess(int a);
public static void Main()
{
talk = new Form();
talk.Text = "TalkUser - The OFFICIAL TalkServ Client";
talk.Closing += new CancelEventHandler(talk_Closing);
talk.Controls.Add(new TextBox());
talk.Controls[0].Dock = DockStyle.Fill;
talk.Controls.Add(new TextBox());
talk.Controls[1].Dock = DockStyle.Bottom;
((TextBox)talk.Controls[0]).Multiline = true;
((TextBox)talk.Controls[1]).Multiline = true;
talk.WindowState = FormWindowState.Maximized;
talk.Show();
((TextBox)talk.Controls[1]).KeyUp += new KeyEventHandler(key_up);
TC = new N.Sockets.TcpClient();
TC.Connect("192.168.1.17", 8000);
Thread t = new Thread(new ThreadStart(run));
t.Start();
while (true)
{
Application.DoEvents();
}
}
private static void talk_Closing(object s, CancelEventArgs e)
{
e.Cancel = false;
Application.Exit();
ExitProcess(0);
}
private static void key_up(object s, KeyEventArgs e)
{
TextBox TB = (TextBox)s;
if (TB.Lines.Length > 1)
{
StreamWriter SW = new StreamWriter(TC.GetStream());
SW.WriteLine(TB.Text);
SW.Flush();
TB.Text = "";
TB.Lines = null;
}
}
private static void run()
{
StreamReader SR = new StreamReader(TC.GetStream());
while (true)
{
Application.DoEvents();
TextBox TB = (TextBox)talk.Controls[0];
TB.AppendText(SR.ReadLine() + "\r\n");
TB.SelectionStart = TB.Text.Length;
}
}
}