多线程服务器客户端C

时间:2018-12-16 15:13:33

标签: c multithreading sockets

您好,我在线程以及客户端和服务器实现方面遇到了一些问题,我创建了2个在套接字上写入无限数量的客户端。 在sever.c中管理2个客户端。每次接受新连接时,我都会创建线程。 它运行,但是如果一个客户端运行,它可以工作,但是如果我运行第二个客户端,则第一个中断自身;我如何交替打印? 我想要:G1                 G2                 G1等

G1。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>

#include <string.h>
#include <stdlib.h> 
#include <errno.h> 
#include <signal.h>
#include <time.h> 
void error(char *msg)
{
    perror(msg);
    exit(0);
}
struct message {  //dichiarazione struct
 time_t timestamp;  
 char g;  //process identifier
 int x;
};
int main(int argc, char *argv[])
{
  int sockfd, portno, n,i;
  struct message m1;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  struct timespec delay;
  delay.tv_sec = 1;
  delay.tv_nsec = 0; //in microseconds
  long int offset=1000000;
  struct timeval tv;
  char buffer[256];
  if (argc < 3) {
    fprintf(stderr,"usage %s hostname port\n", argv[0]);
    exit(0);
  }
  portno = atoi(argv[2]);
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) 
    error("ERROR opening socket");
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
  }
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_UNIX;
  bcopy((char *)server->h_addr, 
  (char *)&serv_addr.sin_addr.s_addr,
  server->h_length);
  serv_addr.sin_port = htons(portno);

  if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
    error("ERROR connecting");
    while(1){
    m1.timestamp=time(NULL);
    m1.x=i;
    m1.g=getpid();

    n = write(sockfd,&m1,sizeof(m1));
    if (n < 0) 
      error("ERROR writing to socket");
    i++;
    delay.tv_nsec=offset+rand()%offset;
    nanosleep(&delay,NULL);
  }

  return 0;
}`

R.c(服务器)

#include<stdio.h>

#include<stdlib.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include <arpa/inet.h>

#include <fcntl.h> // for open

#include <unistd.h> // for close

#include<pthread.h>
struct message {  //dichiarazione struct
  time_t timestamp;  
  char g;  //process identifier
  int x;
};
struct message client_message;

char buffer[1024];

static void * socketThread(void *arg)
{ 
  while(1) {
    int newSocket = *((int *)arg);

    recv(newSocket , &client_message , sizeof(client_message), 0);

    printf("message %d  %d %ld\n",client_message.x,client_message.g,client_message.timestamp);
    fflush(stdout);
    sleep(1);
  }
}

int main(){

  int serverSocket, newSocket;

  struct sockaddr_in serverAddr;

  struct sockaddr_storage serverStorage;

  socklen_t addr_size;

  //Create the socket. 

  serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);

  // Configure settings of the server address struct

  // Address family = Internet 

  serverAddr.sin_family = AF_UNIX;

  //Set port number, using htons function to use proper byte order 

  serverAddr.sin_port = htons(6005);

  //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(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  //Listen on the socket, with 40 max connection requests queued 

  if(listen(serverSocket,50)!=0)
  {
    printf("Error\n");
    return -1;
  }
  printf("Listening\n");

  pthread_t tid[60];

  int i = 0;

  while(1)
  {
        //Accept call creates a new socket for the incoming connection

        addr_size = sizeof serverStorage;

        newSocket = accept(serverSocket, (struct sockaddr *) &serverStorage, &addr_size);

        //for each client request creates a thread and assign the client request to it to process

        //so the main thread can entertain next request

        if( pthread_create(&tid[i], NULL, socketThread, &newSocket) != 0 )
           printf("Failed to create thread\n");
        else
          ++i;
  }

  return 0;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

据我在线程函数 socketThread()中的服务器代码中了解,您在每次迭代中都获得套接字描述符的值。但是当您在该地址接受新连接时,将写入新的套接字描述符的新值。之后,每个线程仅从最后一个套接字获取数据。

您应该按值(而不是指针)传递 socketThread()套接字描述符!