Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用C编写客户机/服务器套接字程序来请求主机名IP地址?_C_Sockets_Ip_Server_Client - Fatal编程技术网

如何用C编写客户机/服务器套接字程序来请求主机名IP地址?

如何用C编写客户机/服务器套接字程序来请求主机名IP地址?,c,sockets,ip,server,client,C,Sockets,Ip,Server,Client,我正在制作一个客户机/服务器套接字程序,其中客户机可以发送主机名请求,服务器返回主机名的IP地址。我不知道出了什么问题,但我的(get_IP)函数似乎不起作用,即使它在服务器端正确返回主机名。当我将第109行的缓冲区大小更改为14,并从客户端请求“www.google.com”时,它工作正常。我怀疑问题是由缓冲区引起的。帮点忙 Server.c #include <netinet/in.h> #include <arpa/inet.h> #include <sys/

我正在制作一个客户机/服务器套接字程序,其中客户机可以发送主机名请求,服务器返回主机名的IP地址。我不知道出了什么问题,但我的(get_IP)函数似乎不起作用,即使它在服务器端正确返回主机名。当我将第109行的缓冲区大小更改为14,并从客户端请求“www.google.com”时,它工作正常。我怀疑问题是由缓冲区引起的。帮点忙

Server.c

#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netdb.h>

#define SERVER_PORT   16700 


char *get_IP(char *host)
{
  struct hostent *hent;
  int iplen = 15;
  char *ip = (char *)malloc(iplen+1);

  memset(ip, 0, iplen+1);
  if((hent = gethostbyname(host)) == NULL)
  {
     herror("Can't get IP");
     exit(1);
  }
  if(inet_ntop(AF_INET,(void *)hent->h_addr_list[0], ip, iplen) == NULL)
  {
    perror("Can't resolve the host");
    exit(1);
  }
  return ip;
}

char *host_page(char *ip)
{
  int sockfd = 0, n;
  char buffer[1024];
  struct sockaddr_in host_addr;
  char *page = malloc(1024);

  memset(buffer, '0' ,sizeof(buffer));
  if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))< 0)
      error("\n Error : Could not create socket \n");


  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(80);
  host_addr.sin_addr.s_addr = inet_addr(ip);

  if(connect(sockfd, (struct sockaddr *)&host_addr, sizeof(host_addr))<0)
     error("\n Error : Connect Failed \n");


  n = write(sockfd,"GET", 3);
  if (n < 0) 
     error("\n ERROR: writing to socket");


  //getting server response
  bzero(buffer,1024);
  n = read(sockfd,buffer,1023);
  if (n < 0) 
     error("\n ERROR: reading from socket");
  strcpy(page, buffer);

  close(sockfd);

 return page;
}


int main(void)
{
  int sockfd, listenfd = 0,connfd = 0, newsockfd, clilen;

  struct sockaddr_in serv_addr, cli_addr;

  char buffer[1025];  
  int n;  

/* Establishing communication with socket */

  printf("XXXXXX - WELCOME TO PAUL'S PROXY SERVER - XXXXXX \n\n");

  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  printf("Connection established... \n\n");

  memset(&serv_addr, '0', sizeof(serv_addr));
  memset(buffer, '0', sizeof(buffer));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);  
  serv_addr.sin_port = htons((unsigned short)SERVER_PORT);

  bind(sockfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));

  if(listen(sockfd, 10) == -1)
     error("Failed to listen\n");

/* start communication with client */     

  newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
  if(newsockfd < 0)
     error("ERROR on accept");

  //getting hostname request
  bzero(buffer, 1024);
  n = read(newsockfd, buffer, 14);

  if (n < 0) 
     error("ERROR reading from socket");
  printf("client requested IP for: %s \n", buffer);

  char ip[40];

  strcpy (ip, get_IP(buffer));

  char page[2000];  

  strcpy(page, host_page(ip));



  //Sending response to client request
  n = write(newsockfd, page, 1024);

  if(n<0)
     error("ERROR reading from socket");      



  return 0;
}
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

#define SERVER_IP    "129.120.151.94"
#define SERVER_PORT  46700

int main(void)
{
  int sockfd = 0, n;
  char buffer[1024];
  struct sockaddr_in serv_addr;

  memset(buffer, '0' ,sizeof(buffer));
  if((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))< 0)
      error("\n Error : Could not create socket \n");


  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons((unsigned short)SERVER_PORT);
  serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

  if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
     error("\n Error : Connect Failed \n");

  printf("Enter hostname in the form www.host.domain:");
  bzero(buffer,1024);
  fgets(buffer,1023,stdin);
  n = write(sockfd,buffer,strlen(buffer));
  if (n < 0) 
     error("\n ERROR: writing to socket");

  printf("\n hostname IP is: ");
  bzero(buffer,1024);
  n = read(sockfd,buffer,1023);
  if (n < 0) 
     error("\n ERROR: reading from socket");
  printf("%s\n",buffer); 

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义服务器端口16700
char*获取IP(char*主机)
{
结构hostent*hent;
int-iplen=15;
char*ip=(char*)malloc(iplen+1);
memset(ip,0,iplen+1);
if((hent=gethostbyname(host))==NULL)
{
herror(“无法获得IP”);
出口(1);
}
if(inet_ntop(AF_inet,(void*)hent->h_addr_list[0],ip,iplen)==NULL)
{
perror(“无法解析主机”);
出口(1);
}
返回ip;
}
char*主机页面(char*ip)
{
int sockfd=0,n;
字符缓冲区[1024];
主机地址中的结构sockaddr\u;
char*page=malloc(1024);
memset(缓冲区,'0',sizeof(缓冲区));
if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
错误(“\n错误:无法创建套接字\n”);
host_addr.sin_family=AF_INET;
主机地址sin端口=htons(80);
主机地址sin地址s地址=inet地址(ip);

如果(connect(sockfd,(struct sockaddr*)和host_addr,sizeof(host_addr)),请添加其工作原理的详细信息。您会收到哪些错误?调用fgets()时,请使用输入缓冲区的完整大小。fgets()函数将在看到输入缓冲区的换行符EOF只剩下一个字符时停止输入字符。然后fgets()将向输入缓冲区的内容追加NUL字节。请注意,换行符也将位于输入缓冲区中,该换行符(如果存在)需要替换为NUL字节。您可以通过调用
gethostbyname()
使用以下语法来获取主机的IP:
struct hostent*gethostbyname(const char*name)
不访问主机。类似地,
gethostbyaddr()
将使用以下语法获取主机名:
struct hostent*gethostbyaddr(const void*addr,socklen\u t len,int type);
此行:
printf(\n主机名IP为:)
以下几行完全是浪费,因为主机IP已经硬编码到客户端源代码中。我到处查看,代码都错误地处理以null结尾的字符串。这个主题需要仔细研究。