?
Solved

C/C++ socket question

Posted on 2012-03-29
2
Medium Priority
?
381 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

809 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