#include <iostream>
#include <winsock.h>
#define PORT 5000
using namespace std;
int main ()
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 1)
{
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
int mysock = socket(AF_INET, SOCK_STREAM, 0);
int list_sock;
struct sockaddr_in sockinfo;
struct sockaddr_in theirsockinfo;
sockinfo.sin_family = AF_INET;
sockinfo.sin_port = htons(PORT);
sockinfo.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(sockinfo.sin_zero),'\0',8);
if (bind(mysock, (struct sockaddr*)&sockinfo, sizeof(struct sockaddr)) != 0)
{
fprintf (stderr, "Error binding socket.\n");
exit(1);
}
if (listen(mysock, 10) != 0)
{
fprintf(stderr, "Error listening on socket... isn't bu gay?.\n");
exit(1);
}
int sinsize;
int datasend, datalen;
char* data;
sinsize = sizeof(struct sockaddr_in);
if (list_sock = accept(mysock, (struct sockaddr*)&theirsockinfo, &sinsize != 0)
{
fprintf(stderr, "Could not accept request\n");
exit(1)
}
while(1)
{
cin<<data<<endl;
datalen = strlen(data);
if (datasend = send(list_sock, data, datalen, 0) != 0)
{
fprintf(stderr, "Could not send data\n");
exit(1);
}
if (data == "close")
{
if (socketclose(list_sock) != 0)
{
fprintf(stderr,"Error closing socket");
exit(1);
}
if (socketclose(mysock) != 0)
{
fprintf(stderr,"Error closing socket");
exit(1);
}
break;
}
}
if (WSACleanup() != 0)
{
fprintf(stderr, "WSACleanup failed.\n");
exit(1);
}
return 0;
}
|