2011-11-04 112 views
0

它是连接到进一步连接到客户端的中间主机的服务器代码。 中间服务器的作用是从服务器或客户端获取数据并在另一端转发数据。 假设有两个客户端请求数据(ie'a'),所以在我的服务器中会生成两个用于定期数据的线程,但是当任何客户端想要退出时发送'b'并且在接收到'b'两个线程退出。 那么,你能否给我一些建议,以便在一个请求中只有一个线程退出(即'b')。c socket编程

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <pthread.h> 


void* periodic(void* t); 
void* oneshot(void* t); 

char value='\0',control; 
static pthread_mutex_t data_lock; 
int no=0,count=0; 

struct transfer 
{ 
int connec; 
struct sockaddr_in client; 
}; 

pthread_t tid1,tid3; 
int main(int argc,char *argv[]) 
{ 
    int sock, connected, bytes_recieved , flag,ret,true; 
    struct sockaddr_in server_addr,client_addr; 
    struct transfer t2,*t1=NULL; 
    t1=&t2; 
    int sin_size; 
    /*here is normal connec stablishment between server and intermidiate host*/ 
    printf("\nTCPServer Waiting for client on port 5000"); 
    fflush(stdout); 


    sin_size = sizeof(struct sockaddr_in); 

    connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size); 


    printf("\n waiting for data request from server2:.......\n"); 
    fflush(stdout); 

    pthread_mutex_init(&data_lock,0); 

    t1->connec=connected; 
    t1->client=client_addr; 

    pthread_create(&tid3,NULL,recieve,(void*)t1); 
    while(1){ 
      if(value=='\0') 
      continue; 
      if(value=='a') 
      { 
       pthread_create(&tid1,NULL,periodic,(void*)t1); 
       count++; 
       control=value; 
       value='\0'; 
      } 

      else if(value=='b') 
      { 
      control=value; 
      pthread_join(tid1,NULL); 
      value='\0'; 
      } 
      }//end of while 
    pthread_join(tid3,NULL); 
    close(connected); 

    }//end of main 

    void* periodic(void* t) 
    { 
    struct transfer *t1=(struct transfer*)t; 
    int data=100; 
    printf("periodic"); 

    fflush(stdout); 
    while(1){ 
      if(control=='b') 
      {count--; 
      data=0; 
      send(t1->connec,&data,sizeof(data),0); 
      printf("1 time\n"); 

      break; 
      } 
      pthread_mutex_lock(&data_lock); 
      send(t1->connec,&data,sizeof(data),0); 
      pthread_mutex_unlock(&data_lock); 

      sleep(4); 
      printf("coming\n"); 
      } 
      printf("comming out\n"); 
      pthread_exit(NULL); 
     }//end of periodic 


     void* recieve(void *t) 
     { 
      struct transfer *t1=(struct transfer*)t; 
      char control; 

     while(1) 
     { 
      //control=value; 
      recv(t1->connec,&value,1,0); 
      //if(value!=control) 
      printf("%c\n",value); 

      } 
      printf("out of recieve\n"); 

     fflush(stdout); 

      pthread_exit(NULL); 
     }//end of recieve 

回答

1

也许你不应该为两个客户共享相同的“价值”。如果你有一组客户端和相应的值,会更好。客户端可以通过accept(见client_addr)返回的sockaddr结构来识别。

+0

我做了全局值,因为我需要检查每个线程的值和那无限while循环是不断检查中间主机接收到的值。所以,有没有其他方式来跨线程共享数据?角。是服务器不直接与客户端联系,我只有服务器和中间主机之间的单一连接。 – tod

0

这个例子是不安全的或将起作用。 value是一个全局变量,可以被所有线程操纵。接收缓冲区必须在线程内创建。而且,你在主循环中创建了一个忙循环,while循环等待值的改变。如果您有多个客户端连接,并且一个真正的数据服务器应该回答请求,您必须实现一个客户端服务器模型。或者如果线程太复杂,请看看select! 。

+0

我做了全局值,因为我需要在每个线程中检查其值,并且无限while循环用于连续检查中间主机接收到的值。那么,是否有任何其他方式可以跨线程共享数据?角。是服务器不直接与客户端联系,我只有服务器和中间主机之间的单一连接。 – tod