Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket

Status
Not open for further replies.

cbarrett30

IS-IT--Management
Jul 19, 2001
1
CA
I try to compile a program in Visual C++ with winsock. When i compile it no error appear, but when i link it the compilator return this :

--------------------Configuration: premer pas - Win32 Debug--------------------
Linking...
premer pas.obj : error LNK2001: unresolved external symbol _WSACancelAsyncRequest@4
premer pas.obj : error LNK2001: unresolved external symbol _send@16
premer pas.obj : error LNK2001: unresolved external symbol _recv@16
premer pas.obj : error LNK2001: unresolved external symbol _accept@12
premer pas.obj : error LNK2001: unresolved external symbol _closesocket@4
premer pas.obj : error LNK2001: unresolved external symbol _listen@8
premer pas.obj : error LNK2001: unresolved external symbol _bind@12
premer pas.obj : error LNK2001: unresolved external symbol _htons@4
premer pas.obj : error LNK2001: unresolved external symbol _WSAAsyncSelect@16
premer pas.obj : error LNK2001: unresolved external symbol _socket@12
premer pas.obj : error LNK2001: unresolved external symbol _WSAAsyncGetHostByName@20
Debug/premer pas.exe : fatal error LNK1120: 11 unresolved externals
Error executing link.exe.

premer pas.exe - 12 error(s), 0 warning(s)

------------------------------------------------------------
The program
------------------------------------------------------------

// premer pas.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <winsock.h>
#include &quot;resource.h&quot; // constates dialog zones #include <winsock.h>
#include <stdio.h>

#define MAX_LOADSTRING 100

#define MAX_CONNECTIONS 20
#define SERVER_PORT 5001


SOCKET gsListen; // Socket pour faire l'écoute des clients
SOCKET gsAccepted[MAX_CONNECTIONS];

#define WM_LISTEN (WM_USER +1)
#define WM_CONNECTION (WM_USER +2)
#define WM_ASYNCGETHOSTBYNAME (WM_USER +1023)

char hostentBuffer[MAXGETHOSTSTRUCT];
HANDLE hAsync = NULL;

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Quit(HWND, UINT, WPARAM, LPARAM);
void ShowStatus(HWND hDlg,LPSTR lpMsg);



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PREMERPAS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_PREMERPAS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:


//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{

case WM_ASYNCGETHOSTBYNAME:
{
hAsync = NULL;
if(!WSAGETASYNCERROR(lParam))
{
if (WSAGETASYNCBUFLEN(lParam) >= sizeof(HOSTENT))
{
HOSTENT *he = (HOSTENT*) hostentBuffer;
DWORD IPAdress = *(DWORD*)(he->h_addr);
}
}
}
break;


case WM_INITDIALOG:
{
int i;
SOCKADDR_IN sin;
hAsync = WSAAsyncGetHostByName(hWnd,
WM_ASYNCGETHOSTBYNAME,
&quot;netscape.com&quot;,
hostentBuffer,
sizeof(hostentBuffer));

if (!hAsync)
{
// error
}

// initialise le tableau global des Sockets

for (i=0; i < MAX_CONNECTIONS; i++)
gsAccepted = INVALID_SOCKET;

ShowStatus(hWnd, IDSTR_SOCKET);

gsListen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if (gsListen != INVALID_SOCKET)
{
ShowStatus(hWnd, &quot;WSAAsyncSelect()&quot;);

if (WSAAsyncSelect(gsListen, hWnd, WM_LISTEN, FD_ACCEPT) != SOCKET_ERROR)
{
// préparation d'une structure d'adresse. toutes les demandes de connexions
// seront prises en compte.
memset(&sin, 0, sizeof(sin));

sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);
sin.sin_addr.s_addr = INADDR_ANY;

// Associe une adresse à la Socket

ShowStatus(hWnd,&quot;Bind()&quot;);

if(bind(gsListen,(SOCKADDR *)&sin,sizeof(sin)) != SOCKET_ERROR)
{
// Association réussie, on se met à l'écoute du port

ShowStatus(hWnd, &quot;Listen()&quot;);

if(listen(gsListen, SOMAXCONN) != SOCKET_ERROR)
{
ShowStatus(hWnd, &quot;Wait...&quot;);
return TRUE;
}
else
ShowStatus(hWnd, &quot;Erreur à l'appel de listen()&quot;);
}
else
ShowStatus(hWnd, &quot;Erreur à l'appel de bind()&quot;);
}
else
ShowStatus(hWnd, &quot;Erreur à l'appel de WSAAsyncSelect()&quot;);
}
else
ShowStatus(hWnd, &quot;Erreur à l'appel de Socket&quot;);

// Erreur de création de socket

if(gsListen != INVALID_SOCKET)
{
ShowStatus(hWnd,&quot;Fermeture du scoket&quot;);
closesocket(gsListen);
}

ShowStatus(hWnd, &quot;Connexion impossible&quot;);

ShowStatus(hWnd, &quot;Vérifier votre configuration&quot;);

ShowStatus(hWnd, &quot;et relancer le serveur&quot;);

MessageBeep(0);

return TRUE;

}
break;


case WM_LISTEN:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
{
SOCKET s;
SOCKADDR_IN addr_accept;

int iLen = sizeof(addr_accept);

ShowStatus(hWnd, &quot;FD_ACCEPT&quot;);

ShowStatus(hWnd, &quot;Accept()&quot;);

s = accept(gsListen, (SOCKADDR *) &addr_accept, &iLen);

if(s == INVALID_SOCKET)
ShowStatus(hWnd, &quot;Erreur à l'appel de la commande accept()&quot;);
else
{
int i;

// Cherche un socket libre

for (i=0; i< MAX_CONNECTIONS; i++)
{
if(gsAccepted == INVALID_SOCKET)
{
char Buffer[80];

gsAccepted = s;

// wsprintf(Buffer, &quot;(%d) connecté à 0x081x, port no : %5d&quot;, i,ntohl(addr_accept.sin_addr.S_un),ntohs(addr_accept.sin_port));

ShowStatus(hWnd,Buffer);

WSAAsyncSelect(s,hWnd,WM_CONNECTION, FD_READ | FD_WRITE);

return 0;

}
}

ShowStatus(hWnd,&quot;Il y n'a pas de socket disponible!&quot;);

closesocket(s);

}
}

break;

}
}

break;

case WM_CONNECTION:
{
char Buffer[80];

int iAccept;
SOCKET actSocket;
int i;

actSocket = (SOCKET)wParam;

iAccept = -1;

for(i=1;i<MAX_CONNECTIONS; i++)
if(gsAccepted == actSocket)
{
iAccept = i;
break;
}

// lecture des messages reçus des clients

switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
char readbytes[255];
char sendbytes[512];

int iReadLen;
int i,j;

wsprintf(Buffer,&quot;FD_READ(%d)&quot;,iAccept);
ShowStatus(hWnd,Buffer);
// Lecteur des données provenant du client

iReadLen = recv(actSocket, readbytes, sizeof(readbytes),0);
if(iReadLen > 0)
{
wsprintf(Buffer,&quot;echo-send(%d)&quot;,iAccept);
ShowStatus(hWnd, Buffer);
// Convertit les chaînes recus en majuscules

j = 0;
for(i=0;i<iReadLen;i++)
{
sendbytes[j++] = toupper(readbytes);
}

if(send(actSocket, sendbytes, j, 0) == SOCKET_ERROR)
{
ShowStatus(hWnd,&quot;Erreur dans re-send()&quot;);
}
}
else
ShowStatus(hWnd, &quot;Erreur dans receive()&quot;);


wsprintf(Buffer, &quot;closeSocket(%d)&quot;,iAccept);

ShowStatus(hWnd, Buffer);

closesocket(actSocket);

// Neutralise le socket

gsAccepted[iAccept] = INVALID_SOCKET;
}
break;

case FD_CLOSE:
{
wsprintf(Buffer, &quot;FD_CLOSE(%d)&quot;,iAccept);

ShowStatus(hWnd, Buffer);
wsprintf(Buffer, &quot;closeSocket(%d)&quot;,iAccept);

ShowStatus(hWnd, Buffer);
closesocket(gsAccepted[iAccept]);

// Neutralise le socket
gsAccepted[iAccept] = INVALID_SOCKET;
}
break;

}
}
break;

case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hWnd, (DLGPROC)Quit);
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
{
int i;

// en cas de fin prématurée de l'application, il faut
// interrompre les opérations asymchrones qui pourraient
// être en cours.

if(hAsync)
WSACancelAsyncRequest(hAsync);

// Fermer les sockets qui sont ouverts, Déclache le message FD_CLOSE
// chez le client

if(gsListen != INVALID_SOCKET)
closesocket(gsListen);

for (i=0; i < MAX_CONNECTIONS; i++)
if(gsAccepted == INVALID_SOCKET)
closesocket(gsAccepted);

PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

LRESULT CALLBACK Quit(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}

if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return FALSE;
}


break;
}
return FALSE;
}

void ShowStatus(HWND hDlg,LPSTR lpMsg)
{
int iEntries;
SendMessage(GetDlgItem(hDlg, IDC_STATUS), LB_ADDSTRING, (WPARAM)-1, (LPARAM)lpMsg);
iEntries = SendMessage(GetDlgItem(hDlg, IDC_STATUS), LB_GETCOUNT, 0, 0);
SendMessage(GetDlgItem(hDlg, IDC_STATUS), LB_SETTOPINDEX, iEntries -1, 0);
}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PREMERPAS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PREMERPAS);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}

------------------------------------------------------------
Ressource file
------------------------------------------------------------

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by premer pas.rc
//
#define IDC_MYICON 2
#define IDD_PREMERPAS_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDS_APP_TITLE 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDS_HELLO 106
#define IDI_PREMERPAS 107
#define IDI_SMALL 108
#define IDC_PREMERPAS 109
#define IDR_MAINFRAME 128
#define IDD_DIALOG1 129
#define IDD_WSSERVER 130
#define IDC_EDIT1 1003
#define IDC_STATE 1004
#define IDC_LSTSTATE 1005
#define IDC_STATUS 1021
#define IDC_OPEN 32771
#define IDC_STATIC -1


#define IDSTR_SOCKET &quot;Starting of sochet&quot;

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 131
#define _APS_NEXT_COMMAND_VALUE 32772
#define _APS_NEXT_CONTROL_VALUE 1006
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
 
Go in ProjectSettings(Alt+F7)->Link and add this to the Object/Library modules
Ws2_32.lib

Also add at the beginning of your .cpp:
#include <Winsock2.h>

Hope this helps, s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top