#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>#define PORT 10000
int main()
{ int ss, cs;
struct sockaddr_in sa;
socklen_t len;
if( (ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{ perror("socket()");
return 1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(PORT);
if(bind(ss, (struct sockaddr*)&sa, sizeof(sa)) < 0)
{ perror("bind()");
return 1;
}
if(listen(ss, 5) < 0)
{ perror("listen()");
return 1;
}
if( (cs = accept(ss, NULL, NULL)) < 0)
{ perror("accept()"); return 1;
}
len = sizeof(sa);
memset(&sa, 0, len);
if(getpeername(cs, (struct sockaddr*)&sa, &len) < 0)
{ perror("getpeername()");
return 1;
}
printf("remote peer: %s\n", inet_ntoa(sa.sin_addr));
return 0;
}