How to test a socket conncection in C++ ?


 
Thread Tools Search this Thread
Top Forums Programming How to test a socket conncection in C++ ?
# 1  
Old 12-14-2009
Java How to test a socket conncection in C++ ?

hi all,

I've written a Server socket program in C++, In my program Device Id is a unique one through which I accept the client connection. My code has to pass the below two test case.

1. If a client the the same device is connceted already, i've to reject the new conncetion.
2. If i remove the client network cable and try to conncet again it has to connect.

Can any one tell me how to do this?.

the major problem that i'm facing is in the 1st test case. The Function that i've written is


Code:
bool ValidatePrinterConnection(string sDeviceName)
{
    char cMessage[1024];
    memset(cMessage,0,1024);
    string sRecvData;
    struct fd_set readfds;
    struct timeval time;
    time.tv_sec =  20;    time.tv_usec = 0;
    bool bret = true;
    int iSocketDesc = m_pConfig->GetSocketDesc1( m_sDeviceName ); //this will return the previous socket desc of the connceted client
 
    for ( int i = 0; (i < 3 && bret == true) ; i++)
    {
        if ( iSocketDesc == 0 )
        {
            return false;
        }
        int iStatus=send(iSocketDesc,"R U connceted",100,0); // test data 
        if ( iStatus > 0 )
        {
        FD_ZERO(&readfds);
        FD_SET(iSocketDesc, &readfds);
            int iRetcode = select(iSocketDesc + 1 , &readfds,(fd_set*)NULL, (fd_set*)NULL, &time);
        if ( iRetcode < 0 )
        {
                bret = false;
        }
        if ( iRetcode == 0)
        {
                bret = true;
            }
            if ( iRetcode == 1 )
            {
                bret = true;
        }
        }
        else
        {
            bret = false;
        }
    }
    return bret;
}


the above code is working fine in few time and giving false information in lot of times. Can any one tell me how to do it in a effective manner?


thanks in advance..
vijay

Last edited by pludi; 12-14-2009 at 03:03 AM.. Reason: code tags, please...
# 2  
Old 12-14-2009
I can't tell what your code is doing since some important behavior is in objects you don't show. What is m_pConfig?
# 3  
Old 12-15-2009
just forgot that line.. that is the configuration class. That method will return the socekt descriptor.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Using socket to test a TCP port

Hello, I'm trying to write a small c application to test a tcp port. This works fine for the most part but the default timeout on the connect is very long. I have been reading many posts but and it looks like I need to set the socket to be non-blocking and poll for a result. I have been totally... (2 Replies)
Discussion started by: tjones1105
2 Replies

2. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

3. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

4. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

8. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

9. Programming

Wireless C program conncection interruption problem

Dear Gurus My company has a wireless handheld C program as text-mode workstations in remote sites. We suspected that the program get interrupted sometimes because of out-of-wireless access and hence resulted in db record locking and other issue. It did trouble us a lot. Is it possible to use... (2 Replies)
Discussion started by: charles_chu
2 Replies
Login or Register to Ask a Question