- 论坛徽章:
- 0
|
回复 #2 chary8088 的帖子
源代码都是从网上找来的,还有我是虚拟机上运行的。哪位高人帮我看看有什么问题啊
服务器端源码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 35235
#define BACKLOG 10
main()
{
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
/* The fllow variables using to file */
char lineStr[30];
char recStr[20];
char headStr[15],tailStr[15];
char *delim="=";
FILE *file;
int flag=0;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error" ;
exit(1);
}
//设置本地socket实例的协议族,连接端口等
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), ;
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind error!" ;
exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen error!" ;
exit(1);
}
//监听并处理客户的连接请求
while(1)
{
int sin_size = sizeof(struct sockaddr_in);
if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
{
perror("accept error!" ;
continue;
}
printf("received a connection from %s\n", inet_nto(remote_addr.sin_addr));
//创建子进程,检索目标串并把结果返回个客户端
if (!fork())
{
int len;
if((len=recv(client_fd,recStr,30,0))==-1)
{
printf("Receive error!\n" ;
exit(1);
}
recStr[len]='\0';
file=fopen("server.conf","r" ;
printf("Client send: %s\n",recStr);
while(fgets(lineStr,30,file))
{
//以“=”为标记,分别去等号的左边和右边,分别存入headStr和tailStr
strcpy(headStr,strtok(lineStr,delim));
strcpy(tailStr,strtok(NULL,delim));
if(strcmp(headStr,recStr)==0)
{
flag=1;
if(send(client_fd,tailStr,20,0)==-1)
{
perror("send error!" ;
exit(1);
}
/* break; */
}
}
if(flag==0)
send(client_fd,"Not find",9,0);
close(client_fd);
exit(0);
}
close(client_fd);
}
}
客户端程序我做了一些改动 ,就是main的入口参数,主要是为了调试方便
客户端源码:
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 35235
#define MAXDATASIZE 100
main(int argc, char *argv[])
{
argc=3;
argc[1]="localhost.localname"; //我的主机名
argc[2]="name"; //需要服务器查询的字符串
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if (argc < 3)
{
fprintf(stderr," lease enter the server's hostname and string!\n" ;
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
herror("gethostbyname砮error!" ;
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket?error!" ;
exit(1);
}
//设置连接服务器的协议族,端口号,服务器端的地址等
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero), ;
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect?error!");
exit(1);
}
if(send(sockfd,argv[2],20,0)==-1)
{
printf("send to server error!\n");
exit(1);
}
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)
{
perror("recv?error");
exit(1);
}
buf[recvbytes] = '\0';
printf("Received: %s\n",buf);
close(sockfd);
} |
|