#ifndef unix
#include <winsock2.h>
#else
#define closesocket close
#define SOCKET int
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif
#include <iostream>
#include <cstring>
using namespace std;
#define PORT 1200
char localhost[] = "localhost";
int main()
{
struct hostent *ptrh;
struct protoent *ptrp;
struct sockaddr_in sad;
SOCKET sd;
char host[256];
#ifdef WIN32
WSADATA wsaData;
if(WSAStartup(0x0101, &wsaData)!=0)
{
fprintf(stderr, "Windows Socket Init failed: %d\n", GetLastError());
exit(1);
}
#endif
memset((char *)&sad,0,sizeof(sad));
sad.sin_family = AF_INET;
strcpy(host,localhost);
ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL ) {
cerr << "invalid host: " << host << endl;
exit(1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
sad.sin_port = htons((u_short)PORT);
ptrp = getprotobyname("tcp");
if ( ptrp == 0) {
cerr << "cannot map \"tcp\" to protocol number" << endl;
exit(1);
}
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
cerr << "soclet creation failed" << endl;
exit(1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
cerr << "connection failed" << endl;
exit(1);
}
{
int n;
char buf[1000];
n = recv(sd, buf, sizeof(buf), 0);
while (n > 0) {
cout.write(buf, n);
n = recv(sd, buf, sizeof(buf), 0);
}
}
closesocket(sd);
#ifdef WIN32
WSACleanup();
#endif
return(0);
}