Solved

C/C++ socket question

Posted on 2012-03-29
2
366 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now