HelpC++ Socket Help

Posts 1–2 of 2 · Page 1 of 1
C++ Socket Help
Basically, I tried making a small IRC client.

I'm stuck on Sockets (Oh boy)
Can someone explain in some detail how to use sockets/what they are?
I couldn't find a good tutorial online about what they are and how to use them properly(With examples)

This is what I got, error is on the connect line:
Code:
#include <winsock2.h>
  #include <iostream>
    using namespace std;
    int main(){
        const int iReqWinsockVer = 2;
        WSADATA wsaData;


        if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
        {

            if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
            {
                 struct sockaddr{
                    u_short   sa_family;
                    char      sa_date[14];
                };
                struct sockaddr_in
                {
                    short   sin_family;
                    u_short sin_port;
                    struct  in_addr sin_addr;
                    char    sin_zero[8];
                }sockAddr;
                sockAddr.sin_family = AF_INET;
                sockAddr.sin_port = htons(80);
                sockAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.114");
                SOCKET s;
                s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
                if(s != INVALID_SOCKET)
                   if(connect(s,  (&sockAddr), sizeof(sockAddr))!=0)
                    {
                        cout << "Failed";
                    }
            }
            else
            {

            }


            if (WSACleanup()!=0)
            {
                // cleanup failed
            }
        }
        else
        {
            //  startup failed
        }
}
Here is a very simple server hosted on your own pc.
Should compile if you link the library.

Code:
#include <winsock2.h>

typedef int socklen_t;

#include <stdio.h>
#include <stdlib.h>
#define PORT 90 //You can use any port



int main(void)
{
        WSADATA WSAData;
        //Starts up WSA server
        int error = WSAStartup(MAKEWORD(2,2), &WSAData);

    char buffer[32] = "";
    SOCKET sock;
    SOCKADDR_IN sin;

    if(!error )
    {
        /* Creates the socket */
        sock = socket(AF_INET, SOCK_STREAM, 0);

        /* Socket configuration */
        sin.sin_addr.s_addr = inet_addr("127.0.0.1"); //The server will be hosted on your own pc
        sin.sin_family = AF_INET; //TCP/IP
        sin.sin_port = htons(PORT); //Selected Port

        if(connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
        //if the the connection worked
            printf("Connected \n");
        else
            printf("Failed. e\n");

        while(1)
            if(recv(sock, buffer, 32, 0) != SOCKET_ERROR)
                //Displays the message received
                printf("Received : %s\n", buffer);

        closesocket(sock);

            WSACleanup();
    }

    return EXIT_SUCCESS;
}
You can easily convert it to C++
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us