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;
}