【问题标题】:socket connection failure套接字连接失败
【发布时间】:2018-12-13 21:43:01
【问题描述】:

我是套接字编程的初学者,正在阅读 Linux 网络编程书籍。我决定按照书中所示实现客户端-服务器连接。服务器程序在 Ubuntu 14.04 机器上运行,客户端代码在 Mac 机器上运行。服务器代码如下

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

const char message[] = "hello, world\n";

int main()
{
  int sock = 0;
  int port = 0;
  sock = socket(AF_INET, SOCK_STREAM, 0);

  if (sock == -1)
    fprintf(stderr, "failed\n");
  else
    printf("connection is establisshed\n");

  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY );
  server.sin_port = 3500;

  int status = bind(sock, (struct sockaddr*) &server, sizeof(server));
  if (status == 0)
    printf("connection completed\n");
  else
    printf("problem is encountered\n");

  status = listen(sock, 5);
  if (status == 0)
    printf("app is ready to work\n");
  else
  {
    printf("connection is failed\n");
    return 0;
  }

  while (1)
  {
    struct sockaddr_in client = { 0 };
    int sclient = 0;
    int len = sizeof(client);
    int childSocket = accept(sock, (struct sockaddr*) &client, &len);
    if (childSocket == -1)
    {
      printf("cannot accept connection\n");
      close(sock);
      break;
    }

    write(childSocket, message, strlen(message));

    close(childSocket);
  }

  return 0;
}

至于客户端我写了以下代码

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

int main(int argc, char* argv[])
{
  int sock = 0;
  int port = 0;
  struct sockaddr_in servaddr;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  int status = 0;
  char buffer[256] = "";
  if (sock == -1)
  {
    printf("could not establish connection\n");
    exit(1);
  }

  port = 3500;
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  servaddr.sin_port = htons(port);
  status = connect(sock, (struct sockaddr*) &servaddr, sizeof(servaddr));
  if (status == 0)
    printf("connection is established successfully\n");
  else
  {
    printf("could not run the app\n");
    exit(1);
  }

  status = read(sock, buffer, sizeof(buffer));
  if (status > 0)
    printf("%d: %s", status, buffer);

  close(sock);

  return 0;
}

为了获取客户端机器的 IP 地址,我从终端运行 ifconfig 并获取 inet_addr 192.168.1.165 值。现在,当我将该地址字符串作为命令行参数传递时,我收到消息表明应用程序未运行消息。据我了解,我得到的地址有问题。那么问题是什么? 提前致谢

【问题讨论】:

  • 考虑使用perror() 记录错误消息。它提供了有关问题所在的更详细信息。检测到错误后立即调用,之前不要调用任何其他函数。
  • “我收到应用程序未运行的消息”。不,你没有。您的应用程序不会打印这样的消息,如果它打印了它不应该有:它应该打印errno,或strerror() 的结果,最简单的是使用perror(). 澄清您的问题。

标签: c linux sockets ifconfig


【解决方案1】:

很可能服务器没有侦听您假设的端口,即3500

要解决此问题,请更改此行:

server.sin_port=3500

成为

server.sin_port = htons(3500);

(要监视哪个进程在哪个地址:端口上列出,您可能希望使用netstat 命令行工具。在您的情况下,可能使用选项-a -p -n


在最近的系统上,accept() 需要一个指向 socklen_t 的指针作为最后一个参数,所以更改这个

int len=sizeof(client);

成为

socklen_t len = sizeof client; /* sizeof is an operator, not a function¨*/

【讨论】:

  • 太棒了!非常感谢您。 htons 转换成功!
猜你喜欢
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多