Brčić i Kmetić su napisali odličan tutorijal - seminarski rad o
programiranju TCP/IP Unix API-jima. U dodatku postoji i primer klijenta i
servera za Windows. Međutim, ni jedan ni drugi program ne mogu uspešno da
se linkuju u Visual C++ 6. Pošto pretpostavljam da je reč o istom tipu
problema (javlja se ista greška LNK2001 prilikom linkovanja), evo ispod
samo programa za klijent (nešto je kraći), kao i outputa kompajlera, pa ako
neko zna rešenje problema, ne bilo mu teško da javi.
Code:
/* client.c -- a stream socket client demo */
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 3490 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector&quot;s address information
WSADATA wsaData;
if (argc != 2) {
fprintf(stderr,"usage: client hostnamen");
exit(1);
}
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
fprintf(stderr,"WSAStartup failed with error %dn",WSAGetLastError());
WSACleanup();
exit(1);
}
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
fprintf(stderr,"gethostbyname");
WSACleanup();
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"socket");
WSACleanup();
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), &quot; &quot;, 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr)) == -1) {
fprintf(stderr,"connect");
WSACleanup();
exit(1);
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
fprintf(stderr,"recv");
WSACleanup();
exit(1);
}
buf[numbytes] = &quot; &quot;;
printf("Received: %s",buf);
closesocket(sockfd);
WSACleanup();
return 0;
}
Greške koje linker prijavljuje su:
[code]
client.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
client.obj : error LNK2001: unresolved external symbol __imp__recv@16
client.obj : error LNK2001: unresolved external symbol __imp__connect@12
client.obj : error LNK2001: unresolved external symbol __imp__htons@4
client.obj : error LNK2001: unresolved external symbol __imp__socket@12
client.obj : error LNK2001: unresolved external symbol
__imp__gethostbyname@4
client.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0
client.obj : error LNK2001: unresolved external symbol
__imp__WSAGetLastError@0
client.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/abcd.exe : fatal error LNK1120: 9 unresolved externals
Error executing link.exe.
[code]