#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    austria
    Posts
    2
    Rep Power
    0

    Question Problems with socket-programming under winxp


    Hy everybody, i have a big problem with winsock programming under winxp, i have the following sourceode
    ...
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <unistd.h> /* close */

    #define SERVER_PORT 2012
    #define MAX_MSG 100

    int main (int argc, char *argv[]) {

    int sd, rc, i;
    struct sockaddr_in localAddr, servAddr;
    struct hostent *h;

    if(argc < 3) {
    printf("usage: %s <server> <data1> <data2> ... <dataN>\n",argv[0]);
    exit(1);
    }

    h = gethostbyname(argv[1]);
    if(h==NULL) {
    printf("%s: unknown host '%s'\n",argv[0],argv[1]);
    exit(1);
    }

    servAddr.sin_family = h->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    servAddr.sin_port = htons(SERVER_PORT);

    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if(sd<0) {
    perror("cannot open socket ");
    exit(1);
    }

    /* bind any port number */
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(0);

    rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
    if(rc<0) {
    printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
    perror("error ");
    exit(1);
    }

    /* connect to server */
    rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
    if(rc<0) {
    perror("cannot connect ");
    exit(1);
    }

    ..............and so on

    this simple client connects to an ibm-host i390, and now to my problem: i compiled and build the exe on a nt4.0 machine and also on a w2k machine. the code works fine with this 2 types of operating systems, but under winxp it doesn't work. the execution of the code stops at the connect to server line rs=connect.....
    with my error message cannot connect.....
    i need this little programm and it must be useable with winxp, has anybody got some ideas? is this code wrong or are there some special tricks for winxp-sockets?

    please help

    thx a lot
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Your program was written for UNIX. Unless WinXP has made sweeping changes, many of those header files don't exist, especially unistd (UNIX standard library, I believe).

    Instead, Windows uses Winsock. This link is to a PDF that explains how to convert a UNIX sockets program to Winsock. It's real quite easy to do:
    http://cs.baylor.edu/~donahoo/practi...owsSockets.pdf

    EDIT:
    Having re-read the last part of your post, I have to ask how you ever succeeded in compiling and running your program on NT and Win2k.
    Last edited by dwise1_aol; June 16th, 2003 at 10:03 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    austria
    Posts
    2
    Rep Power
    0
    I'm sorry, I posted the wrong piece of code
    here i have the code for windows-systems.....

    #include <StdAfx.h>
    #include <stdio.h>
    #include <conio.h>
    #include <winsock2.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <string>

    #pragma comment (lib, "Wsock32.lib")

    char *tcpsend(char *sndbuf1, char *sndbuf2, int iCmd, long *Ret)
    {
    struct hostent *host;
    WSADATA winsock_data;
    char szErrorMsg[128];

    int iRet;

    char szAddress[64];
    char szLocal[64];
    unsigned int iPort;
    char *szServer;

    unsigned int s;
    // szServer = "ATARCHT2";
    GetPrivateProfileString("Startup","ip","void",szAddress,sizeof(szAddress,"sock.ini");
    iPort = GetPrivateProfileInt("Startup", "port",2016, "sock.ini");

    if(iPort<0 || iPort>65563)
    {
    sprintf(szErrorMsg," \tWrong port number %d", iPort);
    *Ret = 100;
    return szErrorMsg;
    exit(1);
    }
    else
    {
    WSAStartup(MAKEWORD(1,1), &winsock_data);
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    }

    if(s == SOCKET_ERROR)
    {
    WSACleanup();
    exit(1);
    }
    else
    {
    addr.sin_family = AF_INET;
    addr.sin_port = htons(iPort);
    host = gethostbyname(szAddress);
    // host = gethostbyname(szServer);
    gethostname ( szLocal, sizeof( szLocal));
    }
    if (host == NULL)
    {
    if (s) closesocket(s);
    WSACleanup();
    }
    memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
    iRet = connect(s,(struct sockaddr *)&addr,sizeof(addr));

    .......and so on

    this is the code for windows, it is running under nt4.0 and w2k
    and this code only works under winxp when the following codelines are edited:

    at the beginning

    char *szServer;
    szServer = "ATARCHT2"
    ..
    ..
    ..
    host = gethostbyname(szServer);
    ..
    ..
    memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
    iRet = connect(s,(struct sockaddr *)&addr,sizeof(addr));
    ..
    ..

    when I use the getprivateprofilestring() method I get the
    servername out of an ini-file and it's stored in the char-array
    szAddress[64]

    to retrieve the ip-address of the servername stored in szAddress
    ...host = gethostbyname(szAdress);

    and this method isn't working under xp,
    do you know why?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Two possibilities come to mind.

    1. Since Win95 & probably the NT version just before that, Windows has been migrating away from .ini files over to the Registry. So I would expect to see support for .ini files to be disappearing. However, I doubt that it would have happened this abruptly. You might want to echo out the data you are reading from the .ini file just to be sure.

    2. There are issues over Winsock versions that have come up, but for which I don't quite remember. You are requesting version 1.1. Try version 2.0 and see if that works:
    WSAStartup(MAKEWORD(2,0), &winsock_data);

IMN logo majestic logo threadwatch logo seochat tools logo