ZealOS/docs/Home/Net/Programs/TCPChatClient.CC.html
2021-08-17 02:47:37 -04:00

163 lines
13 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.00">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF9>CTask</span><span class=cF0> *chat_display_task = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l2"></a></span><span class=cF9>CTask</span><span class=cF0> *chat_message_task = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l3"></a>CTCPSocket *tcp = TCPSocket(AF_INET);
<a name="l4"></a>
<a name="l5"></a></span><span class=cF1>U0</span><span class=cF0> ChatDisplayTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l6"></a>{ </span><span class=cF2>// display received messages.</span><span class=cF0>
<a name="l7"></a> </span><span class=cF5>DocTermNew</span><span class=cF0>;
<a name="l8"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l9"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l10"></a>
<a name="l11"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>TaskValidate</span><span class=cF7>(</span><span class=cF0>chat_message_task</span><span class=cF7>)</span><span class=cF0>)
<a name="l12"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l13"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l14"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l15"></a>}
<a name="l16"></a>
<a name="l17"></a></span><span class=cF1>U0</span><span class=cF0> ChatMessageTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l18"></a>{ </span><span class=cF2>// take in text.</span><span class=cF0>
<a name="l19"></a> </span><span class=cF1>U8</span><span class=cF0> *message;
<a name="l20"></a>
<a name="l21"></a> </span><span class=cF5>DocTermNew</span><span class=cF0>;
<a name="l22"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l23"></a>
<a name="l24"></a> </span><span class=cF1>while</span><span class=cF0> (message = </span><span class=cF5>StrGet</span><span class=cF7>(</span><span class=cF6>&quot;&gt; &quot;</span><span class=cF0>,, </span><span class=cF3>SGF_SHIFT_ESC_EXIT</span><span class=cF7>)</span><span class=cF0>)
<a name="l25"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l26"></a> </span><span class=cF5>DocBottom</span><span class=cF0>(chat_display_task-&gt;put_doc);
<a name="l27"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(chat_display_task-&gt;put_doc,
<a name="l28"></a> </span><span class=cF6>&quot;$BG,BLUE$$BLACK$&lt;local&gt;$FG$$BG$ %s\n&quot;</span><span class=cF0>, message);
<a name="l29"></a>
<a name="l30"></a> TCPSocketSendString(tcp, message);
<a name="l31"></a>
<a name="l32"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l33"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(, </span><span class=cF6>&quot;$WW,1$&quot;</span><span class=cF0>);
<a name="l34"></a>
<a name="l35"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l36"></a>}
<a name="l37"></a>
<a name="l38"></a></span><span class=cF1>U0</span><span class=cF0> ChatInit()
<a name="l39"></a>{
<a name="l40"></a> chat_message_task = </span><span class=cF5>Spawn</span><span class=cF0>(&amp;ChatMessageTask, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF6>&quot;TCP Chat Message&quot;</span><span class=cF0>);
<a name="l41"></a> chat_display_task = </span><span class=cF5>Spawn</span><span class=cF0>(&amp;ChatDisplayTask, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF6>&quot;TCP Chat&quot;</span><span class=cF0>);
<a name="l42"></a> chat_message_task-&gt;win_inhibit = </span><span class=cF3>WIG_USER_TASK_DEFAULT</span><span class=cF0>;
<a name="l43"></a> chat_display_task-&gt;win_inhibit = </span><span class=cF3>WIG_USER_TASK_DEFAULT</span><span class=cF0>;
<a name="l44"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;chat_message_task-&gt;display_flags, </span><span class=cF3>DISPLAYf_SHOW</span><span class=cF0>);
<a name="l45"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;chat_display_task-&gt;display_flags, </span><span class=cF3>DISPLAYf_SHOW</span><span class=cF0>);
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF5>WinFocus</span><span class=cF0>(chat_display_task);
<a name="l48"></a> </span><span class=cF5>WinFocus</span><span class=cF0>(chat_message_task);
<a name="l49"></a>
<a name="l50"></a> chat_display_task-&gt;win_top = </span><span class=cFE>2</span><span class=cF0>;
<a name="l51"></a> chat_display_task-&gt;win_bottom = </span><span class=cF3>TEXT_ROWS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0>;
<a name="l52"></a> chat_display_task-&gt;win_left = </span><span class=cF3>TEXT_COLS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0>;
<a name="l53"></a> chat_display_task-&gt;win_right = </span><span class=cF3>TEXT_COLS</span><span class=cF0> / </span><span class=cFE>3</span><span class=cF0> * </span><span class=cFE>2</span><span class=cF0>;
<a name="l54"></a>
<a name="l55"></a> chat_message_task-&gt;win_top = chat_display_task-&gt;win_bottom + </span><span class=cFE>2</span><span class=cF0>;
<a name="l56"></a> chat_message_task-&gt;win_bottom = chat_message_task-&gt;win_top + </span><span class=cFE>3</span><span class=cF0>;
<a name="l57"></a> chat_message_task-&gt;win_left = chat_display_task-&gt;win_left;
<a name="l58"></a> chat_message_task-&gt;win_right = chat_display_task-&gt;win_right;
<a name="l59"></a>
<a name="l60"></a>}
<a name="l61"></a>
<a name="l62"></a></span><span class=cF1>U0</span><span class=cF0> Chat()
<a name="l63"></a>{
<a name="l64"></a> CSocketAddressIPV4 socket_addr;
<a name="l65"></a> CIPV4Address ip_addr;
<a name="l66"></a> </span><span class=cF1>U8</span><span class=cF0> *ip_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Server IPV4: &quot;</span><span class=cF0>);
<a name="l67"></a> </span><span class=cF1>U8</span><span class=cF0> *port_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Server Port: &quot;</span><span class=cF0>);
<a name="l68"></a> </span><span class=cF9>I64</span><span class=cF0> port = </span><span class=cF5>Str2I64</span><span class=cF0>(port_string);
<a name="l69"></a> </span><span class=cF1>U8</span><span class=cF0> *buffer[ETHERNET_FRAME_SIZE];
<a name="l70"></a> </span><span class=cF9>I64</span><span class=cF0> receive_val;
<a name="l71"></a>
<a name="l72"></a> </span><span class=cF1>while</span><span class=cF0> (PresentationToNetwork</span><span class=cF7>(</span><span class=cF0>AF_INET, ip_string, &amp;ip_addr</span><span class=cF7>)</span><span class=cF0> == -</span><span class=cFE>1</span><span class=cF0>)
<a name="l73"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l74"></a> </span><span class=cF6>&quot;\nERROR: Bad IP entered. Retry.\n&quot;</span><span class=cF0>;
<a name="l75"></a> ip_string = </span><span class=cF5>StrGet</span><span class=cF0>(</span><span class=cF6>&quot;Server IPV4: &quot;</span><span class=cF0>);
<a name="l76"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l77"></a>
<a name="l78"></a>
<a name="l79"></a> </span><span class=cF5>Free</span><span class=cF0>(ip_string);
<a name="l80"></a> </span><span class=cF5>Free</span><span class=cF0>(port_string);
<a name="l81"></a>
<a name="l82"></a> socket_addr.port = </span><span class=cF5>EndianU16</span><span class=cF0>(port);
<a name="l83"></a> socket_addr.family = AF_INET;
<a name="l84"></a> socket_addr.address.address = ip_addr.address;
<a name="l85"></a>
<a name="l86"></a> tcp-&gt;timeout = TCP_TIMEOUT; </span><span class=cF2>// use normal timeout for connect()</span><span class=cF0>
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF6>&quot;\nConnecting to server...\n&quot;</span><span class=cF0>;
<a name="l89"></a> </span><span class=cF1>if</span><span class=cF0> (TCPSocketConnect</span><span class=cF7>(</span><span class=cF0>tcp, &amp;socket_addr</span><span class=cF7>)</span><span class=cF0> != </span><span class=cFE>0</span><span class=cF0>)
<a name="l90"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l91"></a> </span><span class=cF6>&quot;\nFailed to connect to server.\n&quot;</span><span class=cF0>;
<a name="l92"></a> TCPSocketClose(tcp);
<a name="l93"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l94"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l95"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l96"></a> </span><span class=cF6>&quot;\nSuccessfully connected.\n&quot;</span><span class=cF0>;
<a name="l97"></a>
<a name="l98"></a> ChatInit;
<a name="l99"></a>
<a name="l100"></a> tcp-&gt;timeout = </span><span class=cFE>0</span><span class=cF0>;
<a name="l101"></a>
<a name="l102"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>TaskValidate</span><span class=cF7>(</span><span class=cF0>chat_message_task</span><span class=cF7>)</span><span class=cF0>)
<a name="l103"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l104"></a>
<a name="l105"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>receive_val = TCPSocketReceive(tcp, buffer, ETHERNET_FRAME_SIZE)</span><span class=cF7>)</span><span class=cF0> != -</span><span class=cFE>1</span><span class=cF0>)
<a name="l106"></a> {
<a name="l107"></a> </span><span class=cF1>if</span><span class=cF0> (receive_val == </span><span class=cFE>0</span><span class=cF0>)
<a name="l108"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l109"></a> </span><span class=cF6>&quot;\nServer closed the connection.\n&quot;</span><span class=cF0>;
<a name="l110"></a> </span><span class=cF5>Kill</span><span class=cF0>(chat_message_task);
<a name="l111"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l112"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l113"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (receive_val &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l114"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l115"></a> </span><span class=cF5>DocBottom</span><span class=cF0>(chat_display_task-&gt;put_doc);
<a name="l116"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(chat_display_task-&gt;put_doc,
<a name="l117"></a> </span><span class=cF6>&quot;%s\n&quot;</span><span class=cF0>, buffer);
<a name="l118"></a> </span><span class=cF5>MemSet</span><span class=cF0>(buffer, </span><span class=cFE>0</span><span class=cF0>, ETHERNET_FRAME_SIZE);
<a name="l119"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l120"></a> }
<a name="l121"></a>
<a name="l122"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l123"></a>
<a name="l124"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l125"></a>
<a name="l126"></a> tcp-&gt;timeout = TCP_TIMEOUT;
<a name="l127"></a>
<a name="l128"></a> </span><span class=cF6>&quot;\nClosing connected socket...\n&quot;</span><span class=cF0>;
<a name="l129"></a> </span><span class=cF1>if</span><span class=cF0> (TCPSocketClose</span><span class=cF7>(</span><span class=cF0>tcp</span><span class=cF7>)</span><span class=cF0> == </span><span class=cFE>0</span><span class=cF0>)
<a name="l130"></a> </span><span class=cF6>&quot;\nSocket closed.\n&quot;</span><span class=cF0>;
<a name="l131"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l132"></a> </span><span class=cF6>&quot;\nSocket failed to close.\n&quot;</span><span class=cF0>;
<a name="l133"></a>}
<a name="l134"></a>
<a name="l135"></a>Chat;</span></pre></body>
</html>