locked
Ad-Hoc chat application RRS feed

  • Question

  • Hi!

    I made a MFC application which is a chat and I want to send and receive the messages in an ad-hoc network, however I need to know the name of the server and the port that it is listening to. So how could I know this information? I'd really appreciate any help, links or data!

    • Moved by Nicholas Li Tuesday, November 20, 2012 9:05 AM (From:Windows 7 Networking)
    Monday, November 19, 2012 4:30 AM

All replies

  • Hi ,

    Well to find the Host name you can use 

    hostent* remoteHost;
    	char* ip;
    	char* host_name;
    
    host_name[strlen(host_name)] = '\0'; 
    	remoteHost = gethostbyname(host_name);
    	cout<<remoteHost<<"\n";
    	ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list); 

    And to finally find the Port it is simple just show the connected port ,which can be found in  SOCKADDR_IN structure.

    If you want to use console application you can use this code:

    p.s It only shows the Host name but you can easily implement the port number you program is connected to via printing the port number on the screen:

    #include <winsock.h>
    #include <windows.h>
    #include <iostream>
    #include<stdio.h>
    #include<conio.h>
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib, "wsock32.lib")
    #define NO_FLAGS_SET 0
    #define PORT 80
    #define MAXBUFLEN 20480 
    using namespace std;
    int main(void)
    {
    loop:
    	system("color 0a");
    	WSADATA Data;
        SOCKADDR_IN recvSockAddr;
        SOCKET recvSocket;
    	int status;
    	hostent* remoteHost;
    	char* ip;
    	char* host_name;
    	status = WSAStartup(MAKEWORD(2, 2), &Data);
    	system("title CyberWarfare: Stealth Rader v.1.0 by EMPIRERIADER(Cyberwarfare)");
    	cout<<"_______________________________________________________________________________\n";
    	cout<<" ~ CyberWarfare: Stealth Radar v.1.0 by EMPIRERIADER(Cyberwarfare) ~           \n"; 
    	cout<<"_______________________________________________________________________________\n";
    	cout<<"Created by Cyberwarfare(EMPIRERAIDER)...\n\n\n";
    	cout<<"This is program is used to find IP's of the Target\nalso please do not enter invalid address as the program may crash.Please report\nany bugs to the Creator of the program EMPIRERIADER(Cyberwarfare)\n\n\n ";
    cout << "Name of website (www.google.com): \n";
    	host_name = (char*) malloc(sizeof(char*)*128);
    	cin >> host_name;
    	host_name[strlen(host_name)] = '\0'; 
    	remoteHost = gethostbyname(host_name);
    	cout<<remoteHost<<"\n";
    	ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);
    	system("cls");
    	printf("IP address is: %s\n", ip);
    	getch();
    	system("cls");
        memset(&recvSockAddr, 0, sizeof(recvSockAddr)); 
        recvSockAddr.sin_port=htons(PORT); 
        recvSockAddr.sin_addr.s_addr= inet_addr(ip);
    	recvSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
       getch();
       goto loop;
    }
    

    Sorry i am using goto loops :(

    Hope you learn something new from it.

    Sunday, January 20, 2013 8:27 PM