📄 getchar11.c
字号:
//-----------------------取号机客户端------------------
// 文 件 名:getchar11.c
//
// 摘 要:程序启动后输入 “A” (人民币) 或者 “B” (外币)
// 选择进行何种业务。“Q” 退出。
//
//
// 作 者:钟树青
//
// 完成日期:2007-11-21
//
// 备 注:
//-----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.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;
//char strtemp[50];
//char *temp_ch = NULL;
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 (pbuf == NULL)
{
printf("Memory Requisition Error!\n");
exit(0);
}
printf("Please input \"A\" : RMB operation!\n");
printf("Please input \"B\" : $ operation!\n");
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)
{
scanf("%c",&ch);
getchar();
//temp_ch = gets(strtemp);
switch (ch)
{
case 'A':
{
if (send(sockfd, "A", 10, 0) == -1)
{
perror("send");
exit(0);
}
if ((numbytes=recv(sockfd, pbuf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
printf("Received: %c%d\n",pbuf->blogs,pbuf->n);
break;
}
case 'B':
{
if (send(sockfd, "B", 10, 0) == -1)
{
perror("send");
exit(0);
}
if ((numbytes=recv(sockfd, pbuf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
printf("Received: %c%d\n",pbuf->blogs,pbuf->n);
break;
}
case 'Q':
{
if (send(sockfd, "Q", 10, 0) == -1)
{
perror("send");
exit(0);
}
break;
}
default:
{
printf("Please input 'A' and 'B'!\n");
}
}
if(ch == 'Q')
break;
}
free(pbuf);
close(sockfd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -