mere winsock
her min kode:#include "stdafx.h"
#include <winsock.h>
#include <iostream.h>
#include <string.h>
#include <windows.h>
#define PORTNUM 3950 /* random port number, we need something */
SOCKET establish(unsigned short portnum);
void do_something(SOCKET);
void main() {
SOCKET s;
if ((s = establish(PORTNUM)) == INVALID_SOCKET)
{ /* plug in the phone */
perror("establish");
cout << "establish" << endl;
Sleep(3000);
exit(1);
}
for (;;) { /* loop for phone calls */
SOCKET new_sock = accept(s, NULL, NULL);
if (s == INVALID_SOCKET) {
cout << "Error waiting for new connection!\n" << endl;
Sleep(3000);
exit(1);
}
do_something(new_sock);
Sleep(3000);
closesocket(new_sock);
}
}
/* this is the function that plays with the socket. it will be called * after getting a connection. */
void do_something(SOCKET s)
{
cout << "got connection" << endl;
}
SOCKET establish(unsigned short portnum) {
char myname[256];
SOCKET s;
struct sockaddr_in sa;
struct hostent *hp;
memset(&sa, 0, sizeof(struct sockaddr_in));
/* clear our address */
gethostname(myname, sizeof(myname)); /* who are we? */
hp = gethostbyname(myname); /* get our address info */
cout << hp << endl;
Sleep(3000);
if (hp == NULL) /* we don't exist !? */
return(INVALID_SOCKET);
sa.sin_family = hp->h_addrtype; /* this is our host address */
sa.sin_port = htons(portnum); /* this is our port number */
s = socket(AF_INET, SOCK_STREAM, 0); /* create the socket */
if (s == INVALID_SOCKET)
return INVALID_SOCKET; /* bind the socket to the internet address */
if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
closesocket(s); return(INVALID_SOCKET);
}
listen(s, 3); /* max # of queued connects */
return(s);
}
men når jeg køre koden får jeg bare hp=0X000000 og denne perror("establish"); giver No Error men alligevel kommer den ikke længer end perror("establish");
nogle der kan hjælp?