help on tcp/ip device communcation..

This is a discussion on help on tcp/ip device communcation.. within the Networking/Device Communication forums, part of the General Programming Boards category; Hi.. i a newbies workin with vc++.. i suppose to come up with a program to get status of relay ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    Question help on tcp/ip device communcation..

    Hi..
    i a newbies workin with vc++..
    i suppose to come up with a program to get status of relay for a plc device.. i read up on winsock and got hold of a simple tcp/ip program(chat)..
    I was tinkin inorder to communcate with the device i need to send a string of code therefore if i can modify the chat prgaom i should be able to come up wiht a simple program.. the device is a auto reply..

    Problem is here.. the program i have has the destation tcp address set in the uility.h, i was hoppin to change it so that i can change it in my dialog part.. meaning i can check it when the dialog pop up..
    the current situation is the program compile then run with e fix add in the header.. is there any way to change that?

    or is my idea accept.... or u guys tink it better to write a new one.. i rushih 4 dateline.. i set got a hardware and a plc program to settle..

    thank alot

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Hi,

    It is possible to get the IP address at the run-time. IP can be entered at run-time in dotted decimal format as a string, like 1.2.3.4. You can then use API available such as inet_addr() or inet_aton() to convert it from the string format to the format required in structure sockaddr_in which unsigned long.

    A short sample would be

    Code:
    char line[20];
    unsigned long ip;
    unsigned short port;
    struct sockaddr_in remotsock;
    int sockfd;
    int ret;
    
    /* Initialize Winsock here */
    
    printf("Enter the Remote IP address the connect: \n");
    fgets (line,sizeof(line),stdin);
    line[strlen(line) - 1] = '\0';
    
    ip = inet_addr(line); /* Convert IP from string format to unsigned long */
    
    printf("Enter the Port : \n");
    fgets (line,sizeof(line),stdin);
    line[strlen(line) - 1] = '\0';
    
    sscanf(line, "%d", &port);
    
    remotesock.sin_family = AF_INET;
    remotesock.sin_addr.s_addr = ip;
    remotesock.sin_port = port;
    memset(&(remotesock.sin_zero),'\0',8);
    
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    
    ret = connect(sockfd,(struct sockaddr *)&remotesock, sizeof(sockaddr_in));
    
    if (ret < 0)
    {
         printf("Error in connect\n");
         exit(1);
    }
    
    /* Do send/recv() here after this */
    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM
  2. Device Driver: sysfs confusion
    By filker0 in forum Linux Programming
    Replies: 0
    Last Post: 12-02-2005, 11:36 AM
  3. Modem in Linux need help fast please!
    By xxxrugby in forum Tech Board
    Replies: 0
    Last Post: 03-30-2005, 04:10 PM
  4. Replies: 4
    Last Post: 06-30-2004, 04:11 PM
  5. Device problem
    By John22 in forum C Programming
    Replies: 0
    Last Post: 12-19-2002, 12:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21