Hi. First of all I want to thank you all guys for helping me.
What I am trying to make is a little program that will make thins on client when server sends a string. The problem is when I connect for the first time everything works okay,but when I try to connect again - after I closed the program, I can't send the string again for example when I close the client I need to restart the server so I can send again strings,maybe it happens becuase I am trying to connect with the same IP? if yes,is there a way to fix this? Here is the code:
SERVER - http://pastebin.com/m6829ae1f
CLIENT - http://pastebin.com/m704bbd7e
Thanks!
Loading
or nakashPosted Feb 18, 2010, 10:46 AM
I want to do this because sometimes when I send IP to send something to and I want to get this IP from another client, I mean for example when I type "kick 127.0.0.1" it sends a string to the IP 127.0.0.1. Thanx
Sam HobbsPosted Feb 17, 2010, 3:53 PM
or nakashPosted Feb 17, 2010, 7:04 AM
for example I sent "make one" the server will take the one BUT only if the first word is make.
I hope I made myself clear ;p thanks again Sam!
EDIT: Oh, I forgot.. how to send data to a specific IP?
Sam HobbsPosted Feb 17, 2010, 2:54 AM
I hope that sockets are more reliable. It is highly unlikely that the Microsoft software for sockets has problems. Just be sure to follow the documentation.
Oh, okay; now I see. the WaitForData function is one you wrote, correct? So in your connection callback, you do a EndAccept, then get the data, then do BeginAccept to wait for another connection, correct? I assume that your WaitForData function does more than waits; it also gets the data I assume. What you are doing is a lot like what I am doing for pipes, except I have a BeginWaitForConnection instead of BeginAccept and such. So yes, it seems correct that you need the BeginAccept.
or nakashPosted Feb 16, 2010, 6:20 PM
By the way, I think I solve the problem - so far it worked perfectly.
Is this a good code? I mean will it crash etc..
public void OnClientConnect(IAsyncResult asyn)
{
try
{
m_socWorker = m_socListener.EndAccept(asyn);
WaitForData(m_socWorker);
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
SendData("doitall");
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
I underlind'ed the line I added.
one more question - How to send data to a specific IP Address?
Sam HobbsPosted Feb 16, 2010, 1:06 PM