⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delchar11.c

📁 目前银行排队机的服务器、打号机、处理窗口三部分的一个基本框架。 程序用多线程、socket编程。
💻 C
字号:
//-----------------------叫号机客户端------------------
// 文 件 名:delchar11.c
//
// 摘    要:程序启动后输入 “A” (人民币) 或者 “B” (外币)
//           选择处理何种业务。选择后每次按回车来取得处理
//           号码。中间可以按 “R” 复位,“Q” 退出。
//
// 作    者:钟树青
//
// 完成日期:2007-11-21
//
// 备    注:
//-----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <curses.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#define MAXDATASIZE 100 /*每次最大数据传输量 */

int main(int argc, char *argv[])
{
    int sockfd, numbytes;
    
	struct hostent *he;
    
	struct sockaddr_in their_addr;
   
	unsigned int myport;
   
    char ch[5]={0};
	
	struct formation
	{
		char blogs;
		
		int  n;
		
		struct formation *next;
	};
	
	struct formation * pbuf;

    if(argv[2])
		
		myport = atoi(argv[2]);
    else 
		
		myport = 7838;

    if (argc != 3)
	{
		fprintf(stderr,"usage: %s 127.0.0.1 7838 \n", argv[0]);
       
		exit(1);
    }

    if((he=gethostbyname(argv[1]))==NULL) 
    {        
		herror("gethostbyname");

        exit(1);
    }
    	
    pbuf=(struct formation*)malloc(sizeof(struct formation));
  
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) 
    {
        perror("socket");

        exit(1);
    }
	
	their_addr.sin_family=PF_INET;
	
	their_addr.sin_port=htons(myport);
	
	their_addr.sin_addr = *((struct in_addr *)he->h_addr);
	
	bzero(&(their_addr.sin_zero),0);
	
	if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) 
    {
		perror("connect");

        exit(1);
	}
	
	while (1)
	{
		int first=0;
		
		while (1)
		{
			if(first==0)
			{
				printf("Please put \"A\" & \"B\" select serving type!\n");
				
				printf("working may put \"R\" anew begin!\n");
				
				printf("working may put \"Q\" end!\n");
				
				ch[1]=getchar();
				
				ch[0]=getchar();
				
				if(ch[1] == 'Q')
				{
					ch[0] = ch[1];
					
					break;
				}
				
				if(ch[1] == 'R')
				{
					break;
				}
				
				first=1;
			}
			
			else
			{
				ch[0]=getchar();
			}

			if (ch[0] == 'R')
			{
				getchar();
				
				break;
			}

			if (ch[0] == 'Q')
			{
				ch[0] = 'Q';
				break;
			}
			
			if (send(sockfd, ch, 10, 0) == -1)
			{
				perror("send");
				
				exit(0);
			}
			
			if ((numbytes=recv(sockfd, pbuf, MAXDATASIZE, 0)) == -1)
			{
				perror("recv");
				
				exit(1);
			}
			
			if(pbuf->n == 0)
			{
				printf("Queue is empty!\n");
			}
			else
			{
				printf("Received: %c%d\n",pbuf->blogs,pbuf->n);
			}
		}
		
		if (ch[0] == 'Q')
		{
			if (send(sockfd, ch, 10, 0) == -1)
			{
				perror("send");
				
				exit(0);
			}
			
			free(pbuf);
			
			close(sockfd);
			
			break;
		}
	}
	return 0;
}
  
    //***********************************************************************
	
   
    


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -