mirror of
https://github.com/TheAlgorithms/C
synced 2024-12-12 22:57:06 +03:00
386 lines
37 KiB
HTML
386 lines
37 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.2"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: client_server/tcp_half_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 style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<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.2 -->
|
|
<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('d6/d2b/tcp__half__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_half_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 Half Duplex Communication</a>
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><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_half_duplex_server.c:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../dd/d3d/tcp__half__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="../../d6/d2b/tcp__half__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a>   8100</td></tr>
|
|
<tr class="memdesc:a614217d263be1fb1a5f76e2ff7be19a2"><td class="mdescLeft"> </td><td class="mdescRight">For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures. <a href="../../d6/d2b/tcp__half__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="../../d6/d2b/tcp__half__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="../../d6/d2b/tcp__half__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="../../d6/d2b/tcp__half__duplex__server_8c.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../d6/d2b/tcp__half__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 Half 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="../../d6/d2b/tcp__half__duplex__server_8c.html" title="Server-side implementation of TCP Half Duplex Communication">tcp_half_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 data but only one at a time. This is implemented by using a particular ordering of the <code>send()</code> and <code>recv()</code> functions. When one of the clients or servers is sending, the other can only receive and vice-versa. In this way, the Half 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   8100</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>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"><a id="l00037" name="l00037"></a><span class="lineno"> 37</span>{</div>
|
|
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno"> 38</span> perror(<span class="stringliteral">"Socket Creation Failed"</span>);</div>
|
|
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"> 39</span> exit(EXIT_FAILURE);</div>
|
|
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno"> 40</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 >< asic structures for all syscalls and functions that deal with internet addresses. Structures for handling internet addresses</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 >Domain/Family to be used</p>
|
|
<p >Port to be used</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 >First the SERVER is prompted to type a message which is read from stdin and then sent over the connection that was established - the socket</p><ul>
|
|
<li>to be received by the client (send())</li>
|
|
</ul>
|
|
<p >The SERVER then waits for the client to reply. It then receives the reply in the string variable and displays it (recv())</p>
|
|
<p >The server and client can communicate till one of them exits the connection</p>
|
|
<p >Since the exchange of information between the server and client take place one at a time this represents HALF DUPLEX COMMUNICATION</p>
|
|
<p >Send message</p>
|
|
<p >Receive Message</p>
|
|
<p >Close socket</p>
|
|
<div class="fragment"><div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span>{<span class="comment"></span></div>
|
|
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span><span class="comment"> /** Variable Declarations */</span></div>
|
|
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> uint32_t sockfd,</div>
|
|
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> conn; <span class="comment">///< socket descriptors - Like file handles but for sockets</span></div>
|
|
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> <span class="keywordtype">char</span> server_msg[10000],</div>
|
|
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> client_msg[10000]; <span class="comment">///< character arrays to read and store string data</span><span class="comment"></span></div>
|
|
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span><span class="comment"> /// for communication</span></div>
|
|
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span><span class="comment"></span> <span class="keyword">struct </span>sockaddr_in server_addr,</div>
|
|
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> client_addr; <span class="comment">///< asic structures for all syscalls and functions that</span><span class="comment"></span></div>
|
|
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span><span class="comment"> /// deal with internet addresses. Structures for handling</span></div>
|
|
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span><span class="comment"> /// internet addresses</span></div>
|
|
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span><span class="comment"></span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span><span class="comment"> * The TCP socket is created using the socket function</span></div>
|
|
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span><span class="comment"> * AF_INET (Family) - it is an address family that is used to designate the</span></div>
|
|
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span><span class="comment"> * type of addresses that your socket can communicate with</span></div>
|
|
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span><span class="comment"> * SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides</span></div>
|
|
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span><span class="comment"> * for the bidirectional, reliable, sequenced, and unduplicated flow of data</span></div>
|
|
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span><span class="comment"> * without record boundaries. Aside from the bidirectionality of data flow,</span></div>
|
|
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span><span class="comment"> * a pair of connected stream sockets provides an interface nearly identical</span></div>
|
|
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span><span class="comment"> * to pipes</span></div>
|
|
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span><span class="comment"> * 0 (Protocol) - Specifies a particular protocol to be used with the</span></div>
|
|
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span><span class="comment"> * socket. Specifying a protocol of 0 causes socket() to use an unspecified</span></div>
|
|
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span><span class="comment"> * default protocol appropriate for the requested socket type</span></div>
|
|
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> <span class="keywordflow">if</span> ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)</div>
|
|
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> {</div>
|
|
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> <a class="code hl_function" href="../../d6/d2b/tcp__half__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"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span><span class="comment"> /// socket wasnt created</span></div>
|
|
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span><span class="comment"></span> }</div>
|
|
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span><span class="comment"> * Server Address Information</span></div>
|
|
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span><span class="comment"> * The bzero() function erases the data in the n bytes of the memory</span></div>
|
|
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span><span class="comment"> * starting at the location pointed to, by writing zeros (bytes</span></div>
|
|
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span><span class="comment"> * containing '\0') to that area</span></div>
|
|
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span><span class="comment"> * We bind the server_addr to the internet address and port number thus</span></div>
|
|
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span><span class="comment"> * giving our socket an identity with an address and port where it can</span></div>
|
|
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span><span class="comment"> * listen for connections</span></div>
|
|
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span><span class="comment"> * htons - The htons() function translates a short integer from host byte</span></div>
|
|
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span><span class="comment"> * order to network byte order</span></div>
|
|
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span><span class="comment"> * htonl - The htonl() function translates a long integer from host byte</span></div>
|
|
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span><span class="comment"> * order to network byte order</span></div>
|
|
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span><span class="comment"> * These functions are necessary so that the binding of address and port</span></div>
|
|
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span><span class="comment"> * takes place with data in the correct format</span></div>
|
|
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span> bzero(&server_addr, <span class="keyword">sizeof</span>(server_addr));</div>
|
|
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span> server_addr.sin_family = AF_INET; <span class="comment">/// Domain/Family to be used</span></div>
|
|
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span> server_addr.sin_port = htons(<a class="code hl_define" href="../../d6/d2b/tcp__half__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a>); <span class="comment">/// Port to be used</span></div>
|
|
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span> server_addr.sin_addr.s_addr = htonl(INADDR_ANY);</div>
|
|
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> </div>
|
|
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> printf(<span class="stringliteral">"Server is running...\n"</span>);</div>
|
|
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span><span class="comment"> * This binds the socket descriptor to the server thus enabling the server</span></div>
|
|
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span><span class="comment"> * to listen for connections and communicate with other clients</span></div>
|
|
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</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"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> {</div>
|
|
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> <a class="code hl_function" href="../../d6/d2b/tcp__half__duplex__server_8c.html#a7e15c8e2885871839fc2b820dfbdb4ce">error</a>(); <span class="comment">/// If binding is unsuccessful</span></div>
|
|
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> }</div>
|
|
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span><span class="comment"> * This is to listen for clients or connections made to the server</span></div>
|
|
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span><span class="comment"> * The limit is currently at 5 but can be increased to listen for</span></div>
|
|
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span><span class="comment"> * more connections</span></div>
|
|
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span><span class="comment"> * It listens to connections through the socket descriptor</span></div>
|
|
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> listen(sockfd, 5);</div>
|
|
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> </div>
|
|
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> printf(<span class="stringliteral">"Server is listening...\n"</span>);</div>
|
|
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span><span class="comment"> * When a connection is found, a socket is created and connection is</span></div>
|
|
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span><span class="comment"> * accepted and established through the socket descriptor</span></div>
|
|
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> conn = accept(sockfd, (<span class="keyword">struct</span> sockaddr *)NULL, NULL);</div>
|
|
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> </div>
|
|
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> printf(<span class="stringliteral">"Server is connected...\n"</span>);</div>
|
|
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span><span class="comment"> /**</span></div>
|
|
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span><span class="comment"> * Communication between client and server</span></div>
|
|
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span><span class="comment"> * The bzero() function erases the data in the n bytes of the memory</span></div>
|
|
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span><span class="comment"> * starting at the location pointed to, by writing zeros (bytes</span></div>
|
|
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span><span class="comment"> * containing '\0') to that area. The variables are emptied and then</span></div>
|
|
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno"> 143</span><span class="comment"> * ready for use</span></div>
|
|
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno"> 144</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno"> 145</span><span class="comment"> * First the SERVER is prompted to type a message which is read from</span></div>
|
|
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span><span class="comment"> * stdin and then sent over the connection that was established - the socket</span></div>
|
|
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span><span class="comment"> * - to be received by the client (send())</span></div>
|
|
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span><span class="comment"> * The SERVER then waits for the client to reply. It then receives the reply</span></div>
|
|
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span><span class="comment"> * in the string variable and displays it (recv())</span></div>
|
|
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span><span class="comment"> * The server and client can communicate till one of them exits the</span></div>
|
|
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span><span class="comment"> * connection</span></div>
|
|
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span><span class="comment"> *</span></div>
|
|
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span><span class="comment"> * Since the exchange of information between the server and client take</span></div>
|
|
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span><span class="comment"> * place one at a time this represents HALF DUPLEX COMMUNICATION</span></div>
|
|
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> <span class="keywordflow">while</span> (1)</div>
|
|
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> {</div>
|
|
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> bzero(&server_msg, <span class="keyword">sizeof</span>(server_msg));</div>
|
|
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> bzero(&client_msg, <span class="keyword">sizeof</span>(client_msg));</div>
|
|
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span><span class="comment"> /// Send message</span></div>
|
|
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span><span class="comment"></span> printf(<span class="stringliteral">"\nEnter message here: "</span>);</div>
|
|
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> fgets(server_msg, 10000, stdin);</div>
|
|
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> send(conn, server_msg, strlen(server_msg) + 1, 0);</div>
|
|
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span><span class="comment"> /// Receive Message</span></div>
|
|
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span><span class="comment"></span> recv(conn, client_msg, <span class="keyword">sizeof</span>(client_msg), 0);</div>
|
|
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span> printf(<span class="stringliteral">"\nClient Message: %s\n"</span>, client_msg);</div>
|
|
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> }</div>
|
|
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span><span class="comment"></span> </div>
|
|
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span><span class="comment"> /// Close socket</span></div>
|
|
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span><span class="comment"></span> close(sockfd);</div>
|
|
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> printf(<span class="stringliteral">"Server is offline...\n"</span>);</div>
|
|
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a id="l00177" name="l00177"></a><span class="lineno"> 177</span>}</div>
|
|
<div class="ttc" id="atcp__half__duplex__server_8c_html_a614217d263be1fb1a5f76e2ff7be19a2"><div class="ttname"><a href="../../d6/d2b/tcp__half__duplex__server_8c.html#a614217d263be1fb1a5f76e2ff7be19a2">PORT</a></div><div class="ttdeci">#define PORT</div><div class="ttdoc">For structures returned by the network database library - formatted internet addresses and port numbe...</div><div class="ttdef"><b>Definition:</b> tcp_half_duplex_server.c:28</div></div>
|
|
<div class="ttc" id="atcp__half__duplex__server_8c_html_a7e15c8e2885871839fc2b820dfbdb4ce"><div class="ttname"><a href="../../d6/d2b/tcp__half__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_half_duplex_server.c:36</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="../../d6/d2b/tcp__half__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="../../d6/d2b/tcp__half__duplex__server_8c.html">tcp_half_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.2 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|