LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-07-2010, 04:56 PM   #1
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 161

Rep: Reputation: 15
Socket programming in C...print clients IP and queue number...


Hello all,

I'm messing around with socket programming in C. Basically trying to learn how to wait for client connection and print their IP and which host number are they in the current connection.

I can't get the code to show the IP and which client in the queue are they..

Code:
...
skt = socket(PF_INET,SOCK_STREAM,0);
if( skt == -1 ) die("Error with Socket()");

memset(&nodeSkt,0,sizeof (nodeSkt));
nodeSkt.sin_family = AF_INET;
nodeSkt.sin_port = htons(atoi(nodePort));
nodeSkt.sin_addr.s_addr = inet_addr(nodeAddr);
	
len_inet = sizeof(nodeSkt);
state = bind(skt,(struct sockaddr *)&nodeSkt,len_inet);
state = listen(skt,3);

len_inet = sizeof(clntSkt);
Clntsocket = accept(skt, (struct sockaddr *) &clntSkt,&len_inet);
   printf("Client OK!");
   if ( Clntsocket == -1 ) die("Error");

strcpy(data_buffer, "Your IP is: " && (struct sockaddr *) &clntSkt && "  Your are a connected client number: " ??????????);
bytes = strlen(data_buffer);
state = write(Clntsocket,data_buffer,bytes);
...
I have most of the code working, but I'm having issues with the line:

Code:
strcpy(data_buffer, "Your IP is: " && (struct sockaddr *) &clntSkt && "  Your are a connected client number: " ??????????);
I'm not sure if the IP of the client is being returned and not sure how to get the number of connected client from "listen(skt,3)"

Thank you for the help in advance...I'm real new to this so sorry for the errors...
 
Old 02-07-2010, 07:53 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
For showing the client IP, see some of the examples here: http://beej.us/guide/bgnet/output/ht...t_ntopman.html.

EDIT: Maybe this example more complete will help: http://en.wikibooks.org/wiki/C_Progr..._simple_server. After it accepts, it seems to be printing the client address.

Last edited by nadroj; 02-07-2010 at 08:01 PM.
 
Old 02-07-2010, 11:51 PM   #3
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 161

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nadroj View Post
For showing the client IP, see some of the examples here: http://beej.us/guide/bgnet/output/ht...t_ntopman.html.

EDIT: Maybe this example more complete will help: http://en.wikibooks.org/wiki/C_Progr..._simple_server. After it accepts, it seems to be printing the client address.
Thanks for the link, however I'm not sure if I am doing this right, but I'm not getting any output. If I compile both codes, I only get blank terminals and after a while, on the client side I get "Received 0 bytes"...would you mind posting a quick note how you got it to work? Thank you!
 
Old 02-08-2010, 02:22 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
Well I didnt get anything to work, cause I didnt do anything!

In the second link, hopefully you noticed that there is examples of both a full client and server code. I would copy and paste the code into two files, compile, and run them. They should work fine, and just study them a little bit to make sure you understand what it is doing.

There arent many different ways to do this properly, so the way you would have to do it to get it to work would be identical to the code in those examples. What I'm trying to say is that its probably OK to "copy" and use this code. This is opposed to say, copying a complete homework assignment or other project.

So, again, start with the given code which should be enough to get you going and understanding. Post the code you use and any errors, warnings, questions you may have.

The explanation on the site seems good enough. One thing fundamental to program is reuse. So its better for me to suggest to read some good resource, versus me paraphrasing it or just re-writing it again. However, again, if you have any specific questions then of course that requires some specific attention and response.

Last edited by nadroj; 02-08-2010 at 02:24 PM.
 
Old 02-08-2010, 02:52 PM   #5
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 161

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nadroj View Post
Well I didnt get anything to work, cause I didnt do anything!

In the second link, hopefully you noticed that there is examples of both a full client and server code. I would copy and paste the code into two files, compile, and run them. They should work fine, and just study them a little bit to make sure you understand what it is doing.

There arent many different ways to do this properly, so the way you would have to do it to get it to work would be identical to the code in those examples. What I'm trying to say is that its probably OK to "copy" and use this code. This is opposed to say, copying a complete homework assignment or other project.

So, again, start with the given code which should be enough to get you going and understanding. Post the code you use and any errors, warnings, questions you may have.

The explanation on the site seems good enough. One thing fundamental to program is reuse. So its better for me to suggest to read some good resource, versus me paraphrasing it or just re-writing it again. However, again, if you have any specific questions then of course that requires some specific attention and response.
The only issue I am having is that the code in the second link does not do anything for me. After I compile and execute, nothing happens on the server side and the client side shows 0 bytes after few minutes...hence I'm just asking if I'm doing something wrong since I'm quite new to learning socket programming in C...
 
Old 02-08-2010, 03:01 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
Oh I see. Ill try the code now to see what I get.
 
Old 02-08-2010, 03:07 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
I did an exact copy/paste to two files and it seems to run fine.

Are you running the server first and then the client in another window? Are you running it both files on the same machine? Is this your machine or is it a school server or something that may have restrictions? Can you make sure that you get a result from "ping 127.0.0.1"? Or have you changed the IP address in the file?

My guess is that either you're running the code on two different machines, or you've changed the IP. A firewall would be the problem in either case.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print queue lectraplayer Linux - Newbie 9 11-01-2010 04:43 AM
CUPS Queue won't print johnny1959 Linux - Newbie 10 08-08-2009 11:48 PM
CUPS Print Queue Manager for Print Server Matir Linux - Software 9 01-11-2009 04:32 AM
CUPS and number-up queue configuration linuxbird Linux - Networking 2 10-04-2007 02:00 AM
listen queue of a socket overflowed, process is not taking new requests bhaskarna Programming 2 07-18-2006 03:06 AM


All times are GMT -5. The time now is 07:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration