Socket creation problem

Started by
8 comments, last by hplus0603 14 years, 1 month ago
Hello All, I am old to here but new to socket programming :) I am making program like chat server. I have done some coding with net help and with my own logic. I want to make 2 socket for client and server communication. I have done this client/server socket code. This is running well for my Pc. But when i am placing client.exe file to another PC and running both executable.My server.exe which is running from my pc is not getting client.exe file and client.exe file is also not knowing server is running. What is the concept behind socket to know another pc ? I am using c++ in windows environment. thanks.
Advertisement
Do you mean connect() fails? Is the target machine on the same network, or over the Internet?
I'd guess it was a firewall setting preventing you from connecting to the remote machine.
yes.. TY
This is working now. my server is recognizing my client. I want to fetch my IP address dynamically means by function somthing like getIPaddress(), what is the exact function to fetch IP address. i m using <winsock2.h>,#include <windows.h> #include <fcntl.h> important header files.
You can't really - If you're behind a router, you can only get your IP address on the local network. To get the external IP, you'd need to use a site like whatismyip.com. To do that in code, that'd mean making an HTTP request to the site, and then parsing the returned page - which is non-trivial.
this is necessary so i am trying for it.

presently my server is recognizing client connection and this fetching message from client socket. but i want to place these client.exe file 3rd computer also. and want to know haw many client are presently connected with server.exe file. how will i list all client ip?
The server knows how many clients are connected (Because it keeps track of the accept()ed sockets), so you can just call getpeername() on each socket to get the remote address.
I saw whatismyip.com site but this is showin my dynamic IP becz when i m refreshing page IP is being change. I want to fetch my IP which is showing from LAN icon --> properties-->TCP/IP.
getpeername(....) is returning address of peer of each socket which connected with server. so is this necessary each socket must different peer? my server socket code is something like this when socket is accepting client socket

if((*csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){
printf("Received connection from %s",inet_ntoa(sadr.sin_addr));
printf("\n\n %u",getpeername(hsock,SOCKADDR*)&sadr,len));
// len is containg pointer to integer with 100 value
CreateThread(0,0,&SocketHandler, (void*)csock , 0,0);
}

is above code is right ? I have written this in server.cpp file.

OR i want to make an array which will store all ip when client socket will be connected.whenever client socket will be connect server will pass all information of array in client socket.Therefore client socket will know how much clinet is connected with server. what about it ? and plz must see above code .
Quote:Original post by ryan009ryan
I saw whatismyip.com site but this is showin my dynamic IP becz when i m refreshing page IP is being change. I want to fetch my IP which is showing from LAN icon --> properties-->TCP/IP.
If you want to get the IP address of the network adapter, you can use getsockname() on the listening socket.


Quote:Original post by ryan009ryan
getpeername(....) is returning address of peer of each socket which connected with server. so is this necessary each socket must different peer? my server socket code is something like this when socket is accepting client socket

if((*csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){
printf("Received connection from %s",inet_ntoa(sadr.sin_addr));
printf("\n\n %u",getpeername(hsock,SOCKADDR*)&sadr,len));
// len is containg pointer to integer with 100 value
CreateThread(0,0,&SocketHandler, (void*)csock , 0,0);
}

is above code is right ? I have written this in server.cpp file.
You should be calling getpeername() on csock, not on hsock, since it's the client you're interested in. Although I think getpeername() will give the same sockaddr as you get from accept() anyway.

Quote:Original post by ryan009ryan
OR i want to make an array which will store all ip when client socket will be connected.whenever client socket will be connect server will pass all information of array in client socket.Therefore client socket will know how much clinet is connected with server. what about it ? and plz must see above code .
Are you saying that you want all clients to know about all other clients? If so, then you can use getpeername() or the sockaddr from accept() to get the address of each client, and send that to the clients when they connect.
The way the Internet is supposed to work, a client will find a server through a configured domain name. For example, "mygameserver.mydomain.net". The server will then run the service process at a known port, specific to the game/service. For example, the HTTP service is run on port 80, and the SSH service is run on port 22. You can generally pick an arbitrary port number in the range 4000-60000 for your game.

The easiest way to look up a host by name is to call gethostbyname().

If you run your server "at home," then it will not have a publicly routable IP address. To make that work, you have to set up port forwarding on your router, so that the server port forwards to the server machine. Additionally, if you do not have a "static IP" for your home internet service, you'll want to use a service like dyndns.com to map a known name to your current IP. You'll then make "mygameserver.mydomain.net" be a CNAME pointer to the dyndns name, which in turn resolves to your IP address.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement