LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-06-2010, 01:37 AM   #1
sidhuram
LQ Newbie
 
Registered: Aug 2010
Posts: 23

Rep: Reputation: 7
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.
 
Old 09-06-2010, 02:03 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Seems a bit strange asking Windows coding questions in a linux forum but anyway .. from my 2 second review:

# beginner mistake no. 1 - using assignment instead of testing for equality
Code:
if(argc=1)
You also appear to be attempting to ask for input but then checking which arguments were passed on the command line not reading input ... ??

After checking the port number you don't exit after failure:
Code:
printf("\n Bad port number: %s",argv[1]);
// maybe an exit call here ?
Usually a good idea to perform all your argument checking / user prompting right at the start before you do anything else

good luck
 
Old 09-06-2010, 02:17 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

as kbp already pointed out, your problem is that you do not know what argc and argv actually are. Suppose you have a program called prog. when you call it like
Code:
prog arg1 arg2
then arg1 and arg2 will be accessible through argv. argc will be 2 since it holds the number of arguments with which the program was started.
What you are looking for is something like
Code:
int portnumber;
printf("\n Enter port number: ");
scanf("%d", &portnumber);
...
scanf() will read the input and then store it in portnumber.
Hope this clears things up a bit.
 
Old 09-06-2010, 04:01 AM   #4
sidhuram
LQ Newbie
 
Registered: Aug 2010
Posts: 23

Original Poster
Rep: Reputation: 7
@KBP:
I posted the programs from another computer which has internet access. Copied the old pgm and did the editing work here. Thats the reason for MISTAKE NO 1.
Thanks anyway.
 
Old 09-06-2010, 04:03 AM   #5
sidhuram
LQ Newbie
 
Registered: Aug 2010
Posts: 23

Original Poster
Rep: Reputation: 7
@CRTS:
Quote:
your problem is that you do not know what argc and argv actually are.
Thats correct. LL go through the stuff again and get back tomo. Thanks a lot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line arguments bioinformatics_guy Linux - Newbie 2 02-12-2009 07:26 AM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 02:25 AM


All times are GMT -5. The time now is 08:01 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration