【Linux网络编程】客户端具体实现

#include
#include
#include//toupper的头文件
#include// sockaddr_in
#define SERV_PORT 6666
#define SERV_IP "127.0.0.1"

int main(void)
{
  int cfd;
  struct sockaddr_in serv_addr;
  //socklen_t ser_addr_len;
  char buf[BUFSIZ];
  int n;
  
  
  cfd=socket(AF_INET,SOCK_STREAM,0);
  
  memset(&serv_addr,0,sizeof(serv_addr));
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_port=htons(SERV_PORT);
  //serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  inet_pton(AF_INET,SERV_IP,&serv_addr.sin_addr.s_addr);
  
  
  connect(cfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
  while(1)
{  fgets(buf,sizeof(buf),stdin);
  write(cfd,buf,strlen(buf));
  
  n=read(cfd,buf,sizeof(buf));
  write(STDOUT_FILENO,buf,n);
  }
  close(cfd);
}

你可能感兴趣的:(Linux系统编程)