<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[TCP Client&#x2F;Server]]></title><description><![CDATA[<p>Hallo erstmal,</p>
<p>bin neu hier und hoffe ihr könnt mir mit meine Problem weiterhelfen.</p>
<p>ich will eine ganz simple TCP Verbindung zwischen nem Server Programm und nem Client Programm herstellen, aber irgendwie will es nicht...</p>
<p>WSAStartup(),Socket(), bind() initialisieren sich erfolgreich auf der Client und Server-Seite</p>
<p>Probleme machen Connect, liefert immer -1 zurück und accept welches nach Starten das Programm zum hängen bringt. Bei letzterem bin ich mir nciht sicher ob es normal ist, von wegen Blocking-Mode und warten bis eine Verbindung acceptet werden kann.</p>
<p>Ich habe schon mehrere Quellcodes runtergeladen, aber ich bekomme es selber einfach nicht hin und finde den Fehler nicht. Ich nutze WinXP und den C Builder 6 von Borland</p>
<p>Hier mein Quellcode des Clients:</p>
<pre><code>//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &lt;errno.h&gt;
#include &lt;string&gt;
#define AF_INET 2 //Internet Protocol

 #include &lt;fcntl.h&gt;

#include &lt;winsock.h&gt;

#include &quot;Client.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int sockfd,servSock;
struct sockaddr_in con_addr, serv_addr;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{

con_addr.sin_family = AF_INET;
con_addr.sin_port = htons(3131);
con_addr.sin_addr.s_addr = INADDR_ANY;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(3131);
serv_addr.sin_addr.s_addr = INADDR_ANY;

WSADATA wsaData;
if (WSAStartup (MAKEWORD(1, 1), &amp;wsaData) != 0)
   {
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup(): Kann Winsock nicht initialisieren.&quot;);
               exit (EXIT_FAILURE);
   }
   else
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup():erfolgreich.&quot;);

sockfd = socket(AF_INET, SOCK_STREAM,0);
if (sockfd == -1)
   {
           perror (&quot;socket()&quot;);
           Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung fehlgeschlagen&quot;);
   }
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung erfolgreich&quot;);

if (bind(sockfd, (sockaddr *)&amp;con_addr, sizeof(sockaddr)) == -1)
{
       perror (&quot;bind()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung fehlgeschlagen&quot;);
}
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung erfolgreich&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitBtnClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ConBtnClick(TObject *Sender)
{
if(connect(sockfd, (sockaddr *)&amp;serv_addr, sizeof(sockaddr)) == -1)
{
    perror (&quot;connect()&quot;);
    Ausgabe-&gt;Lines-&gt;Add(&quot;Connect initialisierung nicht erfolgreich&quot;);
}
else
    Ausgabe-&gt;Lines-&gt;Add(&quot;Connect initialisierung erfolgreich&quot;);
}

//---------------------------------------------------------------------------
</code></pre>
<p>und hier der des Servers:</p>
<pre><code>//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Server.h&quot;
#include &lt;errno.h&gt;
#include &lt;string&gt;
#define AF_INET 2 //Internet Protocol

 #include &lt;fcntl.h&gt;

#include &lt;winsock.h&gt;
#include &lt;time.h&gt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int sockfd;
struct sockaddr_in con_addr, client_addr;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
con_addr.sin_family = AF_INET;
con_addr.sin_port = htons(3131);
con_addr.sin_addr.s_addr = INADDR_ANY;

WSADATA wsaData;
if (WSAStartup (MAKEWORD(1, 1), &amp;wsaData) != 0)
   {
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup(): Kann Winsock nicht initialisieren.&quot;);
               exit (EXIT_FAILURE);
   }
   else
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup():erfolgreich.&quot;);

sockfd = socket(AF_INET, SOCK_STREAM,0);
if (sockfd == -1)
   {
           perror (&quot;socket()&quot;);
           Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung fehlgeschlagen&quot;);
   }
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung erfolgreich&quot;);

if (bind(sockfd, (sockaddr *)&amp;con_addr, sizeof(sockaddr)) == -1)
{
       perror (&quot;bind()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung fehlgeschlagen&quot;);
}
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung erfolgreich&quot;);

if (listen (sockfd, 5) == -1)
{
       perror (&quot;listen()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Listen initialisierung fehlgeschlagen&quot;);
}
else
        Ausgabe-&gt;Lines-&gt;Add(&quot;Listen initialisierung erfolgreich&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitBtnClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AcceptBtnClick(TObject *Sender)
{
int rlen;
rlen=sizeof client_addr;
if ((accept(sockfd, (struct sockaddr*)&amp;client_addr, &amp;rlen)) &lt; 0)
{

   perror(&quot;accept error&quot;);
   Ausgabe-&gt;Lines-&gt;Add(&quot;Accept fehlgeschlagen&quot;);
}
else
    Ausgabe-&gt;Lines-&gt;Add(&quot;Accept erfolgreich&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/141462/tcp-client-server</link><generator>RSS for Node</generator><lastBuildDate>Mon, 03 Aug 2026 19:28:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/141462.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 22 Mar 2006 10:54:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Wed, 22 Mar 2006 10:54:54 GMT]]></title><description><![CDATA[<p>Hallo erstmal,</p>
<p>bin neu hier und hoffe ihr könnt mir mit meine Problem weiterhelfen.</p>
<p>ich will eine ganz simple TCP Verbindung zwischen nem Server Programm und nem Client Programm herstellen, aber irgendwie will es nicht...</p>
<p>WSAStartup(),Socket(), bind() initialisieren sich erfolgreich auf der Client und Server-Seite</p>
<p>Probleme machen Connect, liefert immer -1 zurück und accept welches nach Starten das Programm zum hängen bringt. Bei letzterem bin ich mir nciht sicher ob es normal ist, von wegen Blocking-Mode und warten bis eine Verbindung acceptet werden kann.</p>
<p>Ich habe schon mehrere Quellcodes runtergeladen, aber ich bekomme es selber einfach nicht hin und finde den Fehler nicht. Ich nutze WinXP und den C Builder 6 von Borland</p>
<p>Hier mein Quellcode des Clients:</p>
<pre><code>//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &lt;errno.h&gt;
#include &lt;string&gt;
#define AF_INET 2 //Internet Protocol

 #include &lt;fcntl.h&gt;

#include &lt;winsock.h&gt;

#include &quot;Client.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int sockfd,servSock;
struct sockaddr_in con_addr, serv_addr;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{

con_addr.sin_family = AF_INET;
con_addr.sin_port = htons(3131);
con_addr.sin_addr.s_addr = INADDR_ANY;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(3131);
serv_addr.sin_addr.s_addr = INADDR_ANY;

WSADATA wsaData;
if (WSAStartup (MAKEWORD(1, 1), &amp;wsaData) != 0)
   {
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup(): Kann Winsock nicht initialisieren.&quot;);
               exit (EXIT_FAILURE);
   }
   else
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup():erfolgreich.&quot;);

sockfd = socket(AF_INET, SOCK_STREAM,0);
if (sockfd == -1)
   {
           perror (&quot;socket()&quot;);
           Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung fehlgeschlagen&quot;);
   }
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung erfolgreich&quot;);

if (bind(sockfd, (sockaddr *)&amp;con_addr, sizeof(sockaddr)) == -1)
{
       perror (&quot;bind()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung fehlgeschlagen&quot;);
}
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung erfolgreich&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitBtnClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ConBtnClick(TObject *Sender)
{
if(connect(sockfd, (sockaddr *)&amp;serv_addr, sizeof(sockaddr)) == -1)
{
    perror (&quot;connect()&quot;);
    Ausgabe-&gt;Lines-&gt;Add(&quot;Connect initialisierung nicht erfolgreich&quot;);
}
else
    Ausgabe-&gt;Lines-&gt;Add(&quot;Connect initialisierung erfolgreich&quot;);
}

//---------------------------------------------------------------------------
</code></pre>
<p>und hier der des Servers:</p>
<pre><code>//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Server.h&quot;
#include &lt;errno.h&gt;
#include &lt;string&gt;
#define AF_INET 2 //Internet Protocol

 #include &lt;fcntl.h&gt;

#include &lt;winsock.h&gt;
#include &lt;time.h&gt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int sockfd;
struct sockaddr_in con_addr, client_addr;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
con_addr.sin_family = AF_INET;
con_addr.sin_port = htons(3131);
con_addr.sin_addr.s_addr = INADDR_ANY;

WSADATA wsaData;
if (WSAStartup (MAKEWORD(1, 1), &amp;wsaData) != 0)
   {
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup(): Kann Winsock nicht initialisieren.&quot;);
               exit (EXIT_FAILURE);
   }
   else
               Ausgabe-&gt;Lines-&gt;Add(&quot;WSAStartup():erfolgreich.&quot;);

sockfd = socket(AF_INET, SOCK_STREAM,0);
if (sockfd == -1)
   {
           perror (&quot;socket()&quot;);
           Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung fehlgeschlagen&quot;);
   }
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Socket initialisierung erfolgreich&quot;);

if (bind(sockfd, (sockaddr *)&amp;con_addr, sizeof(sockaddr)) == -1)
{
       perror (&quot;bind()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung fehlgeschlagen&quot;);
}
   else
       Ausgabe-&gt;Lines-&gt;Add(&quot;Bind initialisierung erfolgreich&quot;);

if (listen (sockfd, 5) == -1)
{
       perror (&quot;listen()&quot;);
       Ausgabe-&gt;Lines-&gt;Add(&quot;Listen initialisierung fehlgeschlagen&quot;);
}
else
        Ausgabe-&gt;Lines-&gt;Add(&quot;Listen initialisierung erfolgreich&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitBtnClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AcceptBtnClick(TObject *Sender)
{
int rlen;
rlen=sizeof client_addr;
if ((accept(sockfd, (struct sockaddr*)&amp;client_addr, &amp;rlen)) &lt; 0)
{

   perror(&quot;accept error&quot;);
   Ausgabe-&gt;Lines-&gt;Add(&quot;Accept fehlgeschlagen&quot;);
}
else
    Ausgabe-&gt;Lines-&gt;Add(&quot;Accept erfolgreich&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1021582</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1021582</guid><dc:creator><![CDATA[TheOrangeBishop]]></dc:creator><pubDate>Wed, 22 Mar 2006 10:54:54 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Wed, 22 Mar 2006 11:04:14 GMT]]></title><description><![CDATA[<p>Beim Connect ist serv_addr ja auch mit INADDR_ANY gefüllt. Soll er sich mit irgendeinem Rechner verbinden? Du musst ihm schon eine Adresse mitteilen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Was das Blockieren bei Accept angeht -&gt; ist normal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1021589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1021589</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 22 Mar 2006 11:04:14 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Wed, 22 Mar 2006 11:04:33 GMT]]></title><description><![CDATA[<p>Der C++ Builder 6 hat doch schon fertige Client / Server Module !?<br />
Warum das Rad neu erfinden ???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1021591</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1021591</guid><dc:creator><![CDATA[DRIVER26]]></dc:creator><pubDate>Wed, 22 Mar 2006 11:04:33 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Wed, 22 Mar 2006 11:22:25 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile.php?mode=viewprofile&amp;u=10455" rel="nofollow">evilissimo</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=15" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=2" rel="nofollow">VCL/CLX (Borland C++ Builder)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1021602</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1021602</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Wed, 22 Mar 2006 11:22:25 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Wed, 22 Mar 2006 12:19:31 GMT]]></title><description><![CDATA[<p>Was INADDR_ANY angeht so will ich testhalber client und Server erstmal auf einem Rechner laufen lassen und dann ist es ja beim Client und auch beim Server die Selbe, oder? Habe es auch schon mit my_addr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;); probiert.... aber da wollte Connect auch nicht</p>
<p>und was das Rad neu Erfinden angeht:<br />
Es geht mehr um einfach mal etwas Erfahrung im Bereich TCP Programmierung (also Basics) zu bekommen als ein produktives Programm zu schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1021633</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1021633</guid><dc:creator><![CDATA[TheOrangeBishop]]></dc:creator><pubDate>Wed, 22 Mar 2006 12:19:31 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Fri, 24 Mar 2006 08:19:36 GMT]]></title><description><![CDATA[<p>Hat denn niemand eine Idee?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1022805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1022805</guid><dc:creator><![CDATA[TheOrangeBishop]]></dc:creator><pubDate>Fri, 24 Mar 2006 08:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Fri, 24 Mar 2006 08:29:58 GMT]]></title><description><![CDATA[<p>kenne mich da leider nicht so aus...<br />
aber wenn ich das richtig verstanden habe, nutzt du ja die winsockets und greifst nicht auf die VCL zurück -&gt; kein BCB/VCL Problem.</p>
<p>ich würde mal ein Moderator lieb fragen ob er dich in das entsprechende Forum schiebt. dort hast du evtl mehr erfolg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1022816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1022816</guid><dc:creator><![CDATA[BigNeal]]></dc:creator><pubDate>Fri, 24 Mar 2006 08:29:58 GMT</pubDate></item><item><title><![CDATA[Reply to TCP Client&#x2F;Server on Fri, 24 Mar 2006 11:21:02 GMT]]></title><description><![CDATA[<p>Vielleicht hilft dir das weiter:</p>
<pre><code class="language-cpp">/*****************************************************************************
*
*	open a tcp connection
*
*	DestIPStr: destionation IP address format xxx.xxx.xxx.xxx
*	ClientPort: local port (0=random port)
*	HostPort: remote port
*	error: TCP/IP error
*
*
*	return: socket descriptor (-1 = error)
*/

int tcp_connect(char *DestIPStr, unsigned int ClientPort, unsigned int HostPort, int *error)
{
	struct sockaddr_in addr;
	unsigned long DestIPAddr;
	int sd; // socket descriptor
        int dummyerror;

	// open socket
	sd = opensocket(SOCK_STREAM,error);
	if (sd==-1 || *error!=0)
	{
		return -1;
	}

	addr.sin_family = PF_INET;
	addr.sin_port = htons(ClientPort);	// convert byte order
	addr.sin_addr.s_addr =  0;
	if (ClientPort!=0)
	{	// force client port
		bind(sd, (struct sockaddr *)&amp;addr, error);
		if (*error!=0)
		{
                   closesocket(sd, &amp;dummyerror);
			return -1;
		}
	}
	addr.sin_port = htons(HostPort);	// convert byte order

	// convert server IP address string to binary
	*error = inet_addr(DestIPStr,&amp;DestIPAddr);
	if (*error!=0)
	{
             closesocket(sd, &amp;dummyerror);
		return -1;
	}
	addr.sin_addr.s_addr = DestIPAddr;

	// establish a connection to the FTP server
	connect(sd, (struct sockaddr *) &amp;addr, error);
	if (*error!=0)
	{
            closesocket(sd, &amp;dummyerror);
		return -1;
	};

	*error = 0;
	return sd;

}	// tcp_connect()
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1022972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1022972</guid><dc:creator><![CDATA[DRIVER26]]></dc:creator><pubDate>Fri, 24 Mar 2006 11:21:02 GMT</pubDate></item></channel></rss>