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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-06-2010, 01:37 AM
|
#1
|
LQ Newbie
Registered: Aug 2010
Posts: 23
Rep:
|
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.
|
|
|
09-06-2010, 02:03 AM
|
#2
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
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
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
|
|
|
09-06-2010, 02:17 AM
|
#3
|
Senior Member
Registered: Jan 2010
Posts: 2,020
|
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
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.
|
|
|
09-06-2010, 04:01 AM
|
#4
|
LQ Newbie
Registered: Aug 2010
Posts: 23
Original Poster
Rep:
|
@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.
|
|
|
09-06-2010, 04:03 AM
|
#5
|
LQ Newbie
Registered: Aug 2010
Posts: 23
Original Poster
Rep:
|
@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.
|
|
|
All times are GMT -5. The time now is 12:43 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|