Async Echo server/client app

21 views
Skip to first unread message

F.N

unread,
Dec 27, 2011, 7:20:11 AM12/27/11
to
Hello.

I ve written this app some days ago but i noticed that there is a
delay with the message transfer.
The idea of this app is that server receives messages from clients and
sends them back to the rest of them.
The method i used is with select() function.

Server:

[code]
#include <WinSock2.h>
#include <stdio.h>
#include <time.h>



main()
{

SOCKET ListeningSocket;
SOCKET AcceptSocket;

SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;

WSADATA wsaData;

const unsigned short PORT = 4444;

FD_SET fdread;
FD_SET BackUpfdread;
FD_SET fdwrite;
FD_SET BackUpfdwrite;

int maxDescriptor;
SOCKET SocketArray[20];
int index = 0;
int selectResults;
int i,k;
int clientAddrSize;
int RecvBytes;
int SentBytes;

char SentBuff[500];
char RecvBuff[500];

struct timeval timeout;

// Initialize Winsock2.2
WSAStartup(MAKEWORD(2,2),&wsaData);

// Initialize Listening Socket
ListeningSocket = socket(AF_INET,SOCK_STREAM,0);

// Initialize ServerAddr
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ServerAddr.sin_port = htons(PORT);

// Bind the ServerAddr with ListeningSocket
bind(ListeningSocket,(SOCKADDR
*)&ServerAddr,sizeof(ServerAddr));

// Listening Socket
listen(ListeningSocket,5);

FD_ZERO(&fdread);
FD_ZERO(&BackUpfdread);
FD_ZERO(&fdwrite);
FD_ZERO(&BackUpfdwrite);

// Asign ListeningSocket at fdread
FD_SET(ListeningSocket,&fdread);

maxDescriptor = ListeningSocket;

SocketArray[index] = ListeningSocket;
index++;

timeout.tv_sec = 0;
timeout.tv_usec = 0;

// Main loop starts here
for(; ;)
{

BackUpfdread = fdread;
BackUpfdwrite = fdwrite;
selectResults = select(maxDescriptor
+1,&BackUpfdread,&BackUpfdwrite,NULL,&timeout);
//printf("server select() OK\n");

if(selectResults == -1)
{
printf("Select() error");
WSACleanup();
return 0;
}


for(i=0;i<=index-1;i++)
{
//printf("%d\n",SocketArray[i]);
if(FD_ISSET(SocketArray[i],&BackUpfdread))
{
if(SocketArray[i] ==
ListeningSocket) // we have a new connection
{
clientAddrSize =
sizeof(ClientAddr);
AcceptSocket =
accept(ListeningSocket,(SOCKADDR *)&ClientAddr,&clientAddrSize);

// Add the newest accepted
socket to the fdread and fdwrite sets.
FD_SET(AcceptSocket,&fdread);
FD_SET(AcceptSocket,&fdwrite);

// Add the newest accepted
socket into SocketArray
SocketArray[index] =
AcceptSocket;
index++;

// keep track of the
maxDescriptor.
if(AcceptSocket >
maxDescriptor)
{
maxDescriptor =
AcceptSocket;
}

printf("New connection from %s
on socket %d\n", inet_ntoa(ClientAddr.sin_addr), AcceptSocket);

}else{ // That means that the socket
is not from a new conection and has something sent.

memset(RecvBuff,
0,sizeof(RecvBuff));
RecvBytes =
recv(SocketArray[i], RecvBuff, sizeof(RecvBuff)-1, 0);

if(RecvBytes > 0) // Some data
received.
{

printf("Message Send.
\n");
printf("Message was: %s
\n",RecvBuff);

for(k=0;k<=index-1;k+
+)
{

if(FD_ISSET(SocketArray[k],&BackUpfdwrite))
{

if(SocketArray[k] != ListeningSocket && SocketArray[k] !=
SocketArray[i])
{

memset(SentBuff,0,sizeof(SentBuff));

strcpy(SentBuff,RecvBuff);

SentBytes = send(SocketArray[k],SentBuff,sizeof(SentBuff),0);

}

}
}

}

}

}
}


}// Main loop ends here


}
[/code]

Client:

[code]
#include <WinSock2.h>
#include <stdio.h>
#include <time.h>



main()
{

SOCKET ConnectSocket;
SOCKET SocketArray[20];

SOCKADDR_IN ServerAddr;

WSADATA wsaData;

FD_SET fdwrite;
FD_SET fdread;
FD_SET BackUpfdread;
FD_SET BackUpfdwrite;

char server_address[20] = "192.168.1.4";
char SentBuff[500];
char RecvBuff[500];

const unsigned short PORT = 4444;

int maxDescriptor;
int index = 0;
int SelectResults;
int i;
int RecvBytes;
int SentBytes;

struct timeval timeout;


// Initialize Winsock 2.2
WSAStartup(MAKEWORD(2,2),&wsaData);

// Initialize ServerAddr
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(server_address);
ServerAddr.sin_port = htons(PORT);

// Create a new socket to make a client connection.
ConnectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

// Clear the fd sets
FD_ZERO(&fdread);
FD_ZERO(&BackUpfdread);
FD_ZERO(&fdwrite);
FD_ZERO(&BackUpfdwrite);

// Asign ConnectSocket into fdread and fdwrite.
FD_SET(ConnectSocket,&fdread);
FD_SET(ConnectSocket,&fdwrite);

// Set timer
timeout.tv_sec = 0;
timeout.tv_usec = 0;

maxDescriptor = ConnectSocket;
SocketArray[index] = ConnectSocket;
index++;

// Make a connection to the server with socket s.
if(connect(ConnectSocket, (SOCKADDR *) &ServerAddr,
sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Couldn't connect to the server\n");
}

// Main loop starts here
for(; ;)
{
BackUpfdread = fdread;
BackUpfdwrite = fdwrite;

memset(SentBuff, 0, sizeof(SentBuff));
printf("Write: ");
gets_s(SentBuff, sizeof(SentBuff));

SelectResults = select(maxDescriptor
+1,&BackUpfdread,&BackUpfdwrite,NULL,&timeout);

for(i=0;i<=index-1;i++)
{
// Something to read from server.
if(FD_ISSET(SocketArray[i],&BackUpfdread) &&
SocketArray[i] == ConnectSocket)
{
RecvBytes = recv(SocketArray[i],
RecvBuff, sizeof(RecvBuff), 0);

if(RecvBytes > 0)
{
printf("%s\n",RecvBuff);
// Cleaning the Receive
Buffer
memset(RecvBuff,
0,sizeof(RecvBuff));
}

}

// Something to write.
if(FD_ISSET(SocketArray[i],&BackUpfdwrite) &&
SocketArray[i] == ConnectSocket)
{
SentBytes = send(SocketArray[i],
SentBuff,sizeof(SentBuff),0);
// Cleaning the Sent Buffer
memset(SentBuff,0,sizeof(SentBuff));

}


}

} // Main menu ends here

}
[/code]

Can't understand why this delay occur.

Any suggestion will be welcomed.

Thanks.

David Gravereaux

unread,
Dec 27, 2011, 1:56:26 PM12/27/11
to
On 12/27/2011 04:20 AM, F.N wrote:
> Can't understand why this delay occur.

A socket that becomes writable is connected

--


signature.asc

F.N

unread,
Dec 28, 2011, 4:56:41 AM12/28/11
to
>  signature.asc
> < 1KViewDownload



I thought that you talked about client and you said that the problem
is that i put the ConnectSocket into fdwrite before connect(), but if
i change it and write this line FD_SET(ConnectSocket,&fdwrite); after
connect() nothing is changing. So..i didn't understand exactly what
you said. Could you be more specific? And also which is the way to fix
it (if there is one) ? To create one more socket except the
ConnectSocket ?

Thank you once again.
Reply all
Reply to author
Forward
0 new messages