View Single Post
Old Apr 25th, 2005, 9:41 PM   #7
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Talking

Geez man, thanks again! Works like a charm! Here's the working code (soon, I will have my first instant messenger together ) And notice how there is no errors at the bottom Thanks Eggbert!
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winsock.h>

using namespace std;

int main(){
    system("title SweetTalk");
    char buffer[30];
    cout << "SweetTalk will now start up..." << endl;
    cout << "What is the IP address you would like to connect to?: " << endl;
    cin.getline(buffer,30);
    WORD sockversion;
    WSADATA wsadata;
    int ret;
    sockversion = MAKEWORD(1, 1);	
    WSAStartup(sockversion, &wsadata);
    LPHOSTENT host;
    in_addr eHost;
    eHost.s_addr = inet_addr(buffer);
    host = gethostbyaddr((const char*)&eHost, sizeof(struct in_addr), AF_INET);
    if (!host)
    {
              cout << "You specified a wrong addrress, SweetTalk will now exit!" << endl;
              cout << "Press any key to exit" << endl;
              getch();
              WSACleanup();
              return EXIT_SUCCESS;
    }
    SOCKET socketB;
    socketB = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (socketB == INVALID_SOCKET)
    {
                cout << "Failed to create socket, press any key to exit" << endl;
                WSACleanup();
                getch();
                return EXIT_SUCCESS;
    }
    SOCKADDR_IN sInfo;
    sInfo.sin_family = AF_INET;
    sInfo.sin_addr = *((LPIN_ADDR)*host->h_addr_list);
    sInfo.sin_port = htons(80);
    ret = connect(socketB, (LPSOCKADDR)&sInfo, sizeof(struct sockaddr));
    if (ret == SOCKET_ERROR)
    {
            cout << "Failed to connect, press any key to exit!" << endl;
            WSACleanup();
            getch();
    }
    
    closesocket(socketB);
    WSACleanup();
    return 0;
}
layer is offline   Reply With Quote