Hi all,
This is my first post in this forum.
I have a issue with socket connection related to non-blocking sockets. Here is the code which i am using under Linux
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#define PORT 0x1234
#define PASS 0
#define FAIL 1
int main()
{
int listenfd,sendfd,acceptfd;
struct sockaddr_in serveraddr,clientaddr;
int retc;
int accetfd;
int servlen = sizeof(serveraddr);
char *recv_buffer, *send_buffer;
int i,j,k;
int err_cnt = 0;
int flag;
int send_done;
int bytes_to_send, bytes_to_read;
int num_bytes_send, num_bytes_read;
/* initialise all */
listenfd = sendfd = acceptfd = 0;
recv_buffer = send_buffer = NULL;
/* allocate buffer for both send and receive */
recv_buffer = (char *)malloc (1024 * 1024);
if(recv_buffer == NULL)
{
printf("Can't allocate memory\n");
retc = FAIL;
goto done;
}
send_buffer = (char *)calloc(1, 1024 * 1024);
if(send_buffer == NULL)
{
printf("Can't allocate memory for send buffer\n");
retc = FAIL;
goto done;
}
/* open socket for send and receive */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0)
{
perror("listenfd socket failed");
retc = FAIL;
goto done;
}
sendfd = socket(AF_INET, SOCK_STREAM, 0);
if(sendfd < 0)
{
perror("sendfd socket failed");
retc = FAIL;
goto done;
}
/* make both send and receive non-blocking */
if ( (flag = fcntl(listenfd, F_GETFL, NULL)) < 0)
{
perror("listenfd fcntl:");
retc = FAIL;
goto done;
}
flag |= O_NONBLOCK;
if ( fcntl(listenfd, F_SETFL, flag) < 0)
{
perror("listenfd fcntl:");
retc = FAIL;
goto done;
}
if ( (flag = fcntl(sendfd, F_GETFL, NULL)) < 0)
{
perror("sendfd fcntl:");
perror("listenfd fcntl:");
retc = FAIL;
}
flag |= O_NONBLOCK;
if ( fcntl(sendfd, F_SETFL, flag) < 0)
{
perror("sendfd fcntl:");
perror("listenfd fcntl:");
retc = FAIL;
}
/* Bind to socket */
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(PORT);
clientaddr.sin_family = AF_INET;
clientaddr.sin_addr.s_addr = INADDR_ANY;
clientaddr.sin_port = htons(PORT);
if (bind (listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1)
{
perror("Bind error under listenfd");
retc = FAIL;
goto done;
}
/* listen for incoming connection */
if (listen(listenfd, 1) == -1)
{
perror("Listen socket error");
retc = FAIL;
goto done;
}
/* establisg connection */
if (connect(sendfd, (struct sockaddr *)&clientaddr,sizeof(struct sockaddr)) < 0)
{
perror("Connect call failed");
retc = FAIL;
goto done;
}
/* accept incoming connection */
if (accept(listenfd, (struct sockaddr *)&serveraddr, &servlen) < 0)
{
perror("Accept call failed");
retc = FAIL;
goto done;
}
retc = PASS;
/* send and receive data */
for ( k = 0; k < 100; k++)
{
bytes_to_send = 1024 * 16;
i = 0;
j = 0;
bytes_to_read = 1024 * 16;
send_done = 0;
while(1)
{
if ( send_done == 0)
{
num_bytes_send = send(sendfd, &send_buffer[j], bytes_to_send,0);
if (num_bytes_send == -1)
{
perror("Sending data failed");
retc = FAIL;
break;
}
else if (num_bytes_send == 0)
{
send_done = 1;
}
else
{
bytes_to_send -= num_bytes_send;
j += num_bytes_send;
}
}
if (retc == FAIL)
break;
num_bytes_read = read(acceptfd, &recv_buffer[i], bytes_to_read);
if (num_bytes_read == -1)
{
perror("Receive data failed");
retc = FAIL;
break;
}
else if (num_bytes_read == 0)
{
break; /* Break on EOF */
}
else
{
bytes_to_read -= num_bytes_read;
i += num_bytes_read;
}
}
if ( retc == FAIL)
break;
for ( i = 0; i < 1024 * 16; i++)
{
if ( recv_buffer[i] != (char)i)
{
printf("Error: receive data not correct, \n");
printf("expected : 0x%x, received: 0x%x\n", i, recv_buffer[i]);
retc = FAIL;
err_cnt ++;
if ( err_cnt > 10) break;
}
}
if (retc != PASS)
break;
}
done:
if ( sendfd != 0 )
{
if (close(sendfd ) < 0)
perror( "close sendfd:");
}
if ( acceptfd != 0 )
{
if (close(acceptfd ) < 0)
perror( "close accetfd:");
}
if ( listenfd != 0)
{
if (close(listenfd) < 0)
perror("close listen:");
}
if ( send_buffer != NULL)
free(send_buffer);
if ( recv_buffer != NULL)
free(recv_buffer);
printf("Test.......");
if(retc == PASS)
printf("PASSED\n");
else
printf("FAILED\n");
return retc;
}
When i try to run this code i am getting error while connecting to socket
Connect call failed: Operation now in progress
Can anyone help..... Thanks in advance
-BalaC-