?? 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 + -