Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:09:57+00:00 2026-05-31T18:09:57+00:00

Using sockets, I am not sure how to set the timeout? thanks int sock,

  • 0

Using sockets, I am not sure how to set the timeout?

thanks

int sock, connected, bytes_recieved;
char send_data [128] , recv_data[128];       
SOCKADDR_IN server_addr,client_addr;    
int sin_size;
int j = 0;

::socket(AF_INET, SOCK_STREAM, 0);

server_addr.sin_family = AF_INET;         
server_addr.sin_port = htons(4000);     
server_addr.sin_addr.s_addr = INADDR_ANY; 

::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
::listen(sock, 5);
::fflush(stdout);

while(1)
{  
    sin_size = sizeof(struct sockaddr_in);
    connected = ::accept(sock, (struct sockaddr *)&client_addr, &sin_size);

    while (1)
    {
        j++;

        ::send(connected, send_data, strlen(send_data), 0);  

        //dealing with lost communication ? 
        //and reastablishing communication
        //set timeout and reset on timeout error    
    }
}
::closesocket(sock);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:09:58+00:00Added an answer on May 31, 2026 at 6:09 pm

    A socket is in blocking mode by default. If you switch it to non-blocking mode using ioctlsocket(FIONBIO), you can use select() to manage timeouts:

    SOCKET sock, connected;
    int bytes_recieved;  
    char send_data [128] , recv_data[128];         
    SOCKADDR_IN server_addr,client_addr;      
    int sin_size;  
    int j = 0, ret;  
    fd_set fd;
    timeval tv;
    
    sock = ::socket(AF_INET, SOCK_STREAM, 0);  
    
    server_addr.sin_family = AF_INET;           
    server_addr.sin_port = htons(4000);       
    server_addr.sin_addr.s_addr = INADDR_ANY;   
    
    ::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));  
    ::listen(sock, 1);  
    ::fflush(stdout);  
    
    u_long nbio = 1;
    ::ioctlsocket(sock, FIONBIO, &nbio);
    
    while(1) 
    {   
        FD_ZERO(&fd);
        FD_SET(sock, &fd);
    
        tv.tv_sec = 5;
        tv.tv_usec = 0;
    
        if (select(0, &fd, NULL, NULL, &tv) > 0)
        {
            sin_size = sizeof(struct sockaddr_in); 
            connected = ::accept(sock, (struct sockaddr *)&client_addr, &sin_size); 
    
            nbio = 1;
            ::ioctlsocket(connected, FIONBIO, &nbio);
    
            while (1) 
            { 
                j++; 
    
                if (::send(connected, send_data, strlen(send_data), 0) < 0)
                {
                    //dealing with lost communication ?  
                    //and reastablishing communication 
                    //set timeout and reset on timeout error     
    
                    if (WSAGetLastError() == WSAEWOULDBLOCK)
                    {
                        FD_ZERO(&fd);
                        FD_SET(connected, &fd);
    
                        tv.tv_sec = 5;
                        tv.tv_usec = 0;
    
                        if (select(0, NULL, &fd, NULL, &tv) > 0)
                            continue;
                    }
    
                    break;
                }
            } 
    
            closesocket(connected);
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an applet that communicates with a servlet using Http (Not sockets). Currently,
i am using raw sockets to create my own socket. i need to set
I am writing a program in java using sockets with UDP. For a number
greetings, i am writng a socket program using sockets in c# (asynchronous). the issue
Using Java sockets, I made a simple server. This works because it sends data
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using
I am creating a chat application using java sockets programming. I want to launch
I need to create 1000 server sockets using Java. Somewhere between creating 600 and
I am writing a client/server program using C sockets. I am specifying that the
I'm using sockets for communication between various processes right now. Everything is running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.