LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 770|回复: 5

代码出错,连接拒绝。

[复制链接]
发表于 2003-12-7 22:35:16 | 显示全部楼层 |阅读模式
$ gcc -o receive_key receive_key.c
receive_key.c: In function `main':
receive_key.c:29: warning: passing arg 2 of `connect' from incompatible pointer type
出错了。代码如下:
----------------------------

  1.       1 #include <sys/stat.h>
  2.       2 #include <fcntl.h>
  3.       3 #include <unistd.h>
  4.       4 #include <sys/types.h>
  5.       5 #include <sys/socket.h>
  6.       6 #include <netinet/in.h>
  7.       7 #include <arpa/inet.h>
  8.       8
  9.       9 #define PORT    1234
  10.      10 #define SERVER_IP       "127.0.0.1"
  11.      11
  12.      12 main()
  13.      13  {
  14.      14    int s;
  15.      15    struct sockaddr_in addr;
  16.      16    char buffer[256];
  17.      17
  18.      18    if((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
  19.      19    {
  20.      20      perror("socket");
  21.      21      exit(1);
  22.      22    }
  23.      23
  24.      24    bzero(&addr, sizeof(addr));
  25.      25    addr.sin_family = AF_INET;
  26.      26    addr.sin_port = htons(PORT);
  27.      27    addr.sin_addr.s_addr = inet_addr(SERVER_IP);
  28.      28
  29.      29    if(connect(s,&addr, sizeof(addr)) < 0)
  30.      30    {
  31.      31      perror("connect");
  32.      32      exit(1);
  33.      33    }
  34.      34    //接收由 server 端传来的信息
  35.      35    recv(s, buffer, sizeof(buffer),0);
  36.      36    printf("%s\n", buffer);
  37.      37    while(1)
  38.      38    {
  39.      39      bzero(buffer, sizeof(buffer));
  40.      40      read(STDIN_FILENO, buffer, sizeof(buffer));
  41.      41      if(send(s, buffer,sizeof(buffer),0) < 0)
  42.      42      {
  43.      43        perror("send");
  44.      44        exit(1);
  45.      45      }
  46.      46    }
  47.      47  }
复制代码


$ ./receive_key
connect: Connection refused
发表于 2003-12-7 22:39:54 | 显示全部楼层
...你的1234有程序在侦听么
 楼主| 发表于 2003-12-9 21:14:31 | 显示全部楼层
没有
发表于 2003-12-9 21:21:08 | 显示全部楼层
没服务端的 代码 吗 ?
 楼主| 发表于 2003-12-11 20:39:24 | 显示全部楼层
哦,可能是没有服务器的问题吧。不会写啊。
发表于 2003-12-12 02:16:18 | 显示全部楼层
服务器端在运行就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表