Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: wille
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?
ThanksLive the life as you know it
/ Wille

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.

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.

![]() |
![]() |
![]() |

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