first program [Archive] - C Board

PDA

View Full Version : first program


pode
09-14-2003, 03:59 PM
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


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;

}

XSquared
09-14-2003, 04:01 PM
Have a look at Beej's Guide (http://www.ecst.csuchico.edu/~beej/guide/net/html/).

pode
09-14-2003, 04:14 PM
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?

Hammer
09-14-2003, 04:26 PM
Is your code (above) for a client or a server? What exactly are you trying to build again?