looks like the guys in HCL don't wanna do on a unix platform..
they want me to stick to windows
i ve got 2 new codes for client n server..
postin them below..
server...
#include <winsock.h>
#include <iostream.h>
#define PORT 1250
#define BUFFERSIZE 8192
typedef struct _MYSOCKET_INFORMATION {
CHAR Buffer[BUFFERSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD SendBytes;
DWORD RecvBytes;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Index);
DWORD TotalSockets = 0;
LPSOCKET_INFORMATION SocketList[FD_SETSIZE];
void main(void)
{
//some of the basic declarations required for this winsock tutorial
SOCKET ListenSocket;
SOCKET AcceptSocket;
SOCKADDR_IN InternetAddr;
WSADATA wsaData;
FD_SET Writer;
FD_SET Reader;
ULONG NonBlock;
DWORD Flags;
DWORD Total;
DWORD Ret;
DWORD i;
DWORD SendBytes;
DWORD RecvBytes;
if ((Ret = WSAStartup(MAKEWORD(2,0),&wsaData)) != 0)
{
cout<<"Start up failed";
}
// Create a socket for the winsock tutorial.
if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
cout<<"Winsock tutorial error: WSASocket() failed \n"<< WSAGetLastError();
return;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(ListenSocket, (SOCKADDR *) &InternetAddr, sizeof(InternetAddr))
== SOCKET_ERROR)
{
cout<<"Winsock tutorial error: Binding failed \n"<<WSAGetLastError();
return;
}
if (listen(ListenSocket, 5))
{
cout<<"Winsock tutorial error: listen failed \n"<< WSAGetLastError();
return;
}
// Change the socket mode on the listening socket from blocking to non-block
NonBlock = 1;
if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"ioctlsocket() failed \n";
return;
}
while(TRUE)
{
// Initialize the Read and Write socket set.
FD_ZERO(&Reader);
FD_ZERO(&Writer);
// Check for connection attempts.
FD_SET(ListenSocket, &Reader);
// Set Read and Write notification for each socket based on the
// current state the buffer.
for (i = 0; i < TotalSockets; i++)
if (SocketList->RecvBytes > SocketList->SendBytes)
FD_SET(SocketList->Socket, &Writer);
else
FD_SET(SocketList->Socket, &Reader);
if ((Total = select(0, &Reader, &Writer, NULL, NULL)) == SOCKET_ERROR)
{
cout<<"Winsock tutorial error: select function returned with error \n"<< WSAGetLastError();
return;
}
// Check for arriving connections on the listening socket.
if (FD_ISSET(ListenSocket, &Reader))
{
Total--;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET)
{
// Set the accepted socket to non-blocking mode so the server will
// not get caught in a blocked condition on WSASends
NonBlock = 1;
if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
cout<<"Winsock tutorial error: ioctlsocket() failed with error \n", WSAGetLastError();
return;
}
if (CreateSocketInformation(AcceptSocket) == FALSE)
return;
}
else
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"accept() failed with error %d\n", WSAGetLastError();
return;
}
}
}
// Check each socket for Read and Write notification for Total number of sockets
for ( i = 0; Total > 0 && i < TotalSockets; i++)
{
LPSOCKET_INFORMATION SocketInfo = SocketList;
// If the Reader is marked for this socket then this means data
// is available to be read on the socket.
if (FD_ISSET(SocketInfo->Socket, &Reader))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = BUFFERSIZE;
Flags = 0;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"Winsock tutorial: Receive failed with error\n";
FreeSocketInformation(i);
}
continue;
}
else
{
SocketInfo->RecvBytes = RecvBytes;
cout<<SocketInfo->DataBuf.buf<<"\n";
// If zero bytes are received, this indicates connection is closed.
if (RecvBytes == 0)
{
FreeSocketInformation(i);
continue;
}
}
}
// If the Writer is marked on this socket then this means the internal
// data buffers are available for more data.
if (FD_ISSET(SocketInfo->Socket, &Writer))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes;
SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes;
if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
cout<<"Send failed with error\n";
FreeSocketInformation(i);
}
continue;
}
else
{
SocketInfo->SendBytes += SendBytes;
if (SocketInfo->SendBytes == SocketInfo->RecvBytes)
{
SocketInfo->SendBytes = 0;
SocketInfo->RecvBytes = 0;
}
}
}
}
}
}
BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;
cout<<"Accepted socket\n";
if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
cout<<"Winsock tutorial error: GlobalAlloc() failed\n";
return FALSE;
}
// Prepare SocketInfo structure for use.
SI->Socket = s;
SI->SendBytes = 0;
SI->RecvBytes = 0;
SocketList[TotalSockets] = SI;
TotalSockets++;
return(TRUE);
}
void FreeSocketInformation(DWORD Index)
{
LPSOCKET_INFORMATION SI = SocketList[Index];
DWORD i;
closesocket(SI->Socket);
cout<<"Closing socket\n";
GlobalFree(SI);
// Remove from the socket array
for (i = Index; i < TotalSockets; i++)
{
SocketList = SocketList[i + 1];
}
TotalSockets--;
}
and the client..
#include <winsock.h>
#include <iostream.h>
int gPort = 1150;
void main()
{
SOCKET lhSocket;
SOCKADDR_IN lSockAddr;
WSADATA wsaData;
int lConnect;
int lLength;
char lData[]="SendData";
if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
cout<<"Socket Initialization Error. Program aborted\n";
return;
}
lhSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(lhSocket == INVALID_SOCKET)
{
cout<<"Invalid Socket "<<GetLastError()<<". Program Aborted\n"<<endl;
}
memset(&lSockAddr,0, sizeof(lSockAddr));
lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(gPort);
lSockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
lConnect = connect(lhSocket,(SOCKADDR *)&lSockAddr,sizeof(SOCKADDR_IN));
if(lConnect != 0)
{
cout<<"Connect Error. Program aborted\n";
return;
}
lLength = send(lhSocket,lData,strlen(lData),0);
if(lLength < strlen(lData))
{
cout<<"Send Error.\n";
}
closesocket(lhSocket);
return;
}
i need help
ive to compile this thingy n submit tomorrow :S
since im a nooblet i dont know how to even use visual studio..
can someone help me out wid how to go abt with this
and..
is it possible that i compile this in devc++ ?? :|
please help..
m in need of help