Tom Knowlton
asked on
URGENT 500 POINTS!!! I just don't get Sockets :(
Why do I have to CLICK multiple times in order to get the message to show up?
FORM CODE:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplicationTestSock et
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer components = null;
private WindowsApplicationTestSock et.ClassLi braryServe rClient.Se rver sv = new WindowsApplicationTestSock et.ClassLi braryServe rClient.Se rver();
private System.Windows.Forms.Butto n button1;
public System.Windows.Forms.TextB ox textBox1;
private System.Windows.Forms.TextB ox textBox2;
private WindowsApplicationTestSock et.ClassLi braryServe rClient.Cl ient client = new WindowsApplicationTestSock et.ClassLi braryServe rClient.Cl ient();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Butto n();
this.textBox1 = new System.Windows.Forms.TextB ox();
this.textBox2 = new System.Windows.Forms.TextB ox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.b utton1_Cli ck);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 136);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(240, 20);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Contr ol[] {
this.textBox2,
this.textBox1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.F orm1_Load) ;
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
client.SendData(this.textB ox2.Text);
Application.DoEvents();
textBox1.Text = sv.txtReceived;
sv.txtReceived = "0";
}
private void Form1_Load(object sender, System.EventArgs e)
{
sv.StartListening();
client.Connect();
}
}
}
SERVER CLASS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Net.Sockets;
namespace WindowsApplicationTestSock et
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Server
{
public AsyncCallback pfnWorkerCallBack;
public Socket m_socListener;
public Socket m_socWorker;
public string txtReceived="";
public Server()
{
}
public void StartListening()
{
try
{
//create the listening socket...
m_socListener = new Socket(AddressFamily.Inter Network,So cketType.S tream,Prot ocolType.T cp);
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
//bind to local IP Address...
m_socListener.Bind( ipLocal );
//start listening...
m_socListener.Listen (4);
// create the call back for any client connections...
m_socListener.BeginAccept( new AsyncCallback ( OnClientConnect ),null);
//cmdListen.Enabled = false;
}
catch(SocketException se)
{
// MessageBox.Show ( se.Message );
}
}
public void OnClientConnect(IAsyncResu lt asyn)
{
try
{
m_socWorker = m_socListener.EndAccept (asyn);
WaitForData(m_socWorker);
}
catch(ObjectDisposedExcept ion)
{
System.Diagnostics.Debugge r.Log(0,"1 ","\n OnClientConnection: Socket has been closed\n");
}
catch(SocketException se)
{
//MessageBox.Show ( se.Message );
}
}
public class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1];
}
public void WaitForData(System.Net.Soc kets.Socke t soc)
{
try
{
if ( pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.dataBuffer ,0,theSocPkt.dataBuffer.Le ngth ,SocketFlags.None,pfnWorke rCallBack, theSocPkt) ;
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
public void OnDataReceived(IAsyncResul t asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncS tate ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndRe ceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8. GetDecoder ();
int charLen = d.GetChars(theSockId.dataB uffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//txtDataRx.Text = txtDataRx.Text + szData;
this.txtReceived += szData[0];
WaitForData(m_socWorker);
// WindowsApplicationTestSock et.Form1.A ctiveForm. Controls[0 ].Text += szData;
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugge r.Log(0,"1 ","\nOnDat aReceived: Socket has been closed\n");
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
public void TransferMessage()
{
try
{
Object objData = txtReceived;
byte[] byData = System.Text.Encoding.ASCII .GetBytes( objData.To String ());
m_socWorker.Send (byData);
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
}
}
}
CLIENT CLASS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net.Sockets ;
namespace WindowsApplicationTestSock et
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Socket m_socWorker;
public string txtReceived="";
public Client()
{
//
// TODO: Add constructor logic here
//
}
public void Connect()
{
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.Inter Network, SocketType.Stream, ProtocolType.IP);
//String szIPSelected = txtIPAddress.Text;
String szIPSelected = "192.168.0.155";
//String szPort = txtPort.Text;
String szPort = "8221";
int alPort = System.Convert.ToInt16 (szPort,10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse (szIPSelec ted);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remo teIPAddres s, alPort);
m_socWorker.Connect(remote EndPoint);
}
catch (System.Net.Sockets.Socket Exception se)
{
//MessageBox.Show ( se.Message );
}
}
public void SendData(string newmessage)
{
try
{
Object objData = newmessage;
byte[] byData = System.Text.Encoding.ASCII .GetBytes( objData.To String());
m_socWorker.Send (byData);
}
catch(System.Net.Sockets.S ocketExcep tion se)
{
//MessageBox.Show (se.Message );
}
}
public void ReceiveData()
{
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;
txtReceived = szData;
}
catch(System.Net.Sockets.S ocketExcep tion se)
{
//MessageBox.Show (se.Message );
}
}
public void StopClient()
{
m_socWorker.Close ();
}
}
}
}
FORM CODE:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplicationTestSock
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
private WindowsApplicationTestSock
private System.Windows.Forms.Butto
public System.Windows.Forms.TextB
private System.Windows.Forms.TextB
private WindowsApplicationTestSock
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Butto
this.textBox1 = new System.Windows.Forms.TextB
this.textBox2 = new System.Windows.Forms.TextB
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.b
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 136);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(240, 20);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new
this.textBox2,
this.textBox1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.F
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
client.SendData(this.textB
Application.DoEvents();
textBox1.Text = sv.txtReceived;
sv.txtReceived = "0";
}
private void Form1_Load(object sender, System.EventArgs e)
{
sv.StartListening();
client.Connect();
}
}
}
SERVER CLASS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Net.Sockets;
namespace WindowsApplicationTestSock
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Server
{
public AsyncCallback pfnWorkerCallBack;
public Socket m_socListener;
public Socket m_socWorker;
public string txtReceived="";
public Server()
{
}
public void StartListening()
{
try
{
//create the listening socket...
m_socListener = new Socket(AddressFamily.Inter
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
//bind to local IP Address...
m_socListener.Bind( ipLocal );
//start listening...
m_socListener.Listen (4);
// create the call back for any client connections...
m_socListener.BeginAccept(
//cmdListen.Enabled = false;
}
catch(SocketException se)
{
// MessageBox.Show ( se.Message );
}
}
public void OnClientConnect(IAsyncResu
{
try
{
m_socWorker = m_socListener.EndAccept (asyn);
WaitForData(m_socWorker);
}
catch(ObjectDisposedExcept
{
System.Diagnostics.Debugge
}
catch(SocketException se)
{
//MessageBox.Show ( se.Message );
}
}
public class CSocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1];
}
public void WaitForData(System.Net.Soc
{
try
{
if ( pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.dataBuffer ,0,theSocPkt.dataBuffer.Le
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
public void OnDataReceived(IAsyncResul
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncS
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndRe
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.
int charLen = d.GetChars(theSockId.dataB
System.String szData = new System.String(chars);
//txtDataRx.Text = txtDataRx.Text + szData;
this.txtReceived += szData[0];
WaitForData(m_socWorker);
// WindowsApplicationTestSock
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugge
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
public void TransferMessage()
{
try
{
Object objData = txtReceived;
byte[] byData = System.Text.Encoding.ASCII
m_socWorker.Send (byData);
}
catch(SocketException se)
{
//MessageBox.Show (se.Message );
}
}
}
}
}
CLIENT CLASS:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net.Sockets ;
namespace WindowsApplicationTestSock
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Socket m_socWorker;
public string txtReceived="";
public Client()
{
//
// TODO: Add constructor logic here
//
}
public void Connect()
{
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.Inter
//String szIPSelected = txtIPAddress.Text;
String szIPSelected = "192.168.0.155";
//String szPort = txtPort.Text;
String szPort = "8221";
int alPort = System.Convert.ToInt16 (szPort,10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remo
m_socWorker.Connect(remote
}
catch (System.Net.Sockets.Socket
{
//MessageBox.Show ( se.Message );
}
}
public void SendData(string newmessage)
{
try
{
Object objData = newmessage;
byte[] byData = System.Text.Encoding.ASCII
m_socWorker.Send (byData);
}
catch(System.Net.Sockets.S
{
//MessageBox.Show (se.Message );
}
}
public void ReceiveData()
{
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.
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//txtDataRx.Text = szData;
txtReceived = szData;
}
catch(System.Net.Sockets.S
{
//MessageBox.Show (se.Message );
}
}
public void StopClient()
{
m_socWorker.Close ();
}
}
}
}
ASKER
Are you running the same application on both computers?
ASKER
I am running a single Windows application which has 2 classes in it: Server.cs and Client.cs.
I instantiate objects for both the server and the client class.
Then I start the server object to listen for attempts to connect.
Then I have the client object send a message when I click the button on the form.
The problem is the message does not show up until I click the button several times.
I instantiate objects for both the server and the client class.
Then I start the server object to listen for attempts to connect.
Then I have the client object send a message when I click the button on the form.
The problem is the message does not show up until I click the button several times.
ASKER
The problem has been solved in the interim.
I am going to ask CS to close this question and mark it as PAQ.
The solution is posted below.
I am going to ask CS to close this question and mark it as PAQ.
The solution is posted below.
ASKER
The following post....in another related question......also answered this question:
[[from Expert esorf ]]
In general, it's a thread synchronization issue. The problem is that your socket listener is working on another thread. Your display logic (textBox1.Text =) happens before the server has received the entire string.
Add a method in your Form1 called UpdateText() which has the line
textBox1.Text = sv.txtReceived;
Then within your server OnDataReceived() method, after the line
txtReceived = txtReceived + szData;
add a call to the updateText method
myform.UpdateText();
This way, the text box value will be updated as each new character comes in.
There are many ways to generalize this solution, but this should get you started.
[[from Expert esorf ]]
In general, it's a thread synchronization issue. The problem is that your socket listener is working on another thread. Your display logic (textBox1.Text =) happens before the server has received the entire string.
Add a method in your Form1 called UpdateText() which has the line
textBox1.Text = sv.txtReceived;
Then within your server OnDataReceived() method, after the line
txtReceived = txtReceived + szData;
add a call to the updateText method
myform.UpdateText();
This way, the text box value will be updated as each new character comes in.
There are many ways to generalize this solution, but this should get you started.
ASKER
Here is the link to the question that answered THIS question as well.
https://www.experts-exchange.com/questions/21213983/Socket-is-working-but-I-get-the-data-one-char-at-a-time.html
https://www.experts-exchange.com/questions/21213983/Socket-is-working-but-I-get-the-data-one-char-at-a-time.html
Eventhough you are closing the question this might help. I created a simple client server class and what you need for generalizatoin is for the client and server objects to throw events that your forms can "subscribe" to just like the OnLoad and OnInit and other events subscribe to events.
ASKER
You mean like the UpdateText( ) method......or what are you talking about?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public event .... so event is a TYPE ????
ASKER
I am still doing something wrong:
I get the error: The type or namespace name 'DataReceivedEventHandler' could not be found (are you missing a using directive or an assembly reference?)
NEW CLIENT CLASS CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net.Sockets ;
namespace WindowsApplicationTestSock et
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Socket m_socWorker;
public string txtReceived="";
public event DataReceivedEventHandler OnDataReceived;
public Client()
{
//
// TODO: Add constructor logic here
//
}
public void Connect()
{
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.Inter Network, SocketType.Stream, ProtocolType.IP);
//String szIPSelected = txtIPAddress.Text;
String szIPSelected = "192.168.0.155";
//String szPort = txtPort.Text;
String szPort = "8221";
int alPort = System.Convert.ToInt16 (szPort,10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse (szIPSelec ted);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remo teIPAddres s, alPort);
m_socWorker.Connect(remote EndPoint);
}
catch (System.Net.Sockets.Socket Exception se)
{
//MessageBox.Show ( se.Message );
}
}
public void SendData(string newmessage)
{
try
{
Object objData = newmessage;
byte[] byData = System.Text.Encoding.ASCII .GetBytes( objData.To String());
m_socWorker.Send (byData);
}
catch(System.Net.Sockets.S ocketExcep tion se)
{
//MessageBox.Show (se.Message );
}
}
public void ReceiveData()
{
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;
txtReceived = szData;
if( OnDataReceived != null )
OnDataReceived( txtReceived );
}
catch(System.Net.Sockets.S ocketExcep tion se)
{
//MessageBox.Show (se.Message );
}
}
public void StopClient()
{
m_socWorker.Close ();
}
}
}
}
I get the error: The type or namespace name 'DataReceivedEventHandler'
NEW CLIENT CLASS CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Net.Sockets ;
namespace WindowsApplicationTestSock
{
namespace ClassLibraryServerClient
{
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Socket m_socWorker;
public string txtReceived="";
public event DataReceivedEventHandler OnDataReceived;
public Client()
{
//
// TODO: Add constructor logic here
//
}
public void Connect()
{
try
{
//create a new client socket ...
m_socWorker = new Socket(AddressFamily.Inter
//String szIPSelected = txtIPAddress.Text;
String szIPSelected = "192.168.0.155";
//String szPort = txtPort.Text;
String szPort = "8221";
int alPort = System.Convert.ToInt16 (szPort,10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remo
m_socWorker.Connect(remote
}
catch (System.Net.Sockets.Socket
{
//MessageBox.Show ( se.Message );
}
}
public void SendData(string newmessage)
{
try
{
Object objData = newmessage;
byte[] byData = System.Text.Encoding.ASCII
m_socWorker.Send (byData);
}
catch(System.Net.Sockets.S
{
//MessageBox.Show (se.Message );
}
}
public void ReceiveData()
{
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.
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//txtDataRx.Text = szData;
txtReceived = szData;
if( OnDataReceived != null )
OnDataReceived( txtReceived );
}
catch(System.Net.Sockets.S
{
//MessageBox.Show (se.Message );
}
}
public void StopClient()
{
m_socWorker.Close ();
}
}
}
}
ASKER
I forgot to declare the delegate......ooops.......
ASKER
Joe:
The points are yours........but I ask that you help me troubleshoot the implementation of delegates and help me wire it up properly.
The points are yours........but I ask that you help me troubleshoot the implementation of delegates and help me wire it up properly.
Did you need help wiring it up to the main form? or are you talking about your socket class?
What I did as well is write a OnConnect and OnDisconnect event handler that is called from the respective functions. In your client connect method you should have it do it asynchronously like the server does for the ondatareceive method. Then in your client connect callback function you can call the OnConnect event handler (you have to set that up).
To tell if the client has disconnected you need to handle that in your DataReceived callback function and check for a null socket.
Here is what a connect callback for the client would look like
private void OnConnectCallBack(IAsyncRe sult ar)
{
try
{
Socket sock = (Socket) ar.AsyncState;
if( sock.Connected )
{
connected = true; // class variable
// Setup the ReceiveDataCallBack function, which has the signature as this callback function
sock.BeginReceive( receivedBytes, 0, receivedBytes.Length, SocketFlags.None,
new AsyncCallback(ReceiveDataC allBack), sock);
IPEndPoint remote = (IPEndPoint)sock.RemoteEnd Point;
// RemoteSocketEventArgs is my own EventHandler Args class
if( OnConnect != null )
OnConnect(new RemoteSocketEventArgs(
remote.Address.ToString(),
remote.Port));
}
}
catch(Exception ex)
{
connected = false;
throw new Exception(ex.Message);
}
}
What I did as well is write a OnConnect and OnDisconnect event handler that is called from the respective functions. In your client connect method you should have it do it asynchronously like the server does for the ondatareceive method. Then in your client connect callback function you can call the OnConnect event handler (you have to set that up).
To tell if the client has disconnected you need to handle that in your DataReceived callback function and check for a null socket.
Here is what a connect callback for the client would look like
private void OnConnectCallBack(IAsyncRe
{
try
{
Socket sock = (Socket) ar.AsyncState;
if( sock.Connected )
{
connected = true; // class variable
// Setup the ReceiveDataCallBack function, which has the signature as this callback function
sock.BeginReceive( receivedBytes, 0, receivedBytes.Length, SocketFlags.None,
new AsyncCallback(ReceiveDataC
IPEndPoint remote = (IPEndPoint)sock.RemoteEnd
// RemoteSocketEventArgs is my own EventHandler Args class
if( OnConnect != null )
OnConnect(new RemoteSocketEventArgs(
remote.Address.ToString(),
remote.Port));
}
}
catch(Exception ex)
{
connected = false;
throw new Exception(ex.Message);
}
}
ASKER
Joe:
How much of an inconvenience would it be for you to download my source code, get it working, then send it back?
I would be happy to re-post your changes HERE in EE so that others may benefit.
What I am looking for is a stand-alone CLIENT class, and a stand-alone SERVER class that I can invoke.........and have them talk to each other from different applications.
How much of an inconvenience would it be for you to download my source code, get it working, then send it back?
I would be happy to re-post your changes HERE in EE so that others may benefit.
What I am looking for is a stand-alone CLIENT class, and a stand-alone SERVER class that I can invoke.........and have them talk to each other from different applications.
is it really necessary to refresh the entire grid on cancel since you already set the DataSet up in the page load event? Instead how about call the DataGrid DataBind() from the cancel command function.
sorry I just posted the wrong comment to the wrong post
ASKER
I am not using a DataGrid.
ASKER
Gotcha....you had me wondering!!!!
You can do what I did, search the web and educate yourself on what is really involved.
Ultimately what I did was createa one class file with a TcpClientSocket and a TcpServerSocket class. This dll would be referenced in the apps that need them. If it is a chat program then you might use a client and server object for the cleint app and just a server object on the server part.
There are articles on thecodeproject.com that can help
http://www.codeproject.com/csharp/asyncsockets2.asp
if you have specified questiosn then ask them in EE, otherwise I don't really want to write your code from start to finish
Ultimately what I did was createa one class file with a TcpClientSocket and a TcpServerSocket class. This dll would be referenced in the apps that need them. If it is a chat program then you might use a client and server object for the cleint app and just a server object on the server part.
There are articles on thecodeproject.com that can help
http://www.codeproject.com/csharp/asyncsockets2.asp
if you have specified questiosn then ask them in EE, otherwise I don't really want to write your code from start to finish
ASKER
Joe:
I don't want you to write my code for me either.
I would ask my questions if I knew what to ask and how to ask it.
I don't want you to write my code for me either.
I would ask my questions if I knew what to ask and how to ask it.
ASKER
Joe:
All I need is someone to tweak the code so it works:
https://www.experts-exchange.com/questions/21216377/SOCKET-question-client-can-send-to-server-but-server-cannot-send-to-client.html
All I need is someone to tweak the code so it works:
https://www.experts-exchange.com/questions/21216377/SOCKET-question-client-can-send-to-server-but-server-cannot-send-to-client.html
ASKER
http://www.robotzgame.com/junk/robotz.zip