Tom Knowlton
asked on
SOCKET question - client can send to server but server cannot send to client?
I have written a small TEST application to acquaint myself with how windows sockets work.
Through trial and error and help from tutorial sites and from EE, I have successfully been able to send a message from a client socket to a server socket.
But I cannot get the server to send a message to the client.
Here is a screenshot depicting the problem:
http://www.robotzgame.com/junk/sockettest.gif
Here is the full source code for my test app:
http://www.robotzgame.com/junk/TestSocket.zip
I am not sure which code to publish.
Expert "Joeisanerd" was trying to help me with delegates creation in another thread, btw. Joe if you are reading this....HELP!!!! :)
Through trial and error and help from tutorial sites and from EE, I have successfully been able to send a message from a client socket to a server socket.
But I cannot get the server to send a message to the client.
Here is a screenshot depicting the problem:
http://www.robotzgame.com/junk/sockettest.gif
Here is the full source code for my test app:
http://www.robotzgame.com/junk/TestSocket.zip
I am not sure which code to publish.
Expert "Joeisanerd" was trying to help me with delegates creation in another thread, btw. Joe if you are reading this....HELP!!!! :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not that I know of right off hand, but Callback functions are fairly simple to use and understand
For Asynchronous Socket communication you use Callbacks so in the Connect method tell your socket to not user blocking
// Connect to server non-Blocking method
mySocket.Blocking = false;
Then make a call to the Sockets BeginConnect, which expects a Callback function as the parameter (no need to call BeginAccept)
// epServer is the EndPoint variable that contains the server information that you are connecting to
// OnConnectCallBack will be called when the there is a successfull connection made to the server
// mySocket is the parameter to the Callback function, in this case the socket we created.
mySocket.BeginConnect( epServer, new AsyncCallback( OnConnectCallBack ), mySocket );
Now you write the OnConnectCallBack which will then call the Sockets BeginReceive with the OnDataReceived Callback
For Asynchronous Socket communication you use Callbacks so in the Connect method tell your socket to not user blocking
// Connect to server non-Blocking method
mySocket.Blocking = false;
Then make a call to the Sockets BeginConnect, which expects a Callback function as the parameter (no need to call BeginAccept)
// epServer is the EndPoint variable that contains the server information that you are connecting to
// OnConnectCallBack will be called when the there is a successfull connection made to the server
// mySocket is the parameter to the Callback function, in this case the socket we created.
mySocket.BeginConnect( epServer, new AsyncCallback( OnConnectCallBack ), mySocket );
Now you write the OnConnectCallBack which will then call the Sockets BeginReceive with the OnDataReceived Callback
ASKER
Thanks.
ASKER
This tutorial looks promising:
http://www.codeproject.com/csharp/socketsincs.asp
Is Remoting easier?
http://www.codeproject.com/csharp/socketsincs.asp
Is Remoting easier?
Remoting is a P.I.T.A. (pain in ...)
It worked nicely in 1.0 but .net 1.1 has added security and it kinda sucks for two way communication (chatting).
It worked nicely in 1.0 but .net 1.1 has added security and it kinda sucks for two way communication (chatting).
ASKER
Okay.
I am looking for the best tutorial available....one that takes you step-by-step-by-step.....v ery simple......and the progression makes sense.
I am looking for the best tutorial available....one that takes you step-by-step-by-step.....v
Class variables
private int iPort;
private string strServer = "";
private byte[] receivedBytes = new byte[256];
public Socket sClient = null;
private bool connected = false;
// Callback function for datareceive
public void ReceiveDataCallBack( IAsyncResult ar )
{
Socket sock = null;
try
{
connected = true;
// Socket was the passed in object
sock = (Socket) ar.AsyncState;
if( !sock.Connected )
{
if( OnDisconnect != null )
OnDisconnect("");
return;
}
int nBytesRec = sock.EndReceive( ar );
if( nBytesRec > 0 )
{
// Wrote the data to the List
string sReceived = Encoding.ASCII.GetString( receivedBytes, 0, nBytesRec );
// If the connection is still usable restablish the callback
sock.BeginReceive( receivedBytes, 0, receivedBytes.Length, SocketFlags.None,
new AsyncCallback(ReceiveDataC allBack), sock);
IPEndPoint remote = (IPEndPoint)sock.RemoteEnd Point;
if( OnDataReceived != null )
OnDataReceived(new RemoteSocketEventArgs(
remote.Address.ToString(),
remote.Port), sReceived);
}
else
{
// If no data was recieved then the connection is probably dead
sock.Shutdown( SocketShutdown.Both );
sock.Close();
connected = false;
}
}
catch(Exception ex)
{
connected = false;
if( sock!=null )
sock.Close();
if( OnDisconnect != null )
OnDisconnect(ex.Message);
}
}
private int iPort;
private string strServer = "";
private byte[] receivedBytes = new byte[256];
public Socket sClient = null;
private bool connected = false;
// Callback function for datareceive
public void ReceiveDataCallBack( IAsyncResult ar )
{
Socket sock = null;
try
{
connected = true;
// Socket was the passed in object
sock = (Socket) ar.AsyncState;
if( !sock.Connected )
{
if( OnDisconnect != null )
OnDisconnect("");
return;
}
int nBytesRec = sock.EndReceive( ar );
if( nBytesRec > 0 )
{
// Wrote the data to the List
string sReceived = Encoding.ASCII.GetString( receivedBytes, 0, nBytesRec );
// If the connection is still usable restablish the callback
sock.BeginReceive( receivedBytes, 0, receivedBytes.Length, SocketFlags.None,
new AsyncCallback(ReceiveDataC
IPEndPoint remote = (IPEndPoint)sock.RemoteEnd
if( OnDataReceived != null )
OnDataReceived(new RemoteSocketEventArgs(
remote.Address.ToString(),
remote.Port), sReceived);
}
else
{
// If no data was recieved then the connection is probably dead
sock.Shutdown( SocketShutdown.Both );
sock.Close();
connected = false;
}
}
catch(Exception ex)
{
connected = false;
if( sock!=null )
sock.Close();
if( OnDisconnect != null )
OnDisconnect(ex.Message);
}
}
ASKER
what is that you just posted?
the OnDataReceived callback that I use
ASKER
Oh.....okay.
===================
Listen.....it has become apparent to me that I am way out of my league here, trying to understand sockets. What I hoped would be like a 5 minute thing has turned out to be much harder than I thought.
All I want is the simplest way to communication between two programs using the internet. Whether I use Sockets or something else...I don't care.
I do not think that sockets are the best way to go, necessarily....I am open to suggestions.
WHERE THIS IS ALL HEADED:
Once I finally figure out a way to send a simple text message between two applications using the internet........I will be sending moves from one client to the other, as in:
Tom moves from square 1 to square 5
Bob moves from square 6 to square 9
Something like that.
===================
Listen.....it has become apparent to me that I am way out of my league here, trying to understand sockets. What I hoped would be like a 5 minute thing has turned out to be much harder than I thought.
All I want is the simplest way to communication between two programs using the internet. Whether I use Sockets or something else...I don't care.
I do not think that sockets are the best way to go, necessarily....I am open to suggestions.
WHERE THIS IS ALL HEADED:
Once I finally figure out a way to send a simple text message between two applications using the internet........I will be sending moves from one client to the other, as in:
Tom moves from square 1 to square 5
Bob moves from square 6 to square 9
Something like that.
ASKER
In C# ... is there a wrapper around Sockets that hide the internals of sockets?
ASKER
Are there some good C# books that focus on Internet communication in C#? Like sockets, remoting, etc.?