#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;
int alen;
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);
alen = sizeof(sad);
ptrp = getprotobyname("udp");
if ( ptrp == 0) {
fprintf(stderr, "cannot map \"udp\" to protocol number\n");
exit(1);
}
sd = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "soclet creation failed\n");
exit(1);
}
{
int n;
int m;
char buf[1000];
m = 1;
m = sendto(sd,buf,m,0,(struct sockaddr*)&sad,alen);
if(m<0)
{
fprintf(stderr,"Error in sending");
}
else
{
n = recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr*)&sad,&alen);
if (n > 0) {
buf[n]='\0';
printf("%s", buf);
}
}
}
closesocket(sd);
#ifdef WIN32
WSACleanup();
#endif
return(0);
}