Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

compiler errors bei socket programmierung

Empfohlene Antworten

hi leute,

ich hab mir bei msdn folgenden quellcode besorgt


#include <windows.h> 

#include <winsock.h>


#define PORTNUM         5000          // Port number

#define HOSTNAME        "localhost"   // Server name string

                                      // This should be changed

                                      // according to the server.

int WINAPI WinMain (

              HINSTANCE hInstance,    // Handle to the current instance

              HINSTANCE hPrevInstance,// Handle to the previous instance

              LPTSTR lpCmdLine,       // Pointer to the command line

              int nCmdShow)           // Show state of the window

{

  int index = 0,                      // Integer index

      iReturn;                        // Return value of recv function

  char szClientA[100];                // ASCII string 

  TCHAR szClientW[100];               // Unicode string

  TCHAR szError[100];                 // Error message string


  SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server

  SOCKADDR_IN destination_sin;        // Server socket address

  PHOSTENT phostent = NULL;           // Points to the HOSTENT structure

                                      // of the server

  WSADATA WSAData;                    // Contains details of the 

                                      // Winsocket implementation


  // Initialize Winsocket. 

  if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0) 

  {

    wsprintf (szError, TEXT("WSAStartup failed. Error: %d"), 

              WSAGetLastError ());

    MessageBox (NULL, szError, TEXT("Error"), MB_OK);

    return FALSE;

  }


  // Create a TCP/IP socket that is bound to the server.

  if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)

  {

    wsprintf (szError, TEXT("Allocating socket failed. Error: %d"), 

              WSAGetLastError ());

    MessageBox (NULL, szError, TEXT("Error"), MB_OK);

    return FALSE;

  }


  // Fill out the server socket's address information.

  destination_sin.sin_family = AF_INET;


  // Retrieve the host information corresponding to the host name.

  if ((phostent = gethostbyname (HOSTNAME)) == NULL) 

  {

    wsprintf (szError, TEXT("Unable to get the host name. Error: %d"), 

              WSAGetLastError ());

    MessageBox (NULL, szError, TEXT("Error"), MB_OK);

    closesocket (ServerSock);

    return FALSE;

  }


  // Assign the socket IP address.

  memcpy ((char FAR *)&(destination_sin.sin_addr), 

          phostent->h_addr, 

          phostent->h_length);


  // Convert to network ordering.

  destination_sin.sin_port = htons (PORTNUM);      


  // Establish a connection to the server socket.

  if (connect (ServerSock, 

               (PSOCKADDR) &destination_sin, 

               sizeof (destination_sin)) == SOCKET_ERROR) 

  {

    wsprintf (szError, 

              TEXT("Connecting to the server failed. Error: %d"),

              WSAGetLastError ());

    MessageBox (NULL, szError, TEXT("Error"), MB_OK);

    closesocket (ServerSock);

    return FALSE;

  }


  // Send a string to the server.

  if (send (ServerSock, "To Server.", strlen ("To Server.") + 1, 0)

           == SOCKET_ERROR) 

  {

    wsprintf (szError, 

              TEXT("Sending data to the server failed. Error: %d"),

              WSAGetLastError ());

    MessageBox (NULL, szError, TEXT("Error"), MB_OK);

  }


  // Disable sending on ServerSock.

  shutdown (ServerSock, 0x01);


  for (;

  {

    // Receive data from the server socket.

    iReturn = recv (ServerSock, szClientA, sizeof (szClientA), 0);


    // Check if there is any data received. If there is, display it.

    if (iReturn == SOCKET_ERROR)

    {

      wsprintf (szError, TEXT("No data is received, recv failed.")

                TEXT(" Error: %d"), WSAGetLastError ());

      MessageBox (NULL, szError, TEXT("Client"), MB_OK);

      break;

    }

    else if (iReturn == 0)

    {

      MessageBox (NULL, TEXT("Finished receiving data"), TEXT("Client"),

                  MB_OK);

      break;

    }

    else

    {

      // Convert the ASCII string to a Unicode string.

      for (index = 0; index <= sizeof (szClientA); index++)

        szClientW[index] = szClientA[index];


      // Display the string received from the server.

      MessageBox (NULL, szClientW, TEXT("Received From Server"), MB_OK);

    }

  }


  // Disable receiving on ServerSock.

  shutdown (ServerSock, 0x00);


  // Close the socket.

  closesocket (ServerSock);


  WSACleanup ();


  return TRUE;

}

[/code]




beim kompilieren unter visual c++ 6.0 oder bloodshed 4.9.7.0 bekomme ich folgende fehlermeldungen...



[code] [Linker error] undefined reference to `WSAStartup@8' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `socket@12' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `gethostbyname@4' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `closesocket@4' [Linker error] undefined reference to `htons@4' [Linker error] undefined reference to `connect@12' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `closesocket@4' [Linker error] undefined reference to `send@16' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `shutdown@8' [Linker error] undefined reference to `recv@16' [Linker error] undefined reference to `WSAGetLastError@0' [Linker error] undefined reference to `shutdown@8' [Linker error] undefined reference to `closesocket@4' [Linker error] undefined reference to `WSACleanup@0'

alle includes sind vorhanden. das compilieren hab ich an 2 verschiedenen systemen jeweils mit den 2 oben genannten ides versucht und bin beides mal gescheitert. hoffentlich kann mir jemand von euch weiterhelfen.

gruß

ralf

Hi,

diese Fehlermeldung bedeutet dass der Linker in den ihm angegebenen

.Lib Dateien, den Verweis auf die DLL nicht finden kann in der die

Funktion definiert ist.

Du mußt einfach nur die Lib Datei dem Linker angeben. In deinem Fall

wäre das die wsock32.lib.

In VC++6 machst du das unter Projekt->Einstellungen->Linker und da einfach

in das Editfeld mit der Überschrift Objekt-/Bibliothek Module eintragen.

Gruß

Guybrush

danke :)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.