当前位置: 技术问答>linux和unix
接收不到client 发送过来的数据求解
来源: 互联网 发布时间:2016-06-22
本文导语: //运行方式:server 8888 #include #include #include #include #include #include #include #include #include #include int fp; char client_user[20],file_user[20]; void regi(int new_fd) //注册 { read(new_fd,client_user,20); fp = open("user.txt" , O_R...
//运行方式:server 8888
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fp;
char client_user[20],file_user[20];
void regi(int new_fd) //注册
{
read(new_fd,client_user,20);
fp = open("user.txt" , O_RDWR | O_APPEND);
while(fp != EOF)
{
write(fp, file_user,20);
if(strcmp(file_user,client_user) == 0)
{
printf("用户已经存在n");
}
}
write(fp,client_user,strlen(client_user));
}
void login(int new_fd)
{
read(new_fd,client_user,20);
fp = open("user.txt" , O_RDWR | O_APPEND);
if(client_user ==NULL)
{
printf("用户信息获取失败!.......n");
}
while(fp != EOF)
{
read(fp, file_user, 20);
if(strcmp(file_user,client_user) == 0)
{
printf("用户登陆成功");
}
}
if(strcmp(file_user,client_user) != 0)
{
printf("没有这个用户");
exit(1);
}
}
void server(int portnumber)
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char ch[2]="1";
int i;
int sin_size;
pi
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket error: %sn", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(portnumber);
if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Bind error: %sn", strerror(errno));
exit(1);
}
if(listen(sockfd, 5) == -1)
{
fprintf(stderr, "Listen error: %sn", strerror(errno));
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1)
{
fprintf(stderr, "Accept error: %sn", strerror(errno));
exit(1);
}
fprintf(stderr, "Server get connection from %sn", inet_ntoa(client_addr.sin_addr));
read(new_fd, ch, 2,0);
i = atoi(ch);
switch(i)
{
case 1: regi(new_fd); continue;
case 2: login(new_fd ); continue;
}
close(new_fd);
}
close(sockfd);
}
int main(int argc, char *argv[])
{
int portnumber;
if(argc != 2)
{
fprintf(stderr, "Usage: %s portnumbern", argv[0]);
exit(1);
}
if((portnumber = atoi(argv[1]))
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fp;
char client_user[20],file_user[20];
void regi(int new_fd) //注册
{
read(new_fd,client_user,20);
fp = open("user.txt" , O_RDWR | O_APPEND);
while(fp != EOF)
{
write(fp, file_user,20);
if(strcmp(file_user,client_user) == 0)
{
printf("用户已经存在n");
}
}
write(fp,client_user,strlen(client_user));
}
void login(int new_fd)
{
read(new_fd,client_user,20);
fp = open("user.txt" , O_RDWR | O_APPEND);
if(client_user ==NULL)
{
printf("用户信息获取失败!.......n");
}
while(fp != EOF)
{
read(fp, file_user, 20);
if(strcmp(file_user,client_user) == 0)
{
printf("用户登陆成功");
}
}
if(strcmp(file_user,client_user) != 0)
{
printf("没有这个用户");
exit(1);
}
}
void server(int portnumber)
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char ch[2]="1";
int i;
int sin_size;
pi
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket error: %sn", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(portnumber);
if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Bind error: %sn", strerror(errno));
exit(1);
}
if(listen(sockfd, 5) == -1)
{
fprintf(stderr, "Listen error: %sn", strerror(errno));
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1)
{
fprintf(stderr, "Accept error: %sn", strerror(errno));
exit(1);
}
fprintf(stderr, "Server get connection from %sn", inet_ntoa(client_addr.sin_addr));
read(new_fd, ch, 2,0);
i = atoi(ch);
switch(i)
{
case 1: regi(new_fd); continue;
case 2: login(new_fd ); continue;
}
close(new_fd);
}
close(sockfd);
}
int main(int argc, char *argv[])
{
int portnumber;
if(argc != 2)
{
fprintf(stderr, "Usage: %s portnumbern", argv[0]);
exit(1);
}
if((portnumber = atoi(argv[1]))