<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Basic_TCP_Server</id>
		<title>Basic TCP Server - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Basic_TCP_Server"/>
		<link rel="alternate" type="text/html" href="http://www.ethernut.de/nutwiki/index.php?title=Basic_TCP_Server&amp;action=history"/>
		<updated>2026-04-25T11:58:06Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://www.ethernut.de/nutwiki/index.php?title=Basic_TCP_Server&amp;diff=24&amp;oldid=prev</id>
		<title>Harald: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="http://www.ethernut.de/nutwiki/index.php?title=Basic_TCP_Server&amp;diff=24&amp;oldid=prev"/>
				<updated>2016-10-27T16:02:52Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;tr style='vertical-align: top;' lang='en'&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='1' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 16:02, 27 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan='2' style='text-align: center;' lang='en'&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Harald</name></author>	</entry>

	<entry>
		<id>http://www.ethernut.de/nutwiki/index.php?title=Basic_TCP_Server&amp;diff=23&amp;oldid=prev</id>
		<title>Niziak at 11:58, 12 April 2011</title>
		<link rel="alternate" type="text/html" href="http://www.ethernut.de/nutwiki/index.php?title=Basic_TCP_Server&amp;diff=23&amp;oldid=prev"/>
				<updated>2011-04-12T11:58:25Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;First register network driver and configure interface by using DHCP.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
NutRegisterDevice(&amp;amp;DEV_ETHER, 0, 0);&lt;br /&gt;
if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {&lt;br /&gt;
    /* Error: Cannot configure interface. */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are several alternatives:&lt;br /&gt;
* [[Network Configuration Using Hard Coded Configuration]]&lt;br /&gt;
* [[Network Configuration Using Stored Configuration]]&lt;br /&gt;
* [[Network Configuration Using a Configuration Editor]]&lt;br /&gt;
&lt;br /&gt;
Next, create a socket and wait for client connections at a specified port (8000).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
sock = NutTcpCreateSocket();&lt;br /&gt;
if (NutTcpAccept(sock, 8000)) {&lt;br /&gt;
    /* Error: Cannot connect server. */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To prevent DOS attack, set timeout value for established TCP connection:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint32_t to = 10000;&lt;br /&gt;
NutTcpSetSockOpt(sock, SO_RCVTIMEO, &amp;amp;to, sizeof(to));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You may then receive data from the client...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
got = NutTcpRecv(sock, buffer, len);&lt;br /&gt;
if (got &amp;lt;= 0) {&lt;br /&gt;
    /* Error or connection closed by the client. */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
... or send data to the client.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
if (NutTcpSend(sock, buffer, len) != len) {&lt;br /&gt;
    /* Error or connection closed by the client. */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When done, close the socket. This will also close the connection if it is still established.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
NutTcpCloseSocket(sock);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more advanced applications you can redirect the connected socket to a file stream and use standard I/O functions like fprintf() or fscanf() for sending and receiving data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
FILE *stream;&lt;br /&gt;
/* ... more code here ... */&lt;br /&gt;
&lt;br /&gt;
stream = _fdopen((int) sock, &amp;quot;r+b&amp;quot;);&lt;br /&gt;
/* ... more code here ... */&lt;br /&gt;
&lt;br /&gt;
/* Sending data. */&lt;br /&gt;
fprintf(stream, &amp;quot;Welcome!\r\n&amp;quot;);&lt;br /&gt;
/* ... more code here ... */&lt;br /&gt;
&lt;br /&gt;
/* Receiving data. */&lt;br /&gt;
got = fread(buffer, 1, len, stream);&lt;br /&gt;
/* ... more code here ... */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do not forget to close the stream with fclose() and socket with NutTcpCloseSocket().&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Network Configuration Using Stored Configuration]]&lt;br /&gt;
* [[Network Configuration Using Hard Coded Configuration]]&lt;br /&gt;
* [[Network Configuration Using a Configuration Editor]]&lt;br /&gt;
* [[Basic TCP Client]]&lt;br /&gt;
* [[Socket Timeouts]]&lt;br /&gt;
* [[Discovery Service]]&lt;br /&gt;
* More [[Nut/OS Examples]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Code Snippets]]&lt;br /&gt;
&lt;br /&gt;
{{Languages}}&lt;/div&gt;</summary>
		<author><name>Niziak</name></author>	</entry>

	</feed>