-
sockets ...
hi there
I have created a simple app which accepts incoming calls from clients.
basically, when the application "listens" to the tcp port - the application just hangs, well it doesnt hang - it's doing its job however when the client says to the server "disconnect" - the server disconnects that session and all is normal - in other words it unfreezes the form
what can i do in order for this "freeze" event not to happen on the server app?
I know it has something to do with asynch? but i cannot find any understandable code to implement it into my solution
any ideas?
-
Re: sockets ...
You might want to make a separate thread for the listening. That is what I am doing in my games. Netowrk stuff is usualy slow, or actualy any big IO event like printing and and so on. So listeing for incomming stuff in a separate thread might solve your problem. Just remember to look for any critical sections.
- ØØ -
-
Re: sockets ...
ok... so how do i go about doing that sort of stuff? :)
currently this is my code
Code:
listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 33);
listener.Start();
Socket theSocket = listener.AcceptSocket();
try
{
if (theSocket.Connected)
{
txtEvents.Text = "Cool, the client connected!";
}
}
catch (Exception ex)
{
txtEvents.Text += ex.ToString();
}
-
Re: sockets ...
As NoteMe said, you probably want to do the communications on a different thread, not the main GUI thread. When you use a blocking method in your main GUI thread, you're going to, well, block, so the GUI freezes. You can do this a couple ways.
One, just spawn a new thread and do your work from there. Two, use the asynchronous socket methods. There's lots of info on threading in MSDN, for the asynchronous sockets methods, check out the MSDN topics "Using an Asynchronous Server Socket", "Asynchronous Server Socket Example", "Using an Asynchronous Client Socket" and "Asynchronous Client Socket Example".
Mike
-
Re: sockets ...
hmm ok
another Q - is there no such event that I could implement/inherit which has the ability to detect incoming data? how would I write a listener which just basically...listens to the socket?
-
Re: sockets ...
You could do this (at least) two ways.
If you're using blocking (synchronous) methods (this would make sense if you're doing this in the non-GUI thread), just call .Receive. It will block on .Receive and proceed to the next line when you get some data.
If you're using non-blocking (asynchronous) methods, you'd call .BeginReceive, then your code in your AsyncCallback delegate would get fired. If you look at the example in "Using an Asynchronous Server Socket", the AsyncCallback is named "readCallback".
You may want to raise an event or do something, then, when you actually get some data.
Mike
-
Re: sockets ...
hope you can help again :)
I am able to successfully send and recieve data to and from server and client
thing is, if i send data from server (perhaps client too) I can only send it once... if i try to send different data then it seems to do it but nothing ever happens... seems to have been sent from the server but the client never recieves it
the only way to make it recieve data is to connect to the server again... so in order to send data you have to reconnect everytime for every data you wanna recieve.
whats going on?
server:
Code:
private void cmdSendMessageToClient_Click(object sender, System.EventArgs e)
{
byte[] msg = System.Text.Encoding.ASCII.GetBytes(txtMsgToSendClient.Text);
this.SendMessageToClient(msg);
}
private void SendMessageToClient(byte[] msg)
{
if (this.client.Connected)
{
this.client.Send(msg, 0, msg.Length, SocketFlags.None);
}
}
client:
Code:
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.sendingThread = new Thread(new ThreadStart(Send));
this.sendingThread.Start();
}
void Send()
{
this.client = new TcpClient("technica-pribx6", 33);
bool readData = false;
NetworkStream ns = this.client.GetStream();
if(ns.CanWrite)
{
try
{
string msg = "hey - from client machine!";
byte[] byteMessage = System.Text.Encoding.ASCII.GetBytes(msg.ToCharArray());
ns.Write(byteMessage, 0, byteMessage.Length);
ns.Flush();
}
catch (IOException ex)
{
MessageBox.Show(ex.Message + " " + ex.InnerException);
Application.Exit();
}
}
while (!readData && ns.CanRead)
{
if(ns.DataAvailable)
{
byte[] incomingData = new byte[64];
int i = ns.Read(incomingData, 0, incomingData.Length);
string decodeDataToString = System.Text.Encoding.ASCII.GetString(incomingData, 0, incomingData.Length);
lblInfo.Text += " " + decodeDataToString + " ";
readData = true;
}
}
}
-
Re: sockets ...
Make sure that you connect your socket and then leave the socket open. Looks like in your client code anyway, you're reconnecting with every send. Just re-use the same socket instead of creating a new one.
-
Re: sockets ...
of course. I also had "readData = true" which forced it to exit the loop.
I have however now made 2 threads, one for sending and one for recieving.
Problem now is that I cannot seem to type any messages in the textbox on the client side and cannot press any buttons - i guess its because it is endlessly going in the while loop.
Anyway I will revert back to the code I had before modifying it to having 2 threads and try your solution :)
if i have more problems, i shall post it here.
thanks for your help - much appreciated :)
-
Re: sockets ...
ok well threading was a bad idea as it slowed down the app a fair bit on the mobile device
it all seems to work well when you are threading but it hangs up after a few msgs sent or recieved
if i take out thread it is faster but the problem occurs where the application will just hang (no errors) and just does nothing.
any ideas? could it be the while loop?
wierd thing is, if i put a debug point anywhere, when it gets to that point and i press F5 - it works fine... until i take the point off the statement