int Initialize(int argc, char *argv[]) { char StrIP[30]; // StrIP will hold the
ID: 3634974 • Letter: I
Question
int Initialize(int argc, char *argv[]){
char StrIP[30]; // StrIP will hold the ip if the
// user don't give an argument.
try
{
WSockClient MyClient(REQ_WINSOCK_VER); // Winsock startup.
cout << "Search server... " << endl;
if(argc >= 2) // If user give an argument (IP ADDRESS)
{
cout << argv[1] << endl;
if(!MyClient.ConnectServer(PORT, argv[1]))
{
cout << "ChatClient was unable to connect. " << endl;
}
}
else
{
cout << "IP ADRESS: ";
cin.getline(StrIP, 29, ' ');
if(!MyClient.ConnectServer(PORT, StrIP))
{
cout << "ChatClient was unable to connect. " << endl;
}
}
cout << "You are connected to the server. " << endl << endl;
cout << "Type 'exit' to disconnect. " << endl;
MyClient.StartChat();
}
catch(char *ErrMsg) { cout << " Error: " << ErrMsg << endl; }
return 0;
}
Explanation / Answer
Dear, int Initialize(int argc, char *argv[]) Here argc and argv are arguments to be passed to the program when the program is running through commamd prompt. where argc is total number of arguments and argv[] represents array of char arguments which are passed to the program via command prompt. for this program you need to pass program name and IP address of the server to which you want to connect. IP Address is in the form of 4 bytes. 0-255.0-255.0-255.0-255. if argc=2 ,that means argv[1]=program name argv[2]=IP address If the IP address is valid then it will connect to the server of the listening PORT number of chat application. if server and client both are same then the IPAdress 127.0.0.1 will become your IPAddress . Different application will user different port numbers,for HTTP THERE is port 80 and SMPT port is 25 etc... Hope this would helpful to you,.,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.