× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Other end of a socket...
  • From: "Phil Hall" <hallp@xxxxxxxx>
  • Date: Tue, 13 Jun 2000 12:26:18 -0500

Here's an example of how to determine the port in use at the other end of a
socket connection...



#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(void)
{
  int    theSocket, structLen;
  short  thePort = 0;
  struct servent *se;
  struct sockaddr_in  rmtAddr;
  struct sockaddr_in  lclAddr;
  char                myInetAddr[256] = "127.0.0.1";

    se = getservbyname( "telnet", "tcp");
    if (se)
    {
        thePort = htons(se->s_port);
    }

    memset(&rmtAddr, 0, sizeof(rmtAddr));
    rmtAddr.sin_family      = AF_INET;
    rmtAddr.sin_addr.s_addr = inet_addr(myInetAddr);
    rmtAddr.sin_port        = htons(thePort);

    if ( (theSocket = socket(AF_INET, SOCK_STREAM, 0 )) < 0 )
    {
        printf("Could not open socket\n");
        return -1;
    }

    if (connect(theSocket, (struct sockaddr *) &rmtAddr,
        sizeof(rmtAddr)) < 0 )
    {
        printf("Cannot connect to telnet Server\n");
        close(theSocket);
        return -1;
    }

    /*-- let's get the local socket info --*/
    memset(&lclAddr, 0, sizeof(lclAddr));
    structLen = sizeof(lclAddr);

    if (getsockname( theSocket, (struct sockaddr *)&lclAddr, &structLen) <
0 )
    {
        printf("Failed to get local socket info\n");
        close(theSocket);
        return -1;
    }
    else
    {
        printf("Connected to server's remote port %d, using local port
%d\n",
                rmtAddr.sin_port, lclAddr.sin_port);
    }

    close(theSocket);
    return 0;
}


+---
| This is the C/400 Mailing List!
| To submit a new message, send your mail to C400-L@midrange.com.
| To subscribe to this list send email to C400-L-SUB@midrange.com.
| To unsubscribe from this list send email to C400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: bob@cstoneindy.com
+---


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.