Simulation of dislocation dynamics enables researchers and scientists to explore the plastic behavior of crystalline materials under loading. Analytic solutions for the stress field due to a linear dislocation segment near a free surface are case-specific, e.g. dealing with either a horizontal segment or a vertical segment, and therefore hard to implement in time-dependent dislocation dynamics simulations as different dislocation segments could be differently oriented. This article presents a generalized numerical framework to find the stress field beneath a free surface due to the presence of a dislocation segment. The framework can be expanded to non-flat surfaces. Also, three-dimensional discrete dislocation dynamics simulations are performed here, which clearly show the effect of free surfaces on the flow stress of a material.