这个是客户端。========================================================
=================================================================
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <strings.h>
-
#include <sqlite3.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
-
#define N 256
-
#define R 1 // user register
-
#define L 2 // user login
-
#define Q 3 // query word
-
#define H 4 // history record
-
-
typedef struct sockaddr SA;
-
-
typedef struct
-
{
-
char type;
-
char name[N];
-
char data[N]; // password or word or remark
-
} MSG;
-
-
void do_register(int sockfd, MSG *buf);
-
int do_login(int sockfd, MSG *buf);
-
void do_query(int sockfd, MSG *buf);
-
void do_history(int sockfd, MSG *buf);
-
-
int main(int argc, char *argv[])
-
{
-
int sockfd;
-
struct sockaddr_in servaddr;
-
char clean[N];
-
MSG buf;
-
-
if (argc < 3)
-
{
-
printf("Usage : %s \n", argv[0]);
-
exit(-1);
-
}
-
-
bzero(&buf, sizeof(MSG));
-
-
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-
{
-
perror("fail to socket");
-
exit(-1);
-
}
-
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = PF_INET;
-
servaddr.sin_port = htons(atoi(argv[2]));
-
//这里要注意inet_addr()里面是255.255.255.255 和 里面写的不对时候的返回好像都是-1;
-
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
-
-
-
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
-
{
-
perror("fail to connect");
-
exit(-1);
-
}
-
-
int n;
-
while ( 1 )
-
{
-
printf("************************************\n");
-
printf("* 1: register 2: login 3: quit *\n");
-
printf("************************************\n");
-
printf("please choose : ");
-
if (scanf("%d", &n) == 0) //匹配失败的时候,返回为0,输入的字符进入了stdin 的缓冲区
-
{
-
fgets(clean, N, stdin); //这里要将缓冲区里面的“不正确的字符”给吃掉
-
printf("\n");
-
continue;
-
}
-
switch ( n )
-
{
-
case 1 :
-
printf("\n");
-
do_register(sockfd, &buf);
-
printf("\n");
-
break;
-
case 2 :
-
printf("\n");
-
if (do_login(sockfd, &buf))
-
goto next;
-
printf("\n");
-
break;
-
case 3 :
-
close(sockfd);
-
exit(0);
-
}
-
}
-
next:
-
while ( 1 )
-
{
-
printf("************************************\n");
-
printf("* 1: query word 2: History 3: quit *\n");
-
printf("************************************\n");
-
printf("please choose : ");
-
if (scanf("%d", &n) == 0)
-
{
-
fgets(clean, N, stdin);
-
printf("\n");
-
continue;
-
}
-
switch ( n )
-
{
-
case 1 :
-
printf("\n");
-
do_query(sockfd, &buf);
-
printf("\n");
-
break;
-
case 2 :
-
printf("\n");
-
do_history(sockfd, &buf);
-
printf("\n");
-
break;
-
case 3 :
-
close(sockfd);
-
exit(0);
-
}
-
}
-
-
-
-
-
exit(0);
-
}
-
-
void do_register(int sockfd, MSG *buf)
-
{
-
buf->type = R;
-
printf("input name:");
-
scanf("%s", buf->name);
-
-
printf("input passwd:");
-
scanf("%s", buf->data);
-
-
#ifdef _DEBUG_
-
printf("type=%d name=%s data=%s\n", buf->type, buf->name, buf->data);
-
#endif
-
-
send(sockfd, buf, sizeof(MSG), 0);
-
-
bzero(buf, sizeof(MSG));
-
recv(sockfd, buf, sizeof(MSG), 0);
-
printf("reply-%s\n", buf->data);
-
}
-
-
int do_login(int sockfd, MSG *buf)
-
{
-
buf->type = L;
-
printf("input name:");
-
scanf("%s", buf->name);
-
-
printf("input passwd:");
-
scanf("%s", buf->data);
-
-
#ifdef _DEBUG_
-
printf("type=%d name=%s data=%s\n", buf->type, buf->name, buf->data);
-
#endif
-
-
send(sockfd, buf, sizeof(MSG), 0);
-
-
bzero(buf, sizeof(MSG));
-
recv(sockfd, buf, sizeof(MSG), 0);
-
printf("reply-%s\n", buf->data);
-
-
if (strcmp(buf->data, "OK") == 0)
-
return 1;
-
else
-
return 0;
-
}
-
-
void do_query(int sockfd, MSG *buf)
-
{
-
while (1)
-
{
-
buf->type = Q;
-
printf("input word:");
-
scanf("%s", buf->data);
-
if (strcmp(buf->data, "#") == 0)
-
break;
-
-
send(sockfd, buf, sizeof(MSG), 0);
-
-
bzero(buf, sizeof(MSG));
-
recv(sockfd, buf, sizeof(MSG), 0);
-
printf("%s\n", buf->data);
-
}
-
}
-
-
void do_history(int sockfd, MSG *buf)
-
{
-
buf->type = H;
-
-
send(sockfd, buf, sizeof(MSG), 0);
-
-
while (1)
-
{
-
bzero(buf, sizeof(MSG));
-
ssize_t n = recv(sockfd, buf, sizeof(MSG), 0);
-
printf("n=%d data=%s\n", n, buf->data);
-
if (n == -1)
-
{
-
perror("recv");
-
exit(-1);
-
}
-
if (strcmp(buf->data, "#") == 0)
-
{
-
printf("data-%s\n", buf->data);
-
break;
-
}
-
printf("%s\n", buf->data);
-
}
-
}
这个是服务器==============================================================
======================================================================
-
#include <stdio.h>
-
#include <sys/wait.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <sqlite3.h>
-
#include <signal.h>
-
#include <time.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
-
#define N 256
-
#define R 1 // user register
-
#define L 2 // user login
-
#define Q 3 // query word
-
#define H 4 // history record
-
-
typedef struct sockaddr SA;
-
-
typedef struct
-
{
-
char type;
-
char name[N];
-
char data[N]; // password or word
-
}MSG;
-
-
void do_register(int connfd, MSG *buf);
-
void do_login(int connfd, MSG *buf);
-
void do_query(int connfd, MSG *buf);
-
void do_history(int connfd, MSG *buf);
-
-
int main(int argc, char *argv[])
-
{
-
int listenfd, connfd;
-
struct sockaddr_in myaddr;
-
pid_t pid;
-
ssize_t n;
-
MSG buf;
-
-
if (argc < 3)
-
{
-
printf("Usage : %s \n", argv[0]);
-
exit(-1);
-
}
-
-
if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-
{
-
perror("fail to socket");
-
exit(-1);
-
}
-
-
bzero(&myaddr, sizeof(myaddr));
-
myaddr.sin_family = PF_INET;
-
myaddr.sin_port = htons(atoi(argv[2]));
-
myaddr.sin_addr.s_addr = inet_addr(argv[1]);
-
-
if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
-
{
-
perror("fail to bind");
-
exit(-1);
-
}
-
-
if (listen(listenfd, 5) < 0)
-
{
-
perror("fail to listen");
-
exit(-1);
-
}
-
-
signal(SIGCHLD, SIG_IGN); //子进程死了的时候会变成僵尸进程,
-
while ( 1 ) //这里是交个操作系统来解决这个问题
-
{
-
if ((connfd = accept(listenfd, NULL, NULL)) < 0)
-
{
-
perror("fail to accept");
-
exit(-1);
-
}
-
if ((pid = fork()) < 0)
-
{
-
perror("fail to fork");
-
exit(-1);
-
}
-
else if (pid == 0) //这里是并发服务器,
-
{
-
close(listenfd); //在子进程中要关掉监听listenfd套接字
-
while(1)
-
{
-
bzero(&buf, sizeof(buf));
-
n = recv(connfd, &buf, sizeof(MSG), 0); //在服务器里收到的字符的个数
-
printf("n=%d type=%d name=%s data=%s\n", n, buf.type, buf.name, buf.data);
-
if (n == 0) //对于tcp连接来说,客服端close的时候,
-
break;
-
switch (buf.type)
-
{
-
case R: do_register(connfd, &buf); break;
-
case L: do_login(connfd, &buf); break;
-
case Q: do_query(connfd, &buf); break;
-
case H: do_history(connfd, &buf); break;
-
}
-
-
}
-
close(connfd);
-
exit(0);
-
}
-
close(connfd); //在父进程中要关闭连接connfd套接字
-
}
-
-
exit(0
阅读(1097) | 评论(0) | 转发(0) |