socket connect returning odd

This is a discussion on socket connect returning odd within the C++ Programming forums, part of the General Programming Boards category; I am trying to do a simple socket connect and test whether my connection succeeds or not. My reading tells ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Question socket connect returning odd

    I am trying to do a simple socket connect and test whether my connection succeeds or not. My reading tells me that connect upon failure should return the value of -1. I disabled the device and I am attempting to connect to, and yet my program still says the connection was successful. So I know I've goofed at this point. Does anyone see anything wrong with my testing or have just got my socket setup wrong..... Thanks for any help-

    Code:
    //we will need variables to hold the client socket.
        //thus we declare them here.
    	cout <<"opening client socket"<<endl;
        SOCKET client;
    	client=socket(AF_INET,SOCK_DGRAM,0);
        sockaddr_in from;
    //	memset((char *) &from,0,sizeof(from));
    	from.sin_family=AF_INET;
    	from.sin_addr.s_addr=inet_addr ("172.20.30.30");
    	from.sin_port=htons((u_short)6101);
        int fromlen=sizeof(from); 
    
    	//connect to printer
    	if ((connect (client, (struct sockaddr*)&from, sizeof from)==-1))
    	{
    		cout <<"client connect failed"<<endl;
    		
    	}
    	else if ((connect (client, (struct sockaddr*)&from, sizeof from)!=-1))
    	{
    		cout <<"client connection successful"<<endl;
    		
    	}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    connect is used with contection oriented sockets such as SOCK_STREAM, datagram sockets are connectionless.

    Code:
    if ((connect (client, (struct sockaddr*)&from, sizeof from)==-1))
    	{
    		cout <<"client connect failed"<<endl;
    		
    	}
    	else if ((connect (client, (struct sockaddr*)&from, sizeof from)!=-1))
    	{
    		cout <<"client connection successful"<<endl;
    		
    	}
    here you try to connect twice you just need
    Code:
    if ((connect (client, (struct sockaddr*)&from, sizeof from)==-1))
    	{
    		cout <<"client connect failed"<<endl;
    		
    	}
    	else
    	{
    		cout <<"client connection successful"<<endl;
    		
    	}

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    164
    good point that makes alot of sense, I am connecting twice....

    I need to use a datagram based socket, is there anyway to do a connect to push this data using datagram or can I just simply use a send command to push to a defined socket?

    I know DGRAM is not preferred but the device I am writing requires a udp based connection and send.....

    Thanks for your code response I am now getting a connection failed message!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To use a datagram socket use sendto()/recvfrom().

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    164
    I'll give it a shot thanks again! I'll post success or failures.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    Bummer.... not much going on, I'm not sure if the connect() command will work with DGRAM settings or not, but I do not seem to be pushing anything to the ip and port I am trying to reach.

    Kinda bummed. the sendto() command does not appear to work, I can paste and copy the same file data with a telnet session to the device on the ip and port and get the response I want, but when my socket app no luck.

    I tried to start from scratch and just make a client program that attaches to a server and drops the data, but still nothing happening...
    Code:
    int sockfd; 
        struct sockaddr_in dest_addr;   // will hold the destination addr 
    
    	sockfd = socket(AF_INET, SOCK_DGRAM, 0); // do some error checking! 
    
        dest_addr.sin_family = AF_INET;          // host byte order 
        dest_addr.sin_port = htons(DEST_PORT);   // short, network byte order 
        dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); 
        memset(&(dest_addr.sin_zero), '\0', 8);  // zero the rest of the struct 
    sendto (sockfd, barCode, strlen(barCode), 0, (struct sockaddr*)&dest_addr, sizeof dest_addr);
    how can I verify connection to my client or server using DGRAM??? Thanks-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Sombody PLEASE help!! Connect returning -1
    By ddoum9999 in forum C Programming
    Replies: 13
    Last Post: 01-03-2007, 02:27 PM
  3. Using a single socket for accept() as well as connect()?
    By nkhambal in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-20-2005, 05:43 AM
  4. My socket attempt refuses to connect
    By kzar in forum C Programming
    Replies: 3
    Last Post: 06-01-2005, 04:15 AM
  5. Odd socket trouble
    By Twiggy in forum C Programming
    Replies: 0
    Last Post: 03-31-2003, 04:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21