winsock in c++

Programming, for all ages and all languages.
Locked
brokenbylaw
Posts: 19
Joined: Fri Apr 03, 2009 3:15 pm

winsock in c++

Post by brokenbylaw »

Ok i have this code that allows a telnet client to connect to it through whom ever's ip its running on
what im trying to do is get the client (telnet) user to input a password and have the server check if that is correct and respond to the user if so or if not
heres the code

Code: Select all

#include <winsock2.h>
#include <conio.h>
#include <iostream>


using namespace std;

UINT ServerThread(LPVOID);

int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
   int nRetCode = 0; 

   cout << "Press ESCAPE to terminate program\r\n";
   switch (ServerThread(0)) {
case 1: 
        cout << "WSAStartup failed!" << endl;
        nRetCode = 2;
   case 2:
        cout << "INVALID_PORT thrown." << endl;
        nRetCode = 2;     
   case 3:
        cout << "Cannot Bind to Socket." << endl;
        nRetCode = 2;     
   case 4:
        cout << "Can't start Listener." << endl;
        nRetCode = 2;          
        }
   return nRetCode;
}

UINT ServerThread(LPVOID pParam)
{ 
   cout << "Starting up TCP server\r\n";


   SOCKET server;


   WSADATA wsaData;


   sockaddr_in local;


   int wsaret=WSAStartup(0x101,&wsaData);

 
   if(wsaret!=0)
   {
       return 1;
   }

  
   local.sin_family=AF_INET; 
   local.sin_addr.s_addr=INADDR_ANY; 
   local.sin_port=htons((u_short)3000); 

   
   server=socket(AF_INET,SOCK_STREAM,0);

   
   if(server==INVALID_SOCKET)
   {
       return 2;
   }

   
   if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
   {
       return 3;
   }

   
   if(listen(server,10)!=0)
   {
       return 4;
   }

   
   SOCKET client;
   sockaddr_in from;
   int fromlen=sizeof(from);

   while(true)
   {
       char temp[512];

       
       client=accept(server,
           (struct sockaddr*)&from,&fromlen);
 
       sprintf(temp,"welcome to my console\n");
      login:
        
       
                     
       send(client,temp,strlen(temp),0);
       cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
       cout << "connection opened\n";

       

   }

  
   cout << "closing socket";
   closesocket(server);

  
   WSACleanup();

   return 0;
}

im not really sure how to get the input from the user
i tryed scanf() but to no avail it didnt work
and getline(cin,pass); doesnt work eithor
any help would be much apreciated
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: winsock in c++

Post by Combuster »

Yet Another RTFM for you, my friend.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Locked