【问题标题】:Server Client program in C using sockets and tcpC中使用套接字和tcp的服务器客户端程序
【发布时间】:2016-08-30 05:29:27
【问题描述】:

我编写了一个程序,用于使用套接字编程在服务器和客户端之间进行简单的文本聊天。服务器端运行正常,因为它正在侦听,但是当我运行客户端程序时,它运行但没有显示输出,这意味着如果我在客户端输入,服务器端不会显示任何内容。为什么我没有收到客户端的任何响应?

    /****************** CLIENT CODE ****************/

        #include <stdio.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <string.h>

        int main(){
  int clientSocket,n;
  char buffer[256];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  clientSocket = socket(PF_INET, SOCK_STREAM, 0);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/
  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

  if (connect(clientSocket,(struct sockaddr *) &serverAddr,sizeof(serverAddr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(clientSocket,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(clientSocket,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(clientSocket);
  /*---- Read the message from the server into the buffer ----*/
  //recv(clientSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
  //printf("Data received: %s",buffer);   

  return 0;
}


/****************** SERVER CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
  int welcomeSocket, newSocket;
  char buffer[256];
  int n;
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

  /*---- Send message to the socket of the incoming connection ----*/
  bzero(buffer,256);
     n = read(welcomeSocket,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(welcomeSocket,"I got your message",18);
if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(welcomeSocket,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     close(newSocket);
     close(welcomeSocket);

  return 0;
}

【问题讨论】:

  • 快速浏览后,我想您可能也想在字符串后面发送结尾零,即write(clientSocket,buffer,strlen(buffer)+1);
  • 我这样做了,但现在我遇到了分段错误(核心转储)
  • n = read(welcomeSocket,buffer,255); -->> n = read(newSocket,buffer,255);n = write(welcomeSocket,"I got your message",18); 相同 [加:许多其他...]
  • 使用调试器或打印中间数据来查找问题或聚焦您的问题。社区不喜欢“我做错了什么”类型的问题。最好问“为什么缓冲区字​​符串长度和内容与接收到的数据不匹配?”是一个重点突出的问题,表明您不只是在转储代码供我们调试。
  • 是的,很抱歉。会改正的

标签: c linux sockets tcp


【解决方案1】:

在您的服务器代码中,read() 应该使用从 accept() 返回的套接字描述符,而不是从对 socket() 的调用返回的套接字描述符。所以尝试使用newSocket 而不是welcomeSocket 阅读。

n = read(newSocket,buffer,255);

【讨论】:

  • @T Johnson 感谢您的帮助。但是我在运行客户端程序时遇到了分段错误(核心转储)。你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2012-05-18
  • 1970-01-01
相关资源
最近更新 更多