socket.c

 1 /* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $
 2  *
 3  * This module has been modified by Radim Kolar for OS/2 emx
 4  */
 5 
 6 /***********************************************************************
 7   module:       socket.c
 8   program:      popclient
 9   SCCS ID:      @(#)socket.c    1.5  4/1/94
10   programmer:   Virginia Tech Computing Center
11   compiler:     DEC RISC C compiler (Ultrix 4.1)
12   environment:  DEC Ultrix 4.3 
13   description:  UNIX sockets code.
14  ***********************************************************************/
15  
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <fcntl.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 
29 int Socket(const char *host, int clientPort)
30 {
31     int sock;
32     unsigned long inaddr;
33     struct sockaddr_in ad;
34     struct hostent *hp;
35     
36     memset(&ad, 0, sizeof(ad));
37     ad.sin_family = AF_INET;
38 
39     inaddr = inet_addr(host);
40     if (inaddr != INADDR_NONE)
41         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
42     else
43     {
44         hp = gethostbyname(host);
45         if (hp == NULL)
46             return -1;
47         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
48     }
49     ad.sin_port = htons(clientPort);
50     
51     sock = socket(AF_INET, SOCK_STREAM, 0);
52     if (sock < 0)
53         return sock;
54     if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
55         return -1;
56     return sock;
57 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2021-09-10
  • 2021-12-28
  • 2021-11-26
猜你喜欢
  • 2021-06-05
  • 2021-07-06
  • 2021-12-12
  • 2021-08-06
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
相关资源
相似解决方案