C#,异步套接字服务器/客户端,StackOverflowException
我正在用 C# 编写异步服务器和客户端。我从 MSDN 中获取了示例代码,对其进行了多次修改并使其能够发送和接收消息。我尝试将 5 个及更多客户端连接到服务器,它可以工作,但在第 98 次迭代中,每个客户端都会抛出异常 StackOverflow。谁能解释一下为什么我会收到此错误?我在 MSDN 中读到我的问题是无限循环,但我不明白如何更改我的代码并在没有循环的情况下编写它。
该服务器客户端应该在多人赛车游戏中使用,我需要每秒多次发送和接收每个玩家的坐标。我怎样才能做到没有无限循环?
这是我的代码:
服务器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Server
{
Socket main_tcp_Sock;
private static ManualResetEvent acceptDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent recvDone = new ManualResetEvent(false);
private static ManualResetEvent closeDone = new ManualResetEvent(false);
//int cl_Count = 0;
List<StateObject> connection_List = new List<StateObject>();
private static String response = String.Empty;
public class StateObject
{
public Socket current_Socket = null;
public byte[] data = new byte[256];
public string id = string.Empty;
}
public Server()
{
Server_Start();
}
public void Server_Start()
{
//Creating socket
main_tcp_Sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
//Bind socket
try
{
main_tcp_Sock.Bind(ipLocal);
Console.WriteLine("Server has started successfully!");
//Start listening
main_tcp_Sock.Listen(100);
while (true)
{
acceptDone.Reset();
Console.WriteLine("Waiting for a connection...");
//AsyncAccept
main_tcp_Sock.BeginAccept(new AsyncCallback(On_Connect), main_tcp_Sock);
acceptDone.WaitOne();
Console.WriteLine("\nPress any button to continue...\n\n");
Console.ReadKey(true);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public void On_Connect(IAsyncResult asyn)
{
try
{
Socket listener = (Socket)asyn.AsyncState;
Socket handler = listener.EndAccept(asyn);
acceptDone.Set();
StateObject connection = new StateObject();
connection.current_Socket = handler;
if (!connection_List.Contains(connection))
{
lock (connection_List)
{
connection_List.Add(connection);
connection.id = "00" + connection_List.Count.ToString() + " ";
}
}
recvDone.Reset();
Receive(connection.current_Socket);
recvDone.WaitOne();
sendDone.Reset();
Send(connection.current_Socket, response);
sendDone.WaitOne();
closeDone.Reset();
Socket_Close(connection.current_Socket);
closeDone.WaitOne();
}
catch (Exception e)
{
Console.WriteLine("On_Connect Error: {0}", e.ToString());
Console.ReadKey(true);
}
}
public void Receive(Socket handler)
{
try{
StateObject connection = new StateObject();
connection.current_Socket = handler;
connection.current_Socket.BeginReceive(connection.data, 0, connection.data.Length, 0,
new AsyncCallback(On_Receive), connection);
}
catch (Exception e){
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public void On_Receive(IAsyncResult asyn)
{
string content = "";
string temp = "";
StateObject connection = (StateObject)asyn.AsyncState;
Socket handler = connection.current_Socket;
int size = handler.EndReceive(asyn);
Console.WriteLine("ConnID from receive: " + connection.id);
if (size > 0)
{
temp += Encoding.ASCII.GetString(connection.data);
}
if (temp.IndexOf("<EOF>") > -1)
{
content += temp.Substring(0, temp.IndexOf("\0"));
Console.WriteLine("Read {0} bytes from socket. \nMessage: {1}", content.Length, content);
lock (connection_List)
{
foreach (StateObject conn in connection_List)
{
if (conn != connection)
{
content.Insert(0, connection.id);
response = content;
}
}
}
recvDone.Set();
}
else
{
handler.BeginReceive(connection.data, 0, connection.data.Length, 0, new AsyncCallback(On_Receive), connection);
}
}
public void Send(Socket handler, String message)
{
byte[] data = Encoding.ASCII.GetBytes(message);
handler.BeginSend(data, 0, data.Length, 0, new AsyncCallback(On_Send), handler);
}
public void On_Send(IAsyncResult result)
{
try
{
StateObject state = new StateObject();
Socket handler = (Socket)result.AsyncState;
state.current_Socket = handler;
int size = state.current_Socket.EndSend(result);
if (size > 0)
{
sendDone.Set();
}
else state.current_Socket.BeginSend(state.data, 0, state.data.Length, SocketFlags.None,
new AsyncCallback(On_Send), state);
Console.WriteLine("Bytes sent to client: {0}", size);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine("On_Send e, error: " + e.ToString());
Console.ReadKey(true);
}
}
public void Socket_Close(Socket sock)
{
sock.LingerState = new LingerOption(true, 3);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
closeDone.Set();
}
}
和客户端:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
public static int count = 0;
// The port number for the remote device.
private const int port = 2000;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
private static ManualResetEvent closeDone = new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static void StartClient()
{
// Connect to a remote device.
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Start(client, remoteEP);
}
public static void Start(Socket client, EndPoint remoteEP)
{
try
{
while (true)
{
/*if (count >= 30)
{
Thread.Sleep(1000);
if (count >= 100)
{
count = 0;
Thread.Sleep(1500);
}
}*/
Console.WriteLine(count);
connectDone.Reset();
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
sendDone.Reset();
Send(client, "Some text and <EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
receiveDone.Reset();
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
closeDone.Reset();
Socket_Close(client);
closeDone.WaitOne();
++count;
}
}
catch (ObjectDisposedException)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
Start(sock, remote);
}
catch (SocketException)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
Start(sock, remote);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void Send(Socket client, String data)
{
try
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public static void Socket_Close(Socket sock)
{
try
{
sock.LingerState = new LingerOption(true, 3);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
closeDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
}
我是 C# 新手。请帮助某人。
I'm writing an asynchronous server and client with C#. I took sample code from MSDN, modified and made it to send and receive messages several times. I tried to connect 5 and more clients to server and it worked but on 98th iteration each client throws an exception StackOverflow. Can anybody explain me why I get this error? I've read in MSDN that my problem in infinite loops but I don't understand how can I change my code and write it without loops.
This server-client should be used in multiplayer racing game and I need to send and receive the coordinates of each player several times per second. How can I do it without infinite loops?
Here is my code:
SERVER:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class Server
{
Socket main_tcp_Sock;
private static ManualResetEvent acceptDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent recvDone = new ManualResetEvent(false);
private static ManualResetEvent closeDone = new ManualResetEvent(false);
//int cl_Count = 0;
List<StateObject> connection_List = new List<StateObject>();
private static String response = String.Empty;
public class StateObject
{
public Socket current_Socket = null;
public byte[] data = new byte[256];
public string id = string.Empty;
}
public Server()
{
Server_Start();
}
public void Server_Start()
{
//Creating socket
main_tcp_Sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
//Bind socket
try
{
main_tcp_Sock.Bind(ipLocal);
Console.WriteLine("Server has started successfully!");
//Start listening
main_tcp_Sock.Listen(100);
while (true)
{
acceptDone.Reset();
Console.WriteLine("Waiting for a connection...");
//AsyncAccept
main_tcp_Sock.BeginAccept(new AsyncCallback(On_Connect), main_tcp_Sock);
acceptDone.WaitOne();
Console.WriteLine("\nPress any button to continue...\n\n");
Console.ReadKey(true);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public void On_Connect(IAsyncResult asyn)
{
try
{
Socket listener = (Socket)asyn.AsyncState;
Socket handler = listener.EndAccept(asyn);
acceptDone.Set();
StateObject connection = new StateObject();
connection.current_Socket = handler;
if (!connection_List.Contains(connection))
{
lock (connection_List)
{
connection_List.Add(connection);
connection.id = "00" + connection_List.Count.ToString() + " ";
}
}
recvDone.Reset();
Receive(connection.current_Socket);
recvDone.WaitOne();
sendDone.Reset();
Send(connection.current_Socket, response);
sendDone.WaitOne();
closeDone.Reset();
Socket_Close(connection.current_Socket);
closeDone.WaitOne();
}
catch (Exception e)
{
Console.WriteLine("On_Connect Error: {0}", e.ToString());
Console.ReadKey(true);
}
}
public void Receive(Socket handler)
{
try{
StateObject connection = new StateObject();
connection.current_Socket = handler;
connection.current_Socket.BeginReceive(connection.data, 0, connection.data.Length, 0,
new AsyncCallback(On_Receive), connection);
}
catch (Exception e){
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public void On_Receive(IAsyncResult asyn)
{
string content = "";
string temp = "";
StateObject connection = (StateObject)asyn.AsyncState;
Socket handler = connection.current_Socket;
int size = handler.EndReceive(asyn);
Console.WriteLine("ConnID from receive: " + connection.id);
if (size > 0)
{
temp += Encoding.ASCII.GetString(connection.data);
}
if (temp.IndexOf("<EOF>") > -1)
{
content += temp.Substring(0, temp.IndexOf("\0"));
Console.WriteLine("Read {0} bytes from socket. \nMessage: {1}", content.Length, content);
lock (connection_List)
{
foreach (StateObject conn in connection_List)
{
if (conn != connection)
{
content.Insert(0, connection.id);
response = content;
}
}
}
recvDone.Set();
}
else
{
handler.BeginReceive(connection.data, 0, connection.data.Length, 0, new AsyncCallback(On_Receive), connection);
}
}
public void Send(Socket handler, String message)
{
byte[] data = Encoding.ASCII.GetBytes(message);
handler.BeginSend(data, 0, data.Length, 0, new AsyncCallback(On_Send), handler);
}
public void On_Send(IAsyncResult result)
{
try
{
StateObject state = new StateObject();
Socket handler = (Socket)result.AsyncState;
state.current_Socket = handler;
int size = state.current_Socket.EndSend(result);
if (size > 0)
{
sendDone.Set();
}
else state.current_Socket.BeginSend(state.data, 0, state.data.Length, SocketFlags.None,
new AsyncCallback(On_Send), state);
Console.WriteLine("Bytes sent to client: {0}", size);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine("On_Send e, error: " + e.ToString());
Console.ReadKey(true);
}
}
public void Socket_Close(Socket sock)
{
sock.LingerState = new LingerOption(true, 3);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
closeDone.Set();
}
}
And client:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
public static int count = 0;
// The port number for the remote device.
private const int port = 2000;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
private static ManualResetEvent closeDone = new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static void StartClient()
{
// Connect to a remote device.
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Start(client, remoteEP);
}
public static void Start(Socket client, EndPoint remoteEP)
{
try
{
while (true)
{
/*if (count >= 30)
{
Thread.Sleep(1000);
if (count >= 100)
{
count = 0;
Thread.Sleep(1500);
}
}*/
Console.WriteLine(count);
connectDone.Reset();
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
sendDone.Reset();
Send(client, "Some text and <EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
receiveDone.Reset();
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
closeDone.Reset();
Socket_Close(client);
closeDone.WaitOne();
++count;
}
}
catch (ObjectDisposedException)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
Start(sock, remote);
}
catch (SocketException)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
Start(sock, remote);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void Send(Socket client, String data)
{
try
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public static void Socket_Close(Socket sock)
{
try
{
sock.LingerState = new LingerOption(true, 3);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
closeDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey(true);
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
}
I'm new in C#. Pls, help somebody.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
while (true)
永远不会停止...你必须做类似while (foo) { if (somecondition) foo=false }
编辑:也许本教程会有所帮助
< a href="http://www.codeguru.com/csharp/csharp/cs_date_time/timeroutines/article.php/c7763" rel="nofollow noreferrer">http://www.codeguru.com/csharp/csharp/cs_date_time /timeroutines/article.php/c7763
片段:
while (true)
will never stop... you must do something likewhile (foo) { if (somecondition) foo=false }
EDIT: Maybe this tutorial will help
http://www.codeguru.com/csharp/csharp/cs_date_time/timeroutines/article.php/c7763
snippet:
堆栈是后进先出队列。当您进行函数调用时,需要保存与您所在函数相关的状态(例如局部变量),以便程序可以处理被调用的函数。当被调用函数返回时,必须恢复调用函数的状态,因此从堆栈中检索它。在正常的程序中,当您调用越来越多的嵌套函数时,堆栈会增长,但当它们返回时,堆栈会再次缩小。
当然,堆栈不是无限的,事实上,通常会分配固定数量的内存,因此,如果您的程序中嵌套函数调用的级别不断增长,最终堆栈将满,您将收到堆栈溢出异常。
演示这一点的最简单方法是编写一个始终递归调用自身的函数。以下内容总是会造成堆栈溢出:
当然,我们也可以编写递归程序而不会出现此问题:
这是一项非常强大的技术。编写此类代码的技巧是让终止条件正确!
当这一切都发生在一个函数中时,逻辑很容易看出,但是,您也可以有类似的逻辑,其中一个函数调用另一个函数,另一个函数调用另一个函数,另一个函数调用第一个函数。
当您使用事件处理代码时,情况会变得更糟,因为函数调用看起来不再像函数调用。假设一个函数触发了一个触发事件处理程序的条件,并且事件处理程序触发了一些调用第一个函数的代码,您还会有循环函数调用,如果终止条件不正确,您将得到堆栈溢出。
不管怎样 - 我只想+1这个问题,因为它是关于堆栈溢出的!
A stack is a Last-In-First-Out queue. When you make a function call, the state relevant to the function you are in (e.g. local variables) needs to be saved so that the programme can process the called function. When the called function returns, the state of the calling function must be restored, so it is retrieved from the stack. In normal programmes, the stack grows as you invoke more and more deeply nested functions, but then shrinks again as they return.
Of course, the stack is not limitless, and in fact is usually allocated a fixed amount of memory, so if you have a programme where the level of nested function calls keeps growing, eventually the stack will be full, and you'll receive a stack overflow exception.
The easiest way to demonstrate this is to write a function that always calls itself recursively. The following will always create a stack overflow:
Of course, we can also write recursive programmes without this problem:
and this is a very powerful technique. The trick with writing such code is to get your terminating condition right!
When this all takes place in one function, the logic is easy to see, however, you can also have similar logic where one function calls another, which calls another, which calls the first function.
When you're working with event handling code, it gets worse, because the function calls don't look like function calls any more. Let's say one function triggers a condition which triggers an event handler, and the event handler triggers some code which calls the first function, you would also have cyclic function invocations, and if the terminating condition isn't right, you would get a stack overflow.
Anyway - I just want to +1 the question because it's about a Stack Overflow!