#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 <stdio.h>
#include <string.h>
#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 ) {
fprintf(stderr, "invalid host: %s\n", host);
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) {
fprintf(stderr, "cannot map \"tcp\" to protocol number\n");
exit(1);
}
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "soclet creation failed\n");
exit(1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr, "connection failed\n");
exit(1);
}
{
char buf[1000];
sprintf(buf,"Automated Client\r\n");
send(sd, buf, strlen(buf),0);
}
{
int n;
char buf[1000];
n = recv(sd, buf, sizeof(buf), 0);
while (n > 0) {
fwrite(buf, n, 1, stdout);
n = recv(sd, buf, sizeof(buf), 0);
}
}
closesocket(sd);
#ifdef WIN32
WSACleanup();
#endif
return(0);
}