C:用户(线程)不能发送消息的简单聊天程序

标签 c multithreading sockets client-server

我是 C 语言的新手,尤其是线程方面的新手。我读过一些关于此类问题的主题,但没有一个对我有用。这是我的代码:

#include <stdio.h>
#include <string.h> /* memset() */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>  
#include <pthread.h>

#define PORT    "7890" /* Port to listen on */
#define BACKLOG     10  /* Passed to listen() */
#define MAX_THREADS 100

//function that dumps buffer 
void dump(const unsigned char *data_buffer, const unsigned int length){
 unsigned char byte;
 unsigned int i, j;

 for(i=0; i < length; i++) 
 {

 byte = data_buffer[i];
 printf("%02x ", data_buffer[i]); // Display byte in hex.

 if(((i%16)==15) || (i==length-1)) {

 for(j=0; j < 15-(i%16); j++)
 printf(" ");
 printf("| ");

 for(j=(i-(i%16)); j <= i; j++) { // Display printable bytes from line.
 byte = data_buffer[j];

 if((byte > 31) && (byte < 127)) // Outside printable char range

 printf("%c", byte);

 else

 printf(".");

 }

 printf("\n"); // End of the dump line (each line is 16 bytes)

 } // End if
 } // End for
}


//thread handler
void *handle(void *pnewsock)
{
  int *sock = (int*)pnewsock;


  char buf[1024];

  while(recv(*sock, buf, 1024, 0) > 0)
  {
    printf("Inside...");
    dump(buf, strlen(buf));
    memset(buf, '\0', 1024);
  }


 printf("Outside...");
 return NULL;
 }

int main(void)
{
int sock;
pthread_t thread[MAX_THREADS];
struct addrinfo hints, *res;
int reuseaddr = 1; /* True */
//struct user client;

/* Get the address info */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(NULL, PORT, &hints, &res) != 0) {
    perror("getaddrinfo");
    return 1;
}

/* Create the socket */
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == -1) {
    perror("socket");
    return 1;
}


/* Bind to the address */
if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
    perror("bind");
    return 0;
}

freeaddrinfo(res);

/* Listen */
if (listen(sock, BACKLOG) == -1) {
    perror("listen");
    return 0;
}

size_t size = sizeof(struct sockaddr_in);
struct sockaddr_in their_addr;
int newsock;

printf("Waiting.\n");

int thread_counter = 0;

/* Main loop */
while (1) 
{
  newsock = accept(sock, (struct sockaddr*)&their_addr, &size);


  if (newsock == -1) 
  {
    perror("accept");
  }
  else 
  {
    printf("Got a connection from %s on port %d\n", 
               inet_ntoa(their_addr.sin_addr), htons(their_addr.sin_port));


    if (pthread_create(&thread[thread_counter], NULL, handle, &newsock) != 0)
    {
      fprintf(stderr, "Failed to create thread\n");
    }
    thread_counter++;

  }
}

close(sock);

return 0;
}

这是我的服务器。我将它与 telnet 连接(我知道它不安全,但用于教育目的)。当多个用户连接时,他们开始写一些东西,服务器显示(带有转储功能)。但是突然服务器停止接受他们的消息并且什么都不显示。我打印出一些字符串用于调试,但它表明线程不会离开 while 循环。但问题是它们不会留在 while 循环中,因为字符串 Inside... 没有打印。我不知道会发生什么。谢谢。

最佳答案

在将变量的地址传递给创建的线程后,您正在更改 main 中 newsock 的值。换句话说,同一个变量在多个线程中使用,造成不一致的结果。

确保在创建线程时,传递的地址指向只传递给该线程的变量。

关于C:用户(线程)不能发送消息的简单聊天程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876063/

相关文章:

c++ - 将 TCHAR 转换为 strstr 函数的 char*

c - 如何正确地将 float 转换为 int

c# - 从多个线程更新 DataGrid

android - 有很多 HandlerThreads 活着,什么也不做,只是等待,这不好吗?

python - 如何用python调试死锁?

c - socket C 客户端特定编号

c - 在arduino中什么是SREG?

c++ - 如何将窗口默认滚动条移动到状态栏上方?

c++ - 无法通过套接字发送文件

Python 客户端/服务器问题