首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C:简单的聊天程序,用户(线程)不能发送消息

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

Stack Overflow用户
提问于 2015-08-07 10:58:17
回答 1查看 186关注 0票数 1

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

代码语言:javascript
复制
#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循环中,因为字符串不在.没有打印。我不知道会发生什么。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-07 11:01:46

在将变量的地址传递给创建的线程之后,您将在主目录中更改newsock的值。换句话说,在多个线程中使用相同的变量,从而产生不一致的结果。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31876063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档