mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
401 lines
35 KiB
HTML
401 lines
35 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.3"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: client_server/tcp_full_duplex_server.c File Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">Algorithms_in_C<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.3 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function(){initNavTree('d1/d6b/tcp__full__duplex__server_8c.html','../../'); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle"><div class="title">tcp_full_duplex_server.c File Reference</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p>Server-side implementation of <a href="http://www.tcpipguide.com/free/t_SimplexFullDuplexandHalfDuplexOperation.htm" target="_blank">TCP Full Duplex Communication</a>
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <arpa/inet.h></code><br />
|
|
<code>#include <netdb.h></code><br />
|
|
<code>#include <netinet/in.h></code><br />
|
|
<code>#include <stdint.h></code><br />
|
|
<code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <string.h></code><br />
|
|
<code>#include <sys/socket.h></code><br />
|
|
<code>#include <sys/types.h></code><br />
|
|
<code>#include <unistd.h></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for tcp_full_duplex_server.c:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d2/d42/tcp__full__duplex__server_8c__incl.svg" width="100%" height="395"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
</div><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:a614217d263be1fb1a5f76e2ff7be19a2"><td class="memItemLeft" align="right" valign="top">#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/tcp__full__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a>   10000</td></tr>
|
|
<tr class="memdesc:a614217d263be1fb1a5f76e2ff7be19a2"><td class="mdescLeft"> </td><td class="mdescRight">For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures. <a href="../../d1/d6b/tcp__full__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">More...</a><br /></td></tr>
|
|
<tr class="separator:a614217d263be1fb1a5f76e2ff7be19a2"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:a7e15c8e2885871839fc2b820dfbdb4ce"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/tcp__full__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">error</a> ()</td></tr>
|
|
<tr class="memdesc:a7e15c8e2885871839fc2b820dfbdb4ce"><td class="mdescLeft"> </td><td class="mdescRight">Utility function used to print an error message to <code>stderr</code>. <a href="../../d1/d6b/tcp__full__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">More...</a><br /></td></tr>
|
|
<tr class="separator:a7e15c8e2885871839fc2b820dfbdb4ce"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/tcp__full__duplex__server_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../d1/d6b/tcp__full__duplex__server_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
|
|
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p >Server-side implementation of <a href="http://www.tcpipguide.com/free/t_SimplexFullDuplexandHalfDuplexOperation.htm" target="_blank">TCP Full Duplex Communication</a> </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/NVombat" target="_blank">NVombat</a> </dd></dl>
|
|
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d1/d6b/tcp__full__duplex__server_8c.html" title="Server-side implementation of TCP Full Duplex Communication">tcp_full_duplex_server.c</a></dd></dl>
|
|
<p>The algorithm is based on the simple TCP client and server model. However, instead of the server only sending and the client only receiving data, The server and client can both send and receive data simultaneously. This is implemented by using the <code>fork</code> function call so that in the server the child process can recieve data and parent process can send data, and in the client the child process can send data and the parent process can receive data. It runs an infinite loop and can send and receive messages indefinitely until the user exits the loop. In this way, the Full Duplex Form of communication can be represented using the TCP server-client model & socket programming </p>
|
|
</div><h2 class="groupheader">Macro Definition Documentation</h2>
|
|
<a id="a614217d263be1fb1a5f76e2ff7be19a2" name="a614217d263be1fb1a5f76e2ff7be19a2"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a614217d263be1fb1a5f76e2ff7be19a2">◆ </a></span>PORT</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">#define PORT   10000</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures. </p>
|
|
<p >For specific bit size values of variables Variable types, several macros, and various functions for performing input and output Variable types, several macros, and various functions for performing general functions Various functions for manipulating arrays of characters For macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs For miscellaneous symbolic constants and types, and miscellaneous functions </p>
|
|
|
|
</div>
|
|
</div>
|
|
<h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a7e15c8e2885871839fc2b820dfbdb4ce" name="a7e15c8e2885871839fc2b820dfbdb4ce"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a7e15c8e2885871839fc2b820dfbdb4ce">◆ </a></span>error()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void error </td>
|
|
<td>(</td>
|
|
<td class="paramname"></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Utility function used to print an error message to <code>stderr</code>. </p>
|
|
<p >It prints <code>str</code> and an implementation-defined error message corresponding to the global variable <code>errno</code>. </p><dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 40</span>{</div>
|
|
<div class="line"><span class="lineno"> 41</span> perror(<span class="stringliteral">"Socket Creation Failed"</span>);</div>
|
|
<div class="line"><span class="lineno"> 42</span> exit(EXIT_FAILURE);</div>
|
|
<div class="line"><span class="lineno"> 43</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">void </td>
|
|
<td class="paramname"></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Main function. </p>
|
|
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
|
|
<p >Variable Declarations</p>
|
|
<p >< socket descriptors - Like file handles but for sockets</p>
|
|
<p >< character arrays to read and store string data for communication</p>
|
|
<p >< basic structures for all syscalls and functions that deal with internet addresses. Structures for handling internet addresses</p>
|
|
<p >size of address</p>
|
|
<p >The TCP socket is created using the socket function</p>
|
|
<p >AF_INET (Family) - it is an address family that is used to designate the type of addresses that your socket can communicate with</p>
|
|
<p >SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides for the bidirectional, reliable, sequenced, and unduplicated flow of data without record boundaries. Aside from the bidirectionality of data flow, a pair of connected stream sockets provides an interface nearly identical to pipes</p>
|
|
<p >0 (Protocol) - Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type</p>
|
|
<p >< Error if the socket descriptor has a value lower than 0 - socket wasnt created</p>
|
|
<p >Server Address Information</p>
|
|
<p >The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area</p>
|
|
<p >We bind the server_addr to the internet address and port number thus giving our socket an identity with an address and port where it can listen for connections</p>
|
|
<p >htons - The htons() function translates a short integer from host byte order to network byte order</p>
|
|
<p >htonl - The htonl() function translates a long integer from host byte order to network byte order</p>
|
|
<p >These functions are necessary so that the binding of address and port takes place with data in the correct format</p>
|
|
<p >This binds the socket descriptor to the server thus enabling the server to listen for connections and communicate with other clients</p>
|
|
<p >If binding is unsuccessful</p>
|
|
<p >This is to listen for clients or connections made to the server</p>
|
|
<p >The limit is currently at 5 but can be increased to listen for more connections</p>
|
|
<p >It listens to connections through the socket descriptor</p>
|
|
<p >When a connection is found, a socket is created and connection is accepted and established through the socket descriptor</p>
|
|
<p >Communication between client and server</p>
|
|
<p >The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area. The variables are emptied and then ready for use</p>
|
|
<p >The fork function call is used to create a child and parent process which run and execute code simultaneously</p>
|
|
<p >The child process is used to receive data and after doing so sleeps for 5 seconds to wait for the parent to send data</p>
|
|
<p >The parent process is used to send data and after doing so sleeps for 5 seconds to wait for the child to receive data</p>
|
|
<p >The server and client can communicate indefinitely till one of them exits the connection</p>
|
|
<p >Since the exchange of information between the server and client takes place simultaneously this represents FULL DUPLEX COMMUNICATION</p>
|
|
<p >Value of 0 is for child process</p>
|
|
<p >Parent process</p>
|
|
<p >Close socket</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 50</span>{<span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 51</span><span class="comment"> /** Variable Declarations */</span></div>
|
|
<div class="line"><span class="lineno"> 52</span> uint32_t sockfd,</div>
|
|
<div class="line"><span class="lineno"> 53</span> conn; <span class="comment">///< socket descriptors - Like file handles but for sockets</span></div>
|
|
<div class="line"><span class="lineno"> 54</span> <span class="keywordtype">char</span> recvbuff[1024],</div>
|
|
<div class="line"><span class="lineno"> 55</span> sendbuff[1024]; <span class="comment">///< character arrays to read and store string data</span><span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 56</span><span class="comment"> /// for communication</span></div>
|
|
<div class="line"><span class="lineno"> 57</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 58</span> <span class="keyword">struct </span>sockaddr_in server_addr,</div>
|
|
<div class="line"><span class="lineno"> 59</span> client_addr; <span class="comment">///< basic structures for all syscalls and functions that</span><span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 60</span><span class="comment"> /// deal with internet addresses. Structures for handling</span></div>
|
|
<div class="line"><span class="lineno"> 61</span><span class="comment"> /// internet addresses</span></div>
|
|
<div class="line"><span class="lineno"> 62</span><span class="comment"></span> socklen_t ClientLen; <span class="comment">/// size of address</span></div>
|
|
<div class="line"><span class="lineno"> 63</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 64</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 65</span><span class="comment"> * The TCP socket is created using the socket function</span></div>
|
|
<div class="line"><span class="lineno"> 66</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 67</span><span class="comment"> * AF_INET (Family) - it is an address family that is used to designate the</span></div>
|
|
<div class="line"><span class="lineno"> 68</span><span class="comment"> * type of addresses that your socket can communicate with</span></div>
|
|
<div class="line"><span class="lineno"> 69</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 70</span><span class="comment"> * SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides</span></div>
|
|
<div class="line"><span class="lineno"> 71</span><span class="comment"> * for the bidirectional, reliable, sequenced, and unduplicated flow of data</span></div>
|
|
<div class="line"><span class="lineno"> 72</span><span class="comment"> * without record boundaries. Aside from the bidirectionality of data flow,</span></div>
|
|
<div class="line"><span class="lineno"> 73</span><span class="comment"> * a pair of connected stream sockets provides an interface nearly identical</span></div>
|
|
<div class="line"><span class="lineno"> 74</span><span class="comment"> * to pipes</span></div>
|
|
<div class="line"><span class="lineno"> 75</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 76</span><span class="comment"> * 0 (Protocol) - Specifies a particular protocol to be used with the</span></div>
|
|
<div class="line"><span class="lineno"> 77</span><span class="comment"> * socket. Specifying a protocol of 0 causes socket() to use an unspecified</span></div>
|
|
<div class="line"><span class="lineno"> 78</span><span class="comment"> * default protocol appropriate for the requested socket type</span></div>
|
|
<div class="line"><span class="lineno"> 79</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">if</span> ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)</div>
|
|
<div class="line"><span class="lineno"> 81</span> {</div>
|
|
<div class="line"><span class="lineno"> 82</span> <a class="code hl_function" href="../../d1/d6b/tcp__full__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">error</a>(); <span class="comment">///< Error if the socket descriptor has a value lower than 0 -</span><span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 83</span><span class="comment"> /// socket wasnt created</span></div>
|
|
<div class="line"><span class="lineno"> 84</span><span class="comment"></span> }</div>
|
|
<div class="line"><span class="lineno"> 85</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 86</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 87</span><span class="comment"> * Server Address Information</span></div>
|
|
<div class="line"><span class="lineno"> 88</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 89</span><span class="comment"> * The bzero() function erases the data in the n bytes of the memory</span></div>
|
|
<div class="line"><span class="lineno"> 90</span><span class="comment"> * starting at the location pointed to, by writing zeros (bytes</span></div>
|
|
<div class="line"><span class="lineno"> 91</span><span class="comment"> * containing '\0') to that area</span></div>
|
|
<div class="line"><span class="lineno"> 92</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 93</span><span class="comment"> * We bind the server_addr to the internet address and port number thus</span></div>
|
|
<div class="line"><span class="lineno"> 94</span><span class="comment"> * giving our socket an identity with an address and port where it can</span></div>
|
|
<div class="line"><span class="lineno"> 95</span><span class="comment"> * listen for connections</span></div>
|
|
<div class="line"><span class="lineno"> 96</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 97</span><span class="comment"> * htons - The htons() function translates a short integer from host byte</span></div>
|
|
<div class="line"><span class="lineno"> 98</span><span class="comment"> * order to network byte order</span></div>
|
|
<div class="line"><span class="lineno"> 99</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 100</span><span class="comment"> * htonl - The htonl() function translates a long integer from host byte</span></div>
|
|
<div class="line"><span class="lineno"> 101</span><span class="comment"> * order to network byte order</span></div>
|
|
<div class="line"><span class="lineno"> 102</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 103</span><span class="comment"> * These functions are necessary so that the binding of address and port</span></div>
|
|
<div class="line"><span class="lineno"> 104</span><span class="comment"> * takes place with data in the correct format</span></div>
|
|
<div class="line"><span class="lineno"> 105</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 106</span> bzero(&server_addr, <span class="keyword">sizeof</span>(server_addr));</div>
|
|
<div class="line"><span class="lineno"> 107</span> server_addr.sin_family = AF_INET;</div>
|
|
<div class="line"><span class="lineno"> 108</span> server_addr.sin_port = htons(<a class="code hl_define" href="../../d1/d6b/tcp__full__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a>);</div>
|
|
<div class="line"><span class="lineno"> 109</span> server_addr.sin_addr.s_addr = htonl(INADDR_ANY);</div>
|
|
<div class="line"><span class="lineno"> 110</span> </div>
|
|
<div class="line"><span class="lineno"> 111</span> printf(<span class="stringliteral">"Server is running...\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 112</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 113</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 114</span><span class="comment"> * This binds the socket descriptor to the server thus enabling the server</span></div>
|
|
<div class="line"><span class="lineno"> 115</span><span class="comment"> * to listen for connections and communicate with other clients</span></div>
|
|
<div class="line"><span class="lineno"> 116</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 117</span> <span class="keywordflow">if</span> (bind(sockfd, (<span class="keyword">struct</span> sockaddr *)&server_addr, <span class="keyword">sizeof</span>(server_addr)) < 0)</div>
|
|
<div class="line"><span class="lineno"> 118</span> {</div>
|
|
<div class="line"><span class="lineno"> 119</span> <a class="code hl_function" href="../../d1/d6b/tcp__full__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">error</a>(); <span class="comment">/// If binding is unsuccessful</span></div>
|
|
<div class="line"><span class="lineno"> 120</span> }<span class="comment"></span></div>
|
|
<div class="line"><span class="lineno"> 121</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 122</span><span class="comment"> * This is to listen for clients or connections made to the server</span></div>
|
|
<div class="line"><span class="lineno"> 123</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 124</span><span class="comment"> * The limit is currently at 5 but can be increased to listen for</span></div>
|
|
<div class="line"><span class="lineno"> 125</span><span class="comment"> * more connections</span></div>
|
|
<div class="line"><span class="lineno"> 126</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 127</span><span class="comment"> * It listens to connections through the socket descriptor</span></div>
|
|
<div class="line"><span class="lineno"> 128</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 129</span> listen(sockfd, 5);</div>
|
|
<div class="line"><span class="lineno"> 130</span> </div>
|
|
<div class="line"><span class="lineno"> 131</span> printf(<span class="stringliteral">"Server is listening...\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 132</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 133</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 134</span><span class="comment"> * When a connection is found, a socket is created and connection is</span></div>
|
|
<div class="line"><span class="lineno"> 135</span><span class="comment"> * accepted and established through the socket descriptor</span></div>
|
|
<div class="line"><span class="lineno"> 136</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 137</span> conn = accept(sockfd, (<span class="keyword">struct</span> sockaddr *)NULL, NULL);</div>
|
|
<div class="line"><span class="lineno"> 138</span> </div>
|
|
<div class="line"><span class="lineno"> 139</span> printf(<span class="stringliteral">"Server is connected...\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 140</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 141</span><span class="comment"> /**</span></div>
|
|
<div class="line"><span class="lineno"> 142</span><span class="comment"> * Communication between client and server</span></div>
|
|
<div class="line"><span class="lineno"> 143</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 144</span><span class="comment"> * The bzero() function erases the data in the n bytes of the memory</span></div>
|
|
<div class="line"><span class="lineno"> 145</span><span class="comment"> * starting at the location pointed to, by writing zeros (bytes</span></div>
|
|
<div class="line"><span class="lineno"> 146</span><span class="comment"> * containing '\0') to that area. The variables are emptied and then</span></div>
|
|
<div class="line"><span class="lineno"> 147</span><span class="comment"> * ready for use</span></div>
|
|
<div class="line"><span class="lineno"> 148</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 149</span><span class="comment"> * The fork function call is used to create a child and parent process</span></div>
|
|
<div class="line"><span class="lineno"> 150</span><span class="comment"> * which run and execute code simultaneously</span></div>
|
|
<div class="line"><span class="lineno"> 151</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 152</span><span class="comment"> * The child process is used to receive data and after doing so</span></div>
|
|
<div class="line"><span class="lineno"> 153</span><span class="comment"> * sleeps for 5 seconds to wait for the parent to send data</span></div>
|
|
<div class="line"><span class="lineno"> 154</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 155</span><span class="comment"> * The parent process is used to send data and after doing so</span></div>
|
|
<div class="line"><span class="lineno"> 156</span><span class="comment"> * sleeps for 5 seconds to wait for the child to receive data</span></div>
|
|
<div class="line"><span class="lineno"> 157</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 158</span><span class="comment"> * The server and client can communicate indefinitely till one of them</span></div>
|
|
<div class="line"><span class="lineno"> 159</span><span class="comment"> * exits the connection</span></div>
|
|
<div class="line"><span class="lineno"> 160</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 161</span><span class="comment"> * Since the exchange of information between the server and client takes</span></div>
|
|
<div class="line"><span class="lineno"> 162</span><span class="comment"> * place simultaneously this represents FULL DUPLEX COMMUNICATION</span></div>
|
|
<div class="line"><span class="lineno"> 163</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 164</span> pid_t <a class="code hl_struct" href="../../d0/d43/structpid.html">pid</a>;</div>
|
|
<div class="line"><span class="lineno"> 165</span> <a class="code hl_struct" href="../../d0/d43/structpid.html">pid</a> = fork();</div>
|
|
<div class="line"><span class="lineno"> 166</span> <span class="keywordflow">if</span> (<a class="code hl_struct" href="../../d0/d43/structpid.html">pid</a> == 0) <span class="comment">/// Value of 0 is for child process</span></div>
|
|
<div class="line"><span class="lineno"> 167</span> {</div>
|
|
<div class="line"><span class="lineno"> 168</span> <span class="keywordflow">while</span> (1)</div>
|
|
<div class="line"><span class="lineno"> 169</span> {</div>
|
|
<div class="line"><span class="lineno"> 170</span> bzero(&recvbuff, <span class="keyword">sizeof</span>(recvbuff));</div>
|
|
<div class="line"><span class="lineno"> 171</span> recv(conn, recvbuff, <span class="keyword">sizeof</span>(recvbuff), 0);</div>
|
|
<div class="line"><span class="lineno"> 172</span> printf(<span class="stringliteral">"\nCLIENT : %s\n"</span>, recvbuff);</div>
|
|
<div class="line"><span class="lineno"> 173</span> sleep(5);</div>
|
|
<div class="line"><span class="lineno"> 174</span> <span class="comment">// break;</span></div>
|
|
<div class="line"><span class="lineno"> 175</span> }</div>
|
|
<div class="line"><span class="lineno"> 176</span> }</div>
|
|
<div class="line"><span class="lineno"> 177</span> <span class="keywordflow">else</span> <span class="comment">/// Parent process</span></div>
|
|
<div class="line"><span class="lineno"> 178</span> {</div>
|
|
<div class="line"><span class="lineno"> 179</span> <span class="keywordflow">while</span> (1)</div>
|
|
<div class="line"><span class="lineno"> 180</span> {</div>
|
|
<div class="line"><span class="lineno"> 181</span> bzero(&sendbuff, <span class="keyword">sizeof</span>(sendbuff));</div>
|
|
<div class="line"><span class="lineno"> 182</span> printf(<span class="stringliteral">"\nType message here: "</span>);</div>
|
|
<div class="line"><span class="lineno"> 183</span> fgets(sendbuff, 1024, stdin);</div>
|
|
<div class="line"><span class="lineno"> 184</span> send(conn, sendbuff, strlen(sendbuff) + 1, 0);</div>
|
|
<div class="line"><span class="lineno"> 185</span> printf(<span class="stringliteral">"\nMessage Sent!\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 186</span> sleep(5);</div>
|
|
<div class="line"><span class="lineno"> 187</span> <span class="comment">// break;</span></div>
|
|
<div class="line"><span class="lineno"> 188</span> }</div>
|
|
<div class="line"><span class="lineno"> 189</span> }</div>
|
|
<div class="line"><span class="lineno"> 190</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 191</span><span class="comment"> /// Close socket</span></div>
|
|
<div class="line"><span class="lineno"> 192</span><span class="comment"></span> close(sockfd);</div>
|
|
<div class="line"><span class="lineno"> 193</span> printf(<span class="stringliteral">"Server is offline...\n"</span>);</div>
|
|
<div class="line"><span class="lineno"> 194</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 195</span>}</div>
|
|
<div class="ttc" id="astructpid_html"><div class="ttname"><a href="../../d0/d43/structpid.html">pid</a></div><div class="ttdoc">PID Controller.</div><div class="ttdef"><b>Definition:</b> pid.c:31</div></div>
|
|
<div class="ttc" id="atcp__full__duplex__server_8c_html_a614217d263be1fb1a5f76e2ff7be19a2"><div class="ttname"><a href="../../d1/d6b/tcp__full__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a></div><div class="ttdeci">#define PORT</div><div class="ttdoc">For the type in_addr_t and in_port_t For structures returned by the network database library - format...</div><div class="ttdef"><b>Definition:</b> tcp_full_duplex_server.c:31</div></div>
|
|
<div class="ttc" id="atcp__full__duplex__server_8c_html_a7e15c8e2885871839fc2b820dfbdb4ce"><div class="ttname"><a href="../../d1/d6b/tcp__full__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">error</a></div><div class="ttdeci">void error()</div><div class="ttdoc">Utility function used to print an error message to stderr.</div><div class="ttdef"><b>Definition:</b> tcp_full_duplex_server.c:39</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/tcp__full__duplex__server_8c_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="158" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../dir_d20ad0b8de644c123692ba5582cc7a7b.html">client_server</a></li><li class="navelem"><a class="el" href="../../d1/d6b/tcp__full__duplex__server_8c.html">tcp_full_duplex_server.c</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|