2

Well i am trying to run the server side and the client side in a for loop. The first time I run it, it runs fine but the second time either the connect fails or it gets stuck at accept(). Here's my code:

Client code:

for(i=0;i<2;i++)
{
        if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nError : Connect Failed\n");
        return 1;
    }   

    //***************Writing_I***************
    memset(recvBuff, '0',sizeof(recvBuff));
    ticks = time(NULL);
    snprintf(recvBuff, sizeof(recvBuff), "%.24s\r\n", ctime(&ticks));
    if((n = send(sockfd, recvBuff, strlen(recvBuff), 0))<0)
    {
        printf("Error : send operation failed\n");
    }

    //***************Reading_I***************
    memset(recvBuff, '0', sizeof(recvBuff)); 
    n= recv(sockfd, recvBuff, sizeof(recvBuff)-1, 0);
    {
        printf("bytes read: %d\n", n);
        recvBuff[n] = '\0';
        printf("Data: %s\n",recvBuff);
    } 
    if(n < 0)
    {
        printf("\n Read error \n");
    } 
}
close(sockfd);  

Server code:

if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL))>=0)
{ 
    for(i=0;i<2;i++)    
    {   
        fd= open("/home/t/Desktop/CS II/A4/test.txt", O_RDONLY);
        //***************Reading***************
        memset(sendBuff, '0', sizeof(sendBuff)); 
        count = recv(connfd, sendBuff, sizeof(sendBuff)-1, 0);
        {
            printf("bytes read: %d\n", count);
            sendBuff[count] = '\0';
            printf("Data: %s\n",sendBuff);
        }
        if(count < 0)
        {
            printf("Read error \n");
        }

        //***************Writing***************
        ticks = time(NULL);
        memset(sendBuff, '0', sizeof(sendBuff)); 
        printf("Reading from file\n");
        if((noOfBytesRd= read(fd, sendBuff, sizeof(sendBuff)-1))<0)
            printf("\n Error : read operation failed\n");
        sendBuff[noOfBytesRd]='\0';
        if((count = send(connfd, sendBuff, strlen(sendBuff), 0))<0)
            printf("\n Error : wtite operation failed\n");
    }
    close(connfd);  
 }
brokenfoot
  • 11,083
  • 10
  • 59
  • 80

1 Answers1

6

A socket can be connect'ed only once in its life cycle. You should create a separate socket for each connection you intend to make, and close it when you're done with the connection.

Alexey Feldgendler
  • 1,792
  • 9
  • 17
  • 1
    Thanks a ton.I moved the connect() outside the for loop and it worked. But if I have multiple threads on the client side, should I assign them with different socketIds and run them parallely or run them sequentially with the same socketId with synchronization? Which is a preferred method of doing it? Thanks again! – brokenfoot Nov 26 '12 at 02:19
  • 1
    I'm not sure I understand your question. Can you describe what you are trying to achieve? – Alexey Feldgendler Nov 26 '12 at 02:24
  • 1
    I am trying to send no of requests parallely from the client, for that I need to generate multiple threads and each thread will send a request to the server. On the server side also, there will be a number of threads taking care of the request received from the client. So, at the client side, should I create a socket for each thread or just use the same socket for all the threads with some sort of synchronization? I want to know how is this usually done. – brokenfoot Nov 26 '12 at 02:30
  • 1
    Running a thread per outgoing connection is one possible approach, but you need to be careful about synchronization of access to shared resources to avoid race conditions. Of course, each connection needs its own socket with a uniqeue fd. Another strategy is to handle all outgoing connections (still each with its separate socket) in a single thread, serializing your logic using `select()`. – Alexey Feldgendler Nov 26 '12 at 02:34
  • https://github.com/dolev146/JACOB-SORBER-multithreaded-server-all-parts watch this repo i made – Dolev Dublon May 20 '22 at 21:46