delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/04/27/21:00:07

From: "Bart van den Burg" <news3 AT tvreclames DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: errors when linking winsock prog
Date: Mon, 28 Apr 2003 02:35:44 +0200
Organization: Planet Internet
Lines: 171
Message-ID: <b8htpk$70j$1@reader08.wxs.nl>
NNTP-Posting-Host: ip503cca9b.speed.planet.nl
X-Trace: reader08.wxs.nl 1051490932 7187 80.60.202.155 (28 Apr 2003 00:48:52 GMT)
X-Complaints-To: abuse AT planet DOT nl
NNTP-Posting-Date: 28 Apr 2003 00:48:52 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

hi

I'm writing myself a socket.cpp
source is below
it compiles fine, but when I try to link it to some other file, i get these
errors:

c:/djgpp/tmp/cckqc7rS.o(.text+0x174):socket.cpp: undefined reference to
`_send'
c:/djgpp/tmp/cckqc7rS.o(.text+0x338):socket.cpp: undefined reference to
`_recv'
c:/djgpp/tmp/cckqc7rS.o(.text+0x400):socket.cpp: undefined reference to
`_gethostbyname'
c:/djgpp/tmp/cckqc7rS.o(.text+0x4a6):socket.cpp: undefined reference to
`_connect'
c:/djgpp/tmp/cckqc7rS.o(.text+0x50e):socket.cpp: undefined reference to
`_socket'
c:/djgpp/tmp/cckqc7rS.o(.text+0x57b):socket.cpp: undefined reference to
`_getprotobyname'
collect2: ld returned 1 exit status

what am I missing here?

thanks
Bart

----------- socket.h -------------------------------------------------
#ifndef SOCKET_H
#define SOCKET_H

#ifdef __linux__
  #include <sys/socket.h>
  #include <sys/types.h>
#endif
#ifdef MSDOS
  #include <winsock.h>
  #include <netinet/in.h>
#endif

#include <netdb.h>
#include <fcntl.h>
#include <cerrno>
#include <iostream>
#include <cstring>
#include <string>

extern int errno;
using namespace std;

class Socket {
  private:
    int read();
    string buffer;
    int socket;
    int create();
    int connect();
    string line;
  public:
    int protocol, port;
    char *server;
    bool nonblock;
    int open(char *, int, char *);
    int readline(string &);
    int write(string);
};

#endif // SOCKET_H
----------------------------------------------------------------------

----------- socket.cpp -----------------------------------------------
#include "socket.h"

int Socket::readline(string &retline) {
  line = '\0';
  retline = '\0';
  if (read() == -1) {
    return -1;
  }
  if (line.length() > 1) {
    retline = line;
    return 1;
  }
}

int Socket::write(string data) {
  if (1) cout << "TX: " << data << endl;
  data = data + "\r\n";
  char *array = new char[data.length() + 1];
  strcpy(array, data.c_str());
  int length = send(socket, array, data.length(), 0);
  delete []array;
}

int Socket::read() {
  char temp[1024];
  errno = 0;
  int pos;
  if ((pos = buffer.find('\r')) > 0) {
    line = buffer.substr(0, pos);
    buffer = buffer.substr(pos + 2, buffer.length());
    if (1) cout << "RX: " << line << endl;
    return 1;
  }
  int length = recv(socket, temp, 1023, 0);
  if (length > 0) {
    if (!errno) {
      temp[length] = 0;
      buffer.append(temp);
      return 0;
    }
  }
  else if (length == 0 && errno != 11) {
    return -1;
  }
  else return 0;
}

int Socket::connect() {
  struct sockaddr_in sin;
  struct hostent *he;
  if (!(he = gethostbyname(server))) {
    cout << "Could not resolve hostname: " << server << endl;
    return 0;
  }
  sin.sin_family = AF_INET;
  memcpy(&sin.sin_addr, he->h_addr_list[0], he->h_length);
  sin.sin_port = htons(port);
  ::connect(socket, (struct sockaddr *)&sin, sizeof(sin));
  cout << "Connection returned: " << strerror(errno) << endl;
}

int Socket::create() {
  if ((socket = ::socket(PF_INET, SOCK_STREAM, protocol)) < 0) {
    return 0;
  }
  if (nonblock) {
    int flags = fcntl(socket, F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(socket, F_SETFL, flags);
  }
}

int Socket::open(char *server, int port, char *protocol) {
  struct protoent *proto = getprotobyname(protocol);
  Socket::protocol = proto->p_proto;
  Socket::port = port;
  Socket::server = server;

  create();
  connect();
}
----------------------------------------------------------------------

---------- test.cpp --------------------------------------------------
#include "socket.h"
#include <iostream>
#include <string>

using namespace std;

int main() {
  Socket socket;
  socket.open("localhost", 21, "tcp");
  while(1) {
    string read;
    socket.readline(read);
  }
}
----------------------------------------------------------------------


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019