+ -
当前位置:首页 → 问答吧 → socket 地址问题请教

socket 地址问题请教

时间:2010-07-19

来源:互联网

本帖最后由 peijue 于 2010-07-19 15:58 编辑

server.h文件
  1. #include <sys/types.h>
  2. #include <pthread.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <netinet/in.h>
  8. #include <fcntl.h>
  9. #include <arpa/inet.h>
  10. #define SERVER_PORT 48888 /*定义通信端口 */
  11. #define BUF_SIZE   4096   /*定义传输数据的大小*/
  12. #define QUEUE_SIZE 10 /* 定义最大连接数 */

  13. void *work_thread(void *c)
  14. {
  15.         int sockfd = (int)c;
  16.         printf("thread id : %u\n", pthread_self() );
  17.         int fd, l;
  18.         char buf[BUF_SIZE];
  19.         l = recv(sockfd, buf, BUF_SIZE, 0);
  20.         printf("%s\n", buf);
  21.         if( l == -1) perror("l");
  22.         fd = open(buf, O_RDONLY);
  23.         if( fd  < 0  )
  24.         {
  25.                 printf("open file fail: %s\n", buf);
  26.                 sprintf(buf, "%s", "not the file in server\n");
  27.                 send(sockfd, buf, strlen(buf)+1, 0);
  28.                 close(sockfd);
  29.                 return (void *)1;
  30.         }
  31.         while(1)
  32.         {
  33.                 l = read(fd, buf, BUF_SIZE);
  34.                 if( l <= 0 ) break;
  35.                 send(sockfd, buf, l, 0);
  36.         }
  37.         printf("send file success\n");
  38.         close(fd);
  39.         close(sockfd);
  40.         return (void *)1;
  41. }

  42. int main( int argc, char *argv[] )
  43. {
  44.         int b, s, l, sa, fd, bytes, on = 1;
  45.         char buf[ BUF_SIZE ];  /*发送文件的缓冲区*/
  46.         struct sockaddr_in channel, addr_c;  /* 控制IP 地址的结构 */

  47.         /*--初始化套接字地址结构--*/
  48.         memset(&channel, 0, sizeof(struct sockaddr_in ) );
  49.         channel.sin_family = AF_INET;
  50.         channel.sin_addr.s_addr = htonl( INADDR_ANY  );
  51.         channel.sin_port = htons( SERVER_PORT );
  52.        

  53.         /*--创建套接字等待连接--*/

  54.         s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  55.         if( s < 0 ) perror("s");
  56.         setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) );

  57.         b = bind( s, (struct sockaddr *)&channel, sizeof(channel) );
  58.         if( b < 0) perror("b");
  59.        
  60.         l = listen( s, QUEUE_SIZE );
  61.         int len = sizeof(struct sockaddr_in);
  62.         if( l < 0) perror("l");
  63.         /* 等待连接到来,并处理连接请求*/
  64.         while(1)
  65.         {
  66.                 int err;
  67.                 char ip[INET_ADDRSTRLEN];
  68.                 sa = accept( s, (struct sockaddr *)&addr_c, &len );/*接受一个套接字的请求*/
  69.                 if( sa < 0)
  70.                         perror("sa");
  71.                 inet_ntop(AF_INET, &addr_c, ip, INET_ADDRSTRLEN);
  72.                 printf("\n%s\n", ip );
  73.                 pthread_t tid;
  74.                 err = pthread_create(&tid, NULL, work_thread, (void *)sa);
  75.                 if(err != 0)
  76.                 {
  77.                         printf("create thread failed");
  78.                         exit(1);
  79.                 }

  80.         }

  81. }
复制代码
为什么同一个IP连接过来 打印出来的IP都不一样呢?
  1. inet_ntop(AF_INET, &addr_c, ip, INET_ADDRSTRLEN);
  2. printf("\n%s\n", ip );
复制代码
各位帮帮忙;
得到的IP都是这样的。
  1. 2.0.134.51
  2. thread id : 3029031792
  3. test.c
  4. send file success

  5. 2.0.134.52
  6. thread id : 3020639088
  7. test.c
  8. send file success
复制代码

作者: peijue   发布时间: 2010-07-19

请问,服务器怎么实现relay,我的想法是把每个客户端的连接的IP地址记录下来,存放在一个表里,然后发送信息的一方发送内容里带接收方的IP地址,然后进行转发,可是我这样接受过来的IP地址是不一样的

作者: peijue   发布时间: 2010-07-20