help regardin c header file..

:mad:
looks like the guys in HCL don't wanna do on a unix platform..
they want me to stick to windows :mad:
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 :(
 
compile it using vc++ (visual studio 6.0 works fine)

in the server program, there is just one error. replace the winsock.h with winsock2.h. The client works just fine :)
 
:S
i need to convert these 2 unix codes to vc++ base..
can someone help me out wid this :( :( ? :huh: :sos::cry:
client.c

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h> /* close */

#define SRV_IP_ADRS "192.168.1.2"
#define SRV_TCP_PORT 27000
#define MAX_MSG 100

void errExit(char *str)
{
puts(str);
exit(0);
}

int main()
{
int sockFd;
struct sockaddr_in srvAdr;
char txmsg[MAX_MSG];
char rxmsg[MAX_MSG];
int n;

if((sockFd = socket(AF_INET,SOCK_STREAM,0))<0)
errExit(" Can't open socket ");
memset(&srvAdr,0,sizeof(srvAdr));
srvAdr.sin_family = AF_INET;
srvAdr.sin_addr.s_addr = inet_addr(SRV_IP_ADRS);
srvAdr.sin_port = htons(SRV_TCP_PORT);

if(connect(sockFd, (struct sockaddr *) &srvAdr, sizeof(srvAdr))<0)
errExit(" Can't connect to server");
while(1)
{
printf(" Enter the message to send , Enter # to exit : \n");
fgets(txmsg,MAX_MSG,stdin);
if(txmsg[0]=='#')
break;

n = strlen(txmsg)+1;
if(send(sockFd,txmsg,n,0) != n)
errExit(" Send Error \n ");

n=recv(sockFd,rxmsg,MAX_MSG,0);
if(n<0)
errExit(" Receive Error \n");

printf(" Server Received the following message : \n %s \n",rxmsg);
}
close(sockFd);
return(0);
}
server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

#define SRV_TCP_PORT 27000 //port on which the server will be listening
#define MAX_MSG 100

void errExit(char *str)
{
puts(str);
exit(0);
}

int main()
{

int sockfd, newsockfd;
struct sockaddr_in cliadr, srvadr;
int clilen, n;
char mesg[MAX_MSG];

/*initialise the socket library*/

if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
errExit(" Can't open stream socket ");
/* bind server port */
memset(&srvadr,0,sizeof(srvadr));
srvadr.sin_family = AF_INET;
//allows you to recieve packets irrespective of ip Adress
srvadr.sin_addr.s_addr = htonl(INADDR_ANY);
//this is the port on which socket will be listening for connections
srvadr.sin_port = htons(SRV_TCP_PORT);

/*The bind() function associates a local address with a socket*/
if(bind(sockfd, (struct sockaddr*) &srvadr, sizeof(srvadr))<0)
errExit(" Can't bind local address");

//listen for connections
listen(sockfd,5);

while(1)
{
printf(" Server waiting for new connection : \n");

//if a client tries to connect on the port the server accepts the connection
//clilen = sizeof(cliaddr);
newsockfd = accept(sockfd, (struct sockaddr*) &cliadr, &clilen);
if(newsockfd<0)
errExit(" Accept Error \n");
printf("Connected to the client \n");

/* receive segments */
while(1)
{
//recieve the message sent by the client
n = recv(newsockfd,mesg,MAX_MSG,0);
if(n<0)
errExit(" Receive Error \n");
if(n==0)
{
close(newsockfd);
break;
}
//send a message same message to the client
if(send(newsockfd,mesg,n,0) != n)
errExit(" Send Error \n");
printf(" Server Received and sent the following message to the Client: \n %s \n", mesg);
} /* while(read_line) */
} /* while(1) */
return 0;
}

well.. these are basically to communicate btw client n server..
:| :help:
 
actually.. they compiled without errors but..

since this code works 100% in unix.. the guys asked me to just do the same thing for windows..

the winsock codes.. whn i run the exe s .. im not gettin a proper o/p :(
 
a small development to my objective :S

how do i serialize/deserialize in vc++ have to add them to my client+server..

can anyone help me out.. ??
 
since im just a beginner..

can someone please please please please please guide me

im really in need of help..

this opportunity to do a project in hcl was such a huge gud luck for me

and i dont want to go screw it up

please.. i need some help :(
 
Back
Top