All pastes #553513 Raw Edit

Stuff

public text v1 · immutable
·published 2007-06-09 19:12 UTC
rendered paste body
//-----------------------Includes
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <iostream>
//-----------------------

#define MAXDATASIZE 100

//-----------------------MISC
#pragma comment(lib, "wsock32")
using namespace std;
//-----------------------

int main()
{
    SOCKET sock,client; //our sockets
	int numbytes;
	char buf[MAXDATASIZE];
    int size, ret, z; //size and return value integers
    SOCKADDR_IN addr,k; //our address to specify in <- we can get IP information from this struct
    WSAData dat; //winsock data
    WORD ver; // our version to use
    ver = MAKEWORD(2,0); //use winsock version 2.0
    int i = WSAStartup(ver,&dat);
    if (i!=0) // did we error
    {
        cout << "We fucked it up" << endl;
        WSACleanup(); //clean our asses up
        return 0; //bye bye
    }
    sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    /*defined as: int socket(int af, int type, int proto);
    All of the parameters are integers because winsock
    already has the specific types of paremeters defined
    as integers.All exceptable types of the parameters
    will be listed below in the tables // // //
    we use the Internet address family
    the Streaming type of socket
    and the TCP/IP protocol as opposed to the UDP protocol
    UDP = user datagram protocol
    */
    addr.sin_addr.s_addr = INADDR_ANY; // here we will allow any incoming connections
    addr.sin_family = AF_INET; //once more our Internet af
    addr.sin_port = htons(8200); // htons is a winsock function to convert a short int to a port struct, either way 8200 is our port
    // there is another member of the struct present, and that is 'zero'. It is actaully unused in most all cases, including this one
    size = sizeof(addr); // the size of our addr struct
    ret = bind(sock,(SOCKADDR*)&addr,size); //bind our socket to port
    if (ret==SOCKET_ERROR)
    {
        cout << "Error Binding Socket to Port" << endl;
        WSACleanup(); //clean up winsock
        return 0; // die with the error
    }
    // if we got here than we succesfully binded and initialized our basic server
    // now lets begin our listening routine
    ret = listen(sock,10);
    // defined as: int listen(SOCKET s, int backlog);
    /* a little bit more to explain, not much though
     listen function begins the listening on our binded socket -sock -
     it will backlog 10 connections simulaineously
     what this means is that when somebody connects to our
     server, then it will keep waiting for another connection
     but it wont accept it until after our server is finished
     dealing with the first connection. The backlog serves as how many
     people you will keep waiting to connect to the server. After the number
     10 has been reached then it will start kicking off connection requests
     to the attempting clients.
     This method will only accept one connection at a time and is good in dealing
     with single user tasks, such as remote desktop management (such as in VCN and
     other RAT's), and other applications of the sort. What it isnt good for is
     being a full fledged server. This is becuase a self-contained server must
     accept a large quantity of connections at a single time, so it must multi-task.
     That will be covered in the next part of the winsock tutorial.
     */
    z = sizeof(k);
    while (1)
    { //easily done with for(;;) or an infinite jump command as well
        client = accept(sock,(SOCKADDR*)&k,&z);
		cout << "Connected" << endl;
		
		char msg[100];
			
			cout << "Send: "; //Ask for the sent message string
			cin >> msg;
			int len, bytes_sent; //length, and number of bytes sent
			len = strlen(msg); //length of string

			bytes_sent = send(client, msg, len, 0);
			if ((numbytes=recv(client, buf, MAXDATASIZE-1, 0)) == -1)
			{
				cout << "Error Recieving" << endl;
			}


			buf[numbytes] = '\0'; //The Buffer Variable
     		printf("Received: %s",buf);

        /* We will now accept our connections
        Notice we put the server on an infinite loop.
        This will come into play, more, in latter versions
        and more full fledged servers. As of now, it really
        doesnt matter, unless you are always trying to listen
        for a single connection no matter who it is.
        we are accepting on our listening socket (sock) and putting
        all connection information into SOCKADDR_IN k value.
        The k struct will allow us to obtain specific information about
        the client such as the IP address (mainly it is just the IP address
        but other functions in the winsock library will allow us to get
        more info from this struct).
        Our new socket is called client, and it contains the important info
        about the client, minus the IP.
        */

        /* Now that we have accepted the connection we will do our functions
        so (functions here) then we will exit)
        */
        closesocket(client);
        // this closes our client socket
        closesocket(sock);
        return 0;
        // this closes our listening socket
        // we are not totally disabled
        // and our server function has ended
        // how was that for a mucky first server?
    }
    //THIS POINT IS NEVER REACHED
}