Computing.Net > Forums > Programming > C++ Tcp/IP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Tcp/IP

Reply to Message Icon

Name: wille
Date: December 7, 2008 at 10:40:49 Pacific
OS: WINDOWS XP Home edition
CPU/Ram: 3.2GHz/1024Mb
Product: Unknown / UNKNOWN
Comment:

Hello, I have a problem with a tcp program in c++, using bloodshed Dev c++ Compiler. I've been programming using .NET alot before but given up that because of the need of .NET Famework.
This here i my code but I get a lot of Linker Errors.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>

void TCPecho(const char *, const char *);
void errexit(const char *, ...);
SOCKET connectTCP(const char *, const char *);

#define LINELEN 128
#define WSVERS MAKEWORD(2, 0)

----------------
* main - TCP client for ECHO service
----------------
*/

int main(int argc, char *argv[])
{
char *host = "localhost"; /* host to use if none supplied */
char *service = "echo"; /* default service name */
WSADATA wsadata;

switch (argc) {
case 1:
host = "localhost";
break;
case 3:
service = argv[2];
/* FALL THROUGH */
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "usage: TCPecho [host [port]]\n");
exit(1);
}
if (WSAStartup(WSVERS, &wsadata) != 0)
errexit("WSAStartup failed\n");
TCPecho(host, service);
WSACleanup();
exit(0);
}

----------------
* TCPecho - send input to ECHO service on specified host and print reply
----------------
*/
void
TCPecho(const char *host, const char *service)
{
char buf[LINELEN+1]; /* buffer for one line of text */
SOCKET s; /* socket descriptor */
int cc, outchars, inchars; /* characters counts */

s = connectTCP(host, service);

while (fgets(buf, sizeof(buf), stdin)) {
buf[LINELEN] = '\0'; /* ensure line null-termination */
outchars = strlen(buf);
(void) send(s, buf, outchars, 0);

/* read it back */
for (inchars = 0; inchars < outchars; inchars += cc) {
cc = recv(s, &buf[inchars], outchars-inchars, 0);
if (cc == SOCKET_ERROR)
errexit("socket recv failed: %d\n",
GetLastError());
}
fputs(buf, stdout);
}
closesocket(s);
}

Any Idea why? Or could anyone give me a link to a tutorial in c++ TCP/IP?
Thanks

Live the life as you know it
/ Wille



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: December 8, 2008 at 15:58:18 Pacific
Reply:

For a good, low-level but thorough tutorial using C, see Beej's Guide to Network Programming. In particular, section 1.5 tells you what DLLs you need to link.

For an example of a well-designed C++ networking library, see the Boost Sockets Library.


0

Response Number 2
Name: Razor2.3
Date: December 8, 2008 at 18:05:40 Pacific
Reply:

You need to add the libws2_32.a library. That'll take care of everything except errexit() and connectTCP(); both of which you declare, but never define.

Edit: Fixed the library name. That's what I get for going from memory, I suppose.


0

Response Number 3
Name: klint
Date: December 9, 2008 at 02:51:41 Pacific
Reply:

Razor, isn't libw2_32.a a Unix library, rather than Windows?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Tcp/IP

TCP/IP Input Reflection Problem www.computing.net/answers/programming/tcpip-input-reflection-problem/15393.html

Remotely access DOS pc using tcp/ip www.computing.net/answers/programming/remotely-access-dos-pc-using-tcpip/14107.html

How do i program a TCP/IP? www.computing.net/answers/programming/how-do-i-program-a-tcpip/10142.html