Command Line arguments

This is a discussion on Command Line arguments within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, Am coding a Chat program in C (WIN32), where i need to communicate between the client and server ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Command Line arguments

    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.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    Command line arguments are passed in when you run your program, not prompted for at runtime:

    Code:
    C:\My Projects\My Project> myprogram arg1 arg2 arg3

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Aside from the fact that your indentation sucks, there are numerous issues

    > if(argc=1)
    Perhaps you meant ==

    You use gets() and fflush(stdin)
    These are very bad form, see the FAQ for details.

    > recvfrom(connectid,rec,sizeof(rec),0,(struct
    Here is where you can really come unstuck.
    1. Why are you using recvfrom() on a TCP stream?
    2. You don't check the return result
    3. Low level functions like recv DO NOT automatically append a \0. Using such a result 'as is' for later strcmp say (which does depend on a well-placed \0) is dangerous.
    4. Just because you send "quit" does not imply that you will ALWAYS receive "quit" by itself in a single corresponding receive call. Message fragmentation on both send AND receive is a fact you have to deal with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 07:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 01:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21