Thread: socket problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    14

    socket problem

    Hi.

    I was trying to make this teste server, but i have a problem. The socket can't be opened. The code compiled well, but when i launch the .exe file, i get the error "Can't creat socket." and I just don't understand why. Here his an extract of the code:

    Code:
    #define _WINSOCKAPI_
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <Winsock2.h>
    #include "useful.h"
    //requires Ws2_32.lib
    
    int main(int argc, char** argv) {
    
    	SOCKET sockfd;
    	int rm;
    	char message[256];
    	struct sockaddr_in serv_addr;
    	//hostent* localHost;
    	//char* localIP;
    
    	if(argc != 3)
    		error_dump("Invalid parameters.\n");
    		
    	if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    		error_dump("Can't creat socket.\n");
    Another problem was that hostent* was not recognized besides including Winsock2.h and compiling with Ws2_32.lib.

    Can someone help me?

    Thanks a lot.

    My best regards.

    PS: I've windows XP sp2

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Under Windows, you must make a call to WSAStartup() before using any socket functions. (And you should also call WSACleanup() when finished.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	struct hostent*		hostEnt;
    	HOSTENT*		hostEnt;

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Gee... so complicated. I've found an example on msdn. Linux is so much easier. Isn't there a smaller way to do that?

    Here is that huge example:
    http://msdn.microsoft.com/library/de...cketserver.asp

    Thanks a lot.

  5. #5

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Thanks a lot

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Hi. I've had another problem. After spending lots of hours searching on MSDN and other sites, i can't find the right solution. When i use gethostbyname it returns the wrong IP address (like de LAN ip when I want the inet one). I read about that problem, and the solution seems to to be using GetAdaptersAddresses or GetAdaptersInfo. GetAdaptersAddresses seems to be good but i cant get the IP from it, just other info. Can anybody help? Thanks!

  8. #8
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    before making a call to socket() you must call WSAStartup, here's a piece of code from a class i made that estamblishes a connection:
    Code:
    	bool make_connection()
    	{
    	  WSADATA wsaData;
    	  struct hostent *h;
    	  
          if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    	  {
    	    error_log = "WSAStartup()\n";
    	    return false;
    	  }
    
    	  if ((h = gethostbyname(host.c_str())) == 0)
    	  {
    		error_log = "gethostbyname()\n";
    		return false;
    	  }
    
    	  memset(&address, 0, sizeof address);
    	  address.sin_family = AF_INET;
    	  address.sin_port = htons(port);
    	  memcpy(&address.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    
    	  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	  {
    		error_log = "socket()\n";
    		return false;
    	  }
    
    	  if (connect(sock, (struct sockaddr *)&address, sizeof address) == -1)
    	  {
    	    closesocket(sock);
            error_log = "connect";
         	return false;
    	  }
    
    	  is_connected = true;
    	  return true;
    	}
    :wq

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Wow, that looks more simple. What's that "host" anyway?
    Code:
    host.c_str()
    thanks

  10. #10
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    host is a just a private std::string in my class but you can use an average char * instead of host.c_str(), the class is in c++ so you might want to change it a bit. address var is of type struct sockaddr_in and is also a member of the class.
    :wq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 01:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM