Hi all,
Am coding a Chat program in C (WIN32), where i need to communicate between the client and server on two different machines. The client will specify the IP address and port number of the server to establish the connection. I need the client to specify these parameters in the command prompt.
Am posting the programs here.
SERVER
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<sys/types.h>
//#include<winsock.h>
//#include<windef.h>
#include<afxsock.h>
#include<winsock.h>
#include<afx.h>
#include<string.h>
#define PROTOCOLPORT 5500
int main(int argc,char *argv[])
{
WORD VersionRequested;
WSADATA wsaData;
int err;
VersionRequested=MAKEWORD(2,2);
err = WSAStartup( VersionRequested, &wsaData );
if(err != 0)
{
return;
}
struct sockaddr_in serveraddr, clientaddr;
struct in_addr address;
struct protoent *ptrp;
char rec[100];
char sen[100];
int port;
int socketid,bindid,clientid,connectid,sock_len;
memset((char*)&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
printf("\n Enter the PORT number:");
if(argc=1)
{
port=atoi(argv[1]);
}
else
{
port=PROTOCOLPORT;
}
if(port>0)
{
serveraddr.sin_port=htons(port);
}
else
{
printf("\n Bad port number: %s",argv[1]);
}
printf("\n Entering socket creation");
ptrp = getprotobyname("tcp");
socketid=socket(AF_INET,SOCK_STREAM,ptrp->p_proto);
if(socketid<0)
printf("error in socket creation %d\n",GetLastError());
else
printf("\n socketid is %d",socketid);
if(bind(socketid, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) ==
SOCKET_ERROR)
printf("\n error in binding %d",WSAGetLastError());
else
printf("\n binding is complete.");
listen(socketid,5);
printf("\n Waiting for active sockets");
while(1)
{
clientid=sizeof(clientaddr);
connectid=accept(socketid,(struct sockaddr*)&clientaddr,&clientid);
if(connectid<0)
{
printf("\n Error in connection ");
exit(1);
}
else
{
printf("\n connection successful");
break;
}
}
address.s_addr=clientaddr.sin_addr.s_addr;
printf("\n connected to :%d",inet_ntoa(address));
rec[0]='\0';
sen[0]='\0';
for(;;)
{
u_long imode=1;
if((ioctlsocket(socketid,FIONBIO,&imode))!=0)
printf("/n Non-Blocking mode could not be enabled, %d",WSAGetLastError());
fflush(stdin);
sock_len=sizeof(clientaddr);
recvfrom(connectid,rec,sizeof(rec),0,(struct
sockaddr*)&clientaddr,&sock_len);
printf("\n Message from the client is: %s",rec);
if(strcmp(rec,"quit")==0)
{
printf("\n End of communication");
closesocket(socketid);
break;
}
else
{
printf("\n Enter the data to be sent:");
gets(sen);
printf("%d",errno=sendto(connectid,sen,sizeof(sen),0,(struct
sockaddr*)&clientaddr,sizeof(clientaddr)));
}
}
exit(1);
printf("\n U have exited from the chat");
closesocket(socketid);
}
CLIENT:
Code:
#include<stdio.h>
#include<conio.h>
//#include<winsock.h>
#include<sys/types.h>
#include<afxsock.h>
//#include<winsock.h>
#include<afx.h>
#include<string.h>
//#include<ws2tcpip.h>
#define PROTOCOLPORT 5500
int main(int argc,char *argv[])
{
WORD VersionRequested;
WSADATA wsaData;
int err;
VersionRequested=MAKEWORD(2,2);
err = WSAStartup( VersionRequested, &wsaData );
if(err != 0)
{
return(0);
}
int socketid,connectid,sock_len,serverid,port;
struct sockaddr_in serveraddr,clientaddr;
char rec[100];
char *host;
char sen[100];
struct protoent *pport;
struct hostent *phost;
char localhost[]="localhost";
memset((char*)&clientaddr,0,sizeof(clientaddr));
serveraddr.sin_family=AF_INET;
printf("\n Enter the IP Adderess of the Server and port number to connect");
if(argc!=2)
{
printf("Executable format is:<IP ADDR> <PORT NO>");
}
if(argc>2)
{
port=atoi(argv[2]);
}
else
{
port=PROTOCOLPORT;
}
if(port>0)
{
serveraddr.sin_port=htons(port);
}
else
{
printf("\n Bad port number: %s",argv[2]);
}
if(argc>1)
{
host=argv[1];
}
else
{
host=localhost;
}
phost=gethostbyname(host);
if(phost==NULL)
{
printf("\n Invalid host %s",argv[1]);
}
memcpy(&serveraddr.sin_addr,phost->h_addr,phost->h_length);
pport=getprotobyname("tcp") ;
socketid=socket(AF_INET,SOCK_STREAM,pport->p_proto);
if(socketid<0)
{
printf("\n Error in socket creation %d",GetLastError());
exit(1);
}
printf("\n socket created");
printf("\n socket Id:%d",socketid);
connectid=connect(socketid,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr));
if(connectid<0)
{
printf("\n error in creating connection");
exit(1);
}
printf("\n connection successful\n");
rec[0]='\0';
sen[0]='\0';
for(int i=0;i<=7;i++)
{
u_long imode=1;
if((ioctlsocket(socketid,FIONBIO,&imode))!=0)
printf("/n Non-Blocking mode could not be enabled, %d",WSAGetLastError());
fflush(stdout);
printf("\n Enter the data to be sent:");
gets(sen);
sendto(socketid,sen,sizeof(sen),0,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
sock_len=sizeof(serveraddr);
recvfrom(socketid,rec,sizeof(rec),0,(struct sockaddr*)&serveraddr,&sock_len);
if(strcmp(rec,"quit")==0)
{
printf("\n End of communication");
closesocket(socketid);
break;
}
else
printf("\n Message from the server is: %s",rec);
}
exit(1);
printf("\n U have exited from the chat");
closesocket(socketid);
}
PROBLEM:
Am not able to input the port number in the server. Am not able to input the IP ADDR and PORT no in the Client. The server and the client just proceed with the next functionality in the program without waiting for an input from the command line. I tried executing the pgms from the command prompt by specifying the input as arguments but to no avail. Also tried out specifying the parameters in PROJECT>SETTINGS>DEBUG>PARAMETER LIST.
What should be done? Where am I going wrong? Any good suggestion would be of great help.
Thanks in advance,
Sidhuram.