Solved

C/C++ socket question

Posted on 2012-03-29
2
371 Views
Last Modified: 2012-03-29
Once the accept() is called to spawn a socket on the server side to talk to the client, I assume by some magic that spawned socket knows the IP address of the client so that the conversation can be continued. right?

If so, is there a way I can get the client's IP information and/or other identification infor of the client using that spawned socket on the serverside?
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 37782804
Yup, there is - use 'getpeername()', e.g.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>

int main()
{
	struct sockaddr_in sin;
	struct in_addr in;
	int sd, client_sd, len;

	/* create a socket */
	sd = socket(AF_INET,SOCK_STREAM,0);

	/* let the OS use any unused port */
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_port = htons(5555);

	/* bind */
	if(bind(sd,(struct sockaddr *)&sin, sizeof(sin)) < 0) {
		perror("bind");
		exit(1);
	}

	/* listen */
	listen(sd,10);

	while(1) {

		client_sd = accept(sd,NULL,NULL);
		if(client_sd < 0) {
			perror("accept");
			break;
		}

		memset(&sin,0,sizeof(&sin));
		len = sizeof(sin);
		getpeername(client_sd,(struct sockaddr*)&sin, &len);

		memset(&in,0,sizeof(in));
		in.s_addr = sin.sin_addr.s_addr;

		printf("Remote Peer IP Address: %s\n", inet_ntoa(in));
		printf("Remote Peer Port Number: %d\n", ntohs(sin.sin_port));
		printf("----------------\n");

		close(client_sd);
	}
	printf("client_sd = %d\n", client_sd);
	close(sd);
	return 0;
}

Open in new window

0
 

Author Comment

by:prain
ID: 37783386
Super. Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question