Thread: first program

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    first program

    this is my first attempt on writing a winsock program
    its pretty cool i always thought it was gonna be harder but it seems really easy

    anyway its only a simple "hello world" program
    my problem is i cant figure out a way to connect to a user
    actually STAY connected with telnet when i run it.
    i want to use connect() to establish a connection with the user
    on telnet.

    ive seen somewhere they used something like LPHOSTENT host
    then gethostbyname("cprogramming.com")

    but my problem is i want to connect to address instead of doing that
    here is the code
    Code:
    int main()
    {	
        int ok;
    	WORD sockVersion;
    	WSADATA wsaData;
       	sockVersion = MAKEWORD(2, 0);	
        WSAStartup(sockVersion, &wsaData);
    	
       SOCKET sock,usersocket, consock;
    
       sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    
       LPHOSTENT host;
       
       struct sockaddr_in insocket;
    
       insocket.sin_family=AF_INET;
       insocket.sin_port=htons(7777);
       insocket.sin_addr.s_addr=INADDR_ANY; 
       memset(&(insocket.sin_zero), '\0', 8);
    
       int bin=bind(sock,(struct sockaddr*)&insocket,sizeof(struct sockaddr));
       
    
    if(bin==SOCKET_ERROR)
    {
    	cout<<"ERROR";
    }
    
    listen(sock,10);
    
    int size=sizeof(sockaddr);
    usersocket=accept(sock,(struct sockaddr*)&insocket,&size);
    
    if(usersocket==INVALID_SOCKET)
    {
    	cout<<"error";
    }
    
    
    else
    {
    
    char *text="pode was here";
    int len;
    len=strlen(text);
    int sen=send(usersocket,text,len,0);
    
    if(sen==SOCKET_ERROR)
    {
    	cout<<sen<<"error";
    }
    else 
    {
    
     ok=connect(consock,(struct sockaddr *)&insocket,sizeof(struct sockaddr));  // here is were my problem is
    
    if(ok==SOCKET_ERROR)
    {
    	cout<<"error";
    }
    
    }
    
    }
    closesocket(usersocket);
    closesocket(sock);
    closesocket(consock);
    	return 0;
    
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Have a look at Beej's Guide.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i looked around a bit
    i got a feeling that im totally in the wrong way

    Is it even possible to connect to telnet?
    or even request something?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is your code (above) for a client or a server? What exactly are you trying to build again?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM