Cで基本的なプロキシサーバーを書いています.
リクエストを受信しているサーバー/マシンを名前または IP で識別したい。
どうやってこれを行うのですか?どこから情報を入手すればよいかわかりません。
これが私の接続コードです:
unsigned short port = atoi(argv[1]); /* port number to listen on */
struct sockaddr_in server;
server.sin_family = PF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( port );
/* host-to-network-short() convert to big endian */
int len = sizeof( server );
if ( bind( sock, (struct sockaddr *)&server, len ) < 0 )
{
perror( "bind() failed" );
return EXIT_FAILURE;
}
/* activate the socket as a listener */
listen( sock, 5 ); /* 5 is number of backlogged waiting client requests */
//printf( "Listener socket created and bound to port %d on fd %d\n", port, sock );
struct sockaddr_in client;
while ( 1 )
{
// printf( "Blocked on accept()\n" );
unsigned int fromlen;
int newsock = accept( sock, (struct sockaddr *)&client, &fromlen );
/* accept() blocks */
//printf( "Accepted client connection\n" );
char buffer[5000];
int n = read( newsock, buffer, 4999 );
if ( n < 1 )
{
perror("Read() failed.\n");
}
else
{
buffer[n] = '\0';
//printf( "Rcvd message from client: \n\n----\n\n%s\n\n----\n\n", buffer );
}