Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

yungivan

Winsock - how to get client's IP?

This topic is 5928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Assuming:
1. Windows 98/NT/ME/2K/XP
2. MS Visual studio


  
include <winsock2.h>

struct hostent * host = gethostbyname("localhost");

unsigned int O1 = (unsigned char)host->h_addr_list[0][0]
unsigned int O2 = (unsigned char)host->h_addr_list[0][1]
unsigned int O3 = (unsigned char)host->h_addr_list[0][2]
unsigned int O4 = (unsigned char)host->h_addr_list[0][3]


This is off the top of my head, it may not be correct but it will get you looking in the right direction.


Go to your help and look up winsock and sockets.


As to you second post, look up recv and send in the socket section.



D.V.

Carpe Diem

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, as above check docs on the hostent struct and how to get it, make sure you don''t go outside the memory bounds!!

As for sending a FILE, you have to read the file into memory, and then send it over a socket with send() as you would with any other data.

Share this post


Link to post
Share on other sites
You can get the IP of the connecting client when you call accept, or, as Fruny pointed out, via getpeername.

As for sending a file, there''s no winsock function to do that, but what''s to stop you reading the file in using regular file I/O and sending it via send?

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
All attempts to get the local IP locally are doomed to fail, especially if there are multiple network cards. A computer has one IP per interface, usually two (127.0.0.1 and one for internet). Often 3, my main PC has one card for ADSL and one for the local net.
My game PC is 192.168.0.3, if I would do this I would get an unusable address.

Especially gethostbyname("localhost"), that will just return 127.0.0.1

What you should do in a client-server setup is not to let the client send its IP by default. Make the client send a packet or open a TCP connection to the server with in it an empty IP address, then you use the one from the connection. If filled in, use that address. Then make a setting that the user can set a hostname to override if he has problems.

But you shouldn''t need the hostname. Just have clients open a connection and communicate on that. Don''t assume that because a client can connect to you, you can connect to that client. Often you will receive a connection from some company''s firewall, because it routes the internal traffic outwards (socks, proxy). If you try to connect back a lot you could be in trouble.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hostent contains all the IP addresses assigned to the computer.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!