?
Solved

C/C++ socket question

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

771 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